Skip to content

Commit 4097428

Browse files
committed
[Constraint solver] Declarations with IUOs don’t have effective overload types
A declaration with an implicitly-unwrapped optional essentially has two effective overload types, because the result might be optional or it might have been forced. Disable computation of the effective overload type in this case.
1 parent 1b2b6f9 commit 4097428

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,11 @@ Type ConstraintSystem::getEffectiveOverloadType(const OverloadChoice &overload,
14881488
if (isa<TypeDecl>(decl))
14891489
return Type();
14901490

1491+
// Declarations returning unwrapped optionals don't have a single effective
1492+
// type.
1493+
if (decl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>())
1494+
return Type();
1495+
14911496
// Retrieve the interface type.
14921497
auto type = decl->getInterfaceType();
14931498
if (!type) {

test/Constraints/common_type.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ struct X {
77

88
subscript(_: Int) -> String { return "" }
99
subscript(_: Double) -> String { return "" }
10+
11+
func iuo(_: Int) -> Int! { return 0 }
12+
func iuo(_: Double) -> Int! { return 0 }
1013
}
1114

1215
struct Y {
@@ -15,6 +18,9 @@ struct Y {
1518

1619
subscript(_: Int) -> Substring { return "" }
1720
subscript(_: Double) -> Substring { return "" }
21+
22+
func iuo(_: Int) -> Double! { return 0 }
23+
func iuo(_: Double) -> Double! { return 0 }
1824
}
1925

2026
func f(_: Int) -> X { return X() }
@@ -29,11 +35,16 @@ func testCallCommonType() {
2935
}
3036

3137
func testSubscriptCommonType() {
32-
// FIXME: This will work once we have more filtering of subscripts.
3338
// CHECK: subscript_expr
3439
// CHECK: overload set choice binding $T{{[0-9]+}} := (Int) -> X
35-
// CHECK-NOT: (common result type for $T{{[0-9]+}} is String)
40+
// CHECK: (common result type for $T{{[0-9]+}} is String)
3641
// CHECK: (overload set choice binding $T{{[0-9]+}} := (Double) -> Y)
37-
// CHECK-NOT: (common result type for $T{{[0-9]+}} is Substring)
42+
// CHECK: (common result type for $T{{[0-9]+}} is Substring)
3843
_ = f(0)[0]
3944
}
45+
46+
func testCommonTypeIUO() {
47+
// CHECK: overload set choice binding $T{{[0-9]+}} := (Int) -> X
48+
// CHECK-NOT: common result type
49+
_ = f(0).iuo(0)
50+
}

0 commit comments

Comments
 (0)