Skip to content

Commit 6435083

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 7fecda7 commit 6435083

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
@@ -10428,6 +10428,34 @@ bool ConstraintSystem::simplifyAppliedOverloadsImpl(
1042810428
return true;
1042910429
}
1043010430

10431+
// This is the situation where a property has the same name
10432+
// as a method e.g.
10433+
//
10434+
// protocol P {
10435+
// var test: String { get }
10436+
// }
10437+
//
10438+
// extension P {
10439+
// var test: String { get { return "" } }
10440+
// }
10441+
//
10442+
// struct S : P {
10443+
// func test() -> Int { 42 }
10444+
// }
10445+
//
10446+
// var s = S()
10447+
// s.test() <- disjunction would have two choices here, one
10448+
// for the property from `P` and one for the method of `S`.
10449+
//
10450+
// In cases like this, let's exclude property overload from common
10451+
// result determination because it cannot be applied.
10452+
//
10453+
// Note that such overloads cannot be disabled, because they still
10454+
// have to be checked in diagnostic mode and there is (currently)
10455+
// no way to re-enable them for diagnostics.
10456+
if (!choiceType->lookThroughAllOptionalTypes()->is<FunctionType>())
10457+
return true;
10458+
1043110459
// If types lined up exactly, let's favor this overload choice.
1043210460
if (Type(argFnType)->isEqual(choiceType))
1043310461
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)