Skip to content

Commit 92d520c

Browse files
committed
ModulePrinting: Using constraint solver to decide the right overload to print in synthesized extensions.
1 parent 76b31a9 commit 92d520c

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

lib/Sema/CSGen.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,17 +3267,18 @@ swift::resolveValueMember(DeclContext &DC, Type BaseTy, DeclName Name) {
32673267
ConstraintKind::ValueMember, Name, BaseTy, nullptr, false);
32683268
if (LookupResult.ViableCandidates.empty())
32693269
return Result;
3270-
if (OverloadChoice *Choice = LookupResult.getFavoredChoice()) {
3271-
Result.Favored = Choice->getDecl();
3272-
}
3270+
ConstraintLocator *Locator = CS.getConstraintLocator(nullptr);
3271+
TypeVariableType *TV = CS.createTypeVariable(Locator, TVO_CanBindToLValue);
3272+
CS.addOverloadSet(TV, LookupResult.ViableCandidates, Locator);
3273+
Optional<Solution> OpSolution = CS.solveSingle();
3274+
if (!OpSolution.hasValue())
3275+
return Result;
3276+
SelectedOverload Selected = OpSolution.getValue().overloadChoices[Locator];
3277+
Result.Favored = Selected.choice.getDecl();
32733278
for (OverloadChoice& Choice : LookupResult.ViableCandidates) {
32743279
ValueDecl *VD = Choice.getDecl();
32753280
if (VD != Result.Favored)
32763281
Result.OtherViables.push_back(VD);
32773282
}
3278-
if (!Result.Favored) {
3279-
Result.Favored = Result.OtherViables.front();
3280-
Result.OtherViables.erase(Result.OtherViables.begin());
3281-
}
32823283
return Result;
32833284
}

test/IDE/print_synthesized_extensions.swift

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,31 @@ public extension P5 {
173173
public func foo2() {}
174174
}
175175

176+
public extension P5 {
177+
178+
/// This is not picked
179+
public func foo3() {}
180+
}
181+
182+
public extension P5 where T1 : Comparable{
183+
184+
/// This is picked
185+
public func foo3() {}
186+
}
187+
188+
public extension P5 where T1 : Comparable {
189+
190+
/// This is picked
191+
public func foo4() {}
192+
}
193+
194+
public extension P5 {
195+
196+
/// This is not picked
197+
public func foo4() {}
198+
}
199+
200+
176201
public struct S12 : P5{
177202
public typealias T1 = Int
178203
public func foo1() {}
@@ -240,10 +265,14 @@ public struct S12 : P5{
240265
// CHECK10-NEXT: <decl:Func>public func <loc>ef5(<decl:Param>t: <ref:Struct>S5</ref></decl>)</loc></decl>
241266
// CHECK10-NEXT: }</synthesized>
242267

243-
// CHECK11: <decl:Struct>public struct <loc>S12</loc> : <ref:Protocol>P5</ref> {
244-
// CHECK11-NEXT: <decl:TypeAlias>public typealias <loc>T1</loc> = <ref:Struct>Int</ref></decl>
245-
// CHECK11-NEXT: <decl:Func>/// This is picked
246-
// CHECK11-NEXT: public func <loc>foo1()</loc></decl></decl>
247-
// CHECK11-NEXT: <decl:Func>/// This is picked
248-
// CHECK11-NEXT: public func <loc>foo2()</loc></decl>
268+
// CHECK11: <decl:Struct>public struct <loc>S12</loc> : <ref:Protocol>P5</ref> {
269+
// CHECK11-NEXT: <decl:TypeAlias>public typealias <loc>T1</loc> = <ref:Struct>Int</ref></decl>
270+
// CHECK11-NEXT: <decl:Func>/// This is picked
271+
// CHECK11-NEXT: public func <loc>foo1()</loc></decl></decl>
272+
// CHECK11-NEXT: <decl:Func>/// This is picked
273+
// CHECK11-NEXT: public func <loc>foo2()</loc></decl>
274+
// CHECK11-NEXT: <decl:Func>/// This is picked
275+
// CHECK11-NEXT: public func <loc>foo3()</loc></decl>
276+
// CHECK11-NEXT: <decl:Func>/// This is picked
277+
// CHECK11-NEXT: public func <loc>foo4()</loc></decl>
249278
// CHECK11-NEXT: }</synthesized>

0 commit comments

Comments
 (0)