Skip to content

Commit a83e977

Browse files
committed
[Sema] Enforce we don't type-check bindings independently of closures
Bindings in closures must be type-checked together with the surrounding closure, add an assertion to make sure we don't try this. Carve out an exception for code completion and error cases which may still kick lazy type-checking. We ought to eventually fix up all the error cases cases though since they imply we're leaving bits of the AST un-type-checked.
1 parent 66c010f commit a83e977

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,23 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
841841
initializer, DC, patternType, pattern,
842842
/*bindPatternVarsOneWay=*/false);
843843

844+
// Bindings cannot be type-checked independently from their context in a
845+
// closure, make sure we don't ever attempt to do this. If we want to be able
846+
// to lazily type-check these we'll need to type-check the entire surrounding
847+
// expression.
848+
if (auto *CE = dyn_cast<ClosureExpr>(DC)) {
849+
if (!PBD || !isPlaceholderVar(PBD)) {
850+
// Completion may trigger lazy type-checking as part of fallback
851+
// unqualified lookup, just decline to type-check.
852+
auto &ctx = CE->getASTContext();
853+
if (ctx.SourceMgr.hasIDEInspectionTargetBuffer()) {
854+
target.markInvalid();
855+
return true;
856+
}
857+
ABORT("Cannot type-check PatternBindingDecl without closure context");
858+
}
859+
}
860+
844861
// Type-check the initializer.
845862
auto resultTarget = typeCheckExpression(target, options);
846863

test/IDE/complete_unqualified_fallback.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %batch-code-completion
1+
// RUN: %batch-code-completion -solver-scope-threshold=50
22

33
protocol P0 {}
44
protocol P1 {}
@@ -47,8 +47,16 @@ S {
4747
S {
4848
S {
4949
S {
50+
let x = 0
51+
let y: Int = 0
5052
#^COMPLETE^#
51-
// COMPLETE: Decl[Struct]/CurrModule: FooBar[#FooBar#]; name=FooBar
53+
// COMPLETE-DAG: Decl[Struct]/CurrModule: FooBar[#FooBar#]; name=FooBar
54+
55+
// We decline to type-check a variable indepedently of the surrounding
56+
// closure, so this has an ErrorType. If this changes, you either need
57+
// to make this test case more complex or tweak the limits.
58+
// COMPLETE-DAG: Decl[LocalVar]/Local: x[#_#]; name=x
59+
// COMPLETE-DAG: Decl[LocalVar]/Local: y[#Int#]; name=y
5260
}
5361
}
5462
}

0 commit comments

Comments
 (0)