1/* 2 * Copyright (c) 2021-2024 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 48// #15276 foo(Object|null) and foo(Object) overloads 49function foo7(p: Object | null): int { 50 return 7; 51} 52 53function main(): void { 54 sameTypeLUB(); 55 objectLUB(); 56 forkSubtypeLUB(); 57} 58 59function sameTypeLUB(): void { 60 let a : A = new A(); 61 let b : A = new A(); 62 let c = true ? a : b; 63 assert(foo(c) == 2); 64} 65 66function objectLUB(): void { 67 let a : A = new A(); 68 let b : Int = 2; 69 let c = true ? a : b; 70 assert(foo(c) == 1); 71 72 let arr : Int[] | null = null; 73 let d = true ? a : arr; 74 assert(foo7(d) == 7); 75} 76 77function forkSubtypeLUB(): void { 78 let a : F = new F(); 79 let b : D = new D(); 80 let c = true ? a : b; 81 assert(foo(c) == 2); 82 let d : A = new A(); 83 let e = true ? a : b; 84 assert(foo(e) == 2); 85 let f : B = new B(); 86 let g = true ? a : f; 87 assert(foo(g) == 3); 88} 89