• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16interface I {}
17
18class A {}
19class B extends A implements I {}
20final class C extends A implements I {}
21final class D extends A {}
22final class F extends B {}
23
24function foo(p: Object): int {
25  return 1;
26}
27
28function foo(p: A): int {
29  return 2;
30}
31
32function foo(p: B): int {
33  return 3;
34}
35
36function foo(p: C): int {
37  return 4;
38}
39
40function foo(p: D): int {
41  return 5;
42}
43
44function foo(p: F): int {
45  return 6;
46}
47
48function getTrue(): boolean {
49  return true
50}
51
52// #15276 foo(Object|null) and foo(Object) overloads
53function foo7(p: Object | null): int {
54    return 7;
55}
56
57function main(): void {
58    sameTypeLUB();
59    objectLUB();
60    forkSubtypeLUB();
61}
62
63function sameTypeLUB(): void {
64    let a : A = new A();
65    let b : A = new A();
66    let c = getTrue() ? a : b;
67    assertEQ(foo(c), 2)
68}
69
70function objectLUB(): void {
71    let a : A = new A();
72    let b : Int = 2;
73    let c = getTrue() ? a : b;
74    assertEQ(foo(c), 1)
75
76    let arr : Int[] | null = null;
77    let d = getTrue() ? a : arr;
78    assertEQ(foo7(d), 7)
79}
80
81function forkSubtypeLUB(): void {
82    let a : F = new F();
83    let b : D = new D();
84    let c = getTrue() ? a : b;
85    assertEQ(foo(c), 2)
86    let d : A = new A();
87    let e = getTrue() ? a : b;
88    assertEQ(foo(e), 2)
89    let f : B = new B();
90    let g = getTrue() ? a : f;
91    assertEQ(foo(g), 3)
92}
93