Skip to content

Commit 85fea89

Browse files
committed
fix two more issues found by the stress tester
1 parent 3808cb1 commit 85fea89

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5709,8 +5709,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
57095709
if (!type1->is<LValueType>() &&
57105710
type2->isExistentialType()) {
57115711

5712-
// Penalize conversions to Any.
5713-
if (kind >= ConstraintKind::Conversion && type2->isAny())
5712+
// Penalize conversions to Any, unless we're solving for code completion.
5713+
// Code completion should offer completions both from solutions with
5714+
// overloads involving Any and those with more specific types because the
5715+
// completion the user then choses can force the Any overload to be
5716+
// picked.
5717+
if (kind >= ConstraintKind::Conversion && type2->isAny() &&
5718+
!isForCodeCompletion())
57145719
increaseScore(ScoreKind::SK_EmptyExistentialConversion);
57155720

57165721
conversionsOrFixes.push_back(ConversionRestrictionKind::Existential);

lib/Sema/TypeCheckCodeCompletion.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,20 @@ class SanitizeExpr : public ASTWalker {
183183
continue;
184184
}
185185

186-
// Restore '@autoclosure'd value.
187186
if (auto ACE = dyn_cast<AutoClosureExpr>(expr)) {
187+
// Restore '@autoclosure'd value.
188188
// This is only valid if the closure doesn't have parameters.
189189
if (ACE->getParameters()->size() == 0) {
190190
expr = ACE->getSingleExpressionBody();
191191
continue;
192192
}
193+
194+
// Restore autoclosure'd function reference.
195+
if (auto *unwrapped = ACE->getUnwrappedCurryThunkExpr()) {
196+
expr = unwrapped;
197+
continue;
198+
}
199+
193200
llvm_unreachable("other AutoClosureExpr must be handled specially");
194201
}
195202

test/IDE/complete_ambiguous.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ _ = testing([Point(4, 89)]) { arg in
383383

384384
struct Thing {
385385
init(_ block: (Point) -> Void) {}
386+
387+
enum ThingEnum { case first, second }
388+
func doStuff(_ x: ThingEnum, _ y: Int) -> Thing { return self }
389+
func takesRef(_ ref: () -> ()) -> Thing { return self }
386390
}
387391
@resultBuilder
388392
struct ThingBuilder {
@@ -429,6 +433,20 @@ CreateThings {
429433
Thing. // ErrorExpr
430434
}
431435

436+
struct TestFuncBodyBuilder {
437+
func someFunc() {}
438+
439+
@ThingBuilder func foo() -> [Thing] {
440+
Thing()
441+
.doStuff(.#^FUNCBUILDER_FUNCBODY^#, 3)
442+
.takesRef(someFunc)
443+
}
444+
}
445+
// FUNCBUILDER_FUNCBODY: Begin completions, 3 items
446+
// FUNCBUILDER_FUNCBODY-DAG: Decl[EnumElement]/CurrNominal/Flair[ExprSpecific]/TypeRelation[Identical]: first[#Thing.ThingEnum#];
447+
// FUNCBUILDER_FUNCBODY-DAG: Decl[EnumElement]/CurrNominal/Flair[ExprSpecific]/TypeRelation[Identical]: second[#Thing.ThingEnum#];
448+
// FUNCBUILDER_FUNCBODY-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: hash({#(self): Thing.ThingEnum#})[#(into: inout Hasher) -> Void#];
449+
// FUNCBUILDER_FUNCBODY: End completions
432450

433451
func takesClosureOfPoint(_: (Point)->()) {}
434452
func overloadedWithDefaulted(_: ()->()) {}

test/IDE/complete_subscript.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=LABELED_SUBSCRIPT | %FileCheck %s -check-prefix=LABELED_SUBSCRIPT
1919
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TUPLE | %FileCheck %s -check-prefix=TUPLE
20+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SETTABLE_SUBSCRIPT | %FileCheck %s -check-prefix=SETTABLE_SUBSCRIPT
2021

2122
struct MyStruct<T> {
2223
static subscript(x: Int, static defValue: T) -> MyStruct<T> {
@@ -159,3 +160,20 @@ func testSubcscriptTuple(val: (x: Int, String)) {
159160
// TUPLE-DAG: Pattern/CurrNominal/Flair[ArgLabels]: ['[']{#keyPath: KeyPath<(x: Int, String), Value>#}[']'][#Value#];
160161
// TUPLE: End completions
161162
}
163+
164+
struct HasSettableSub {
165+
subscript(a: String) -> Any {
166+
get { return 1 }
167+
set { }
168+
}
169+
}
170+
171+
func testSettableSub(x: inout HasSettableSub) {
172+
let local = "some string"
173+
x[#^SETTABLE_SUBSCRIPT^#] = 32
174+
}
175+
// SETTABLE_SUBSCRIPT: Begin completions
176+
// SETTABLE_SUBSCRIPT-DAG: Pattern/CurrNominal/Flair[ArgLabels]: ['[']{#keyPath: KeyPath<HasSettableSub, Value>#}[']'][#Value#];
177+
// SETTABLE_SUBSCRIPT-DAG: Decl[Subscript]/CurrNominal/Flair[ArgLabels]: ['[']{#(a): String#}[']'][#@lvalue Any#];
178+
// SETTABLE_SUBSCRIPT-DAG: Decl[LocalVar]/Local/TypeRelation[Identical]: local[#String#]; name=local
179+
// SETTABLE_SUBSCRIPT: End completions

0 commit comments

Comments
 (0)