Skip to content

Commit b9aac87

Browse files
committed
[ConstraintSystem] Exclude not-applicable members (e.g. properties) from common result compulation
This is the situation where a property has the same name as a method e.g. ```swift protocol P { var test: String { get } } extension P { var test: String { get { return "" } } } struct S : P { func test() -> Int { 42 } } var s = S() s.test() // disjunction would have two choices here, one // for the property from `P` and one for the method of `S`. ``` In cases like this, let's exclude property overload from common result determination because it cannot be applied. Note that such overloads cannot be disabled, because they still have to be checked in diagnostic mode and there is (currently) no way to re-enable them for diagnostics.
1 parent 7c66f70 commit b9aac87

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10432,6 +10432,34 @@ bool ConstraintSystem::simplifyAppliedOverloadsImpl(
1043210432
return true;
1043310433
}
1043410434

10435+
// This is the situation where a property has the same name
10436+
// as a method e.g.
10437+
//
10438+
// protocol P {
10439+
// var test: String { get }
10440+
// }
10441+
//
10442+
// extension P {
10443+
// var test: String { get { return "" } }
10444+
// }
10445+
//
10446+
// struct S : P {
10447+
// func test() -> Int { 42 }
10448+
// }
10449+
//
10450+
// var s = S()
10451+
// s.test() <- disjunction would have two choices here, one
10452+
// for the property from `P` and one for the method of `S`.
10453+
//
10454+
// In cases like this, let's exclude property overload from common
10455+
// result determination because it cannot be applied.
10456+
//
10457+
// Note that such overloads cannot be disabled, because they still
10458+
// have to be checked in diagnostic mode and there is (currently)
10459+
// no way to re-enable them for diagnostics.
10460+
if (!choiceType->lookThroughAllOptionalTypes()->is<FunctionType>())
10461+
return true;
10462+
1043510463
// If types lined up exactly, let's favor this overload choice.
1043610464
if (Type(argFnType)->isEqual(choiceType))
1043710465
constraint->setFavored();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %scale-test --begin 1 --end 10 --step 1 --select NumLeafScopes %s
2+
// REQUIRES: asserts,no_asan
3+
4+
func test(_: [A]) {}
5+
6+
class A {}
7+
8+
protocol P {
9+
var arr: Int { get }
10+
}
11+
12+
extension P {
13+
var arr: Int { get { return 42 } }
14+
}
15+
16+
class S : P {
17+
func arr() -> [A] { [] }
18+
func arr(_: Int = 42) -> [A] { [] }
19+
}
20+
21+
// There is a clash between `arr` property and `arr` methods
22+
// returning `[A]` which shouldn't prevent "common result"
23+
// determination.
24+
func run_test(s: S) {
25+
test(s.arr() +
26+
%for i in range(0, N):
27+
s.arr() +
28+
%end
29+
s.arr())
30+
}

0 commit comments

Comments
 (0)