Skip to content

[5.8][CSGen] Handle recursive use of variable declarations #63551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1351,20 +1351,33 @@ namespace {
Type visitDeclRefExpr(DeclRefExpr *E) {
auto locator = CS.getConstraintLocator(E);

auto invalidateReference = [&]() -> Type {
auto *hole = CS.createTypeVariable(locator, TVO_CanBindToHole);
(void)CS.recordFix(AllowRefToInvalidDecl::create(CS, locator));
CS.setType(E, hole);
return hole;
};

Type knownType;
if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
knownType = CS.getTypeIfAvailable(VD);
if (!knownType)
knownType = CS.getVarType(VD);

if (knownType) {
// An out-of-scope type variable could be a type of a declaration
// only in diagnostic mode when invalid variable declaration is
// recursively referenced inside of a multi-statement closure
// located somewhere within its initializer e.g.:
// `let x = [<call>] { ... print(x) }`
if (auto *typeVar = knownType->getAs<TypeVariableType>()) {
if (!CS.isActiveTypeVariable(typeVar))
return invalidateReference();
}

// If the known type has an error, bail out.
if (knownType->hasError()) {
auto *hole = CS.createTypeVariable(locator, TVO_CanBindToHole);
(void)CS.recordFix(AllowRefToInvalidDecl::create(CS, locator));
if (!CS.hasType(E))
CS.setType(E, hole);
return hole;
return invalidateReference();
}

if (!knownType->hasPlaceholder()) {
Expand All @@ -1381,10 +1394,7 @@ namespace {
// (in getTypeOfReference) so we can match non-error param types.
if (!knownType && E->getDecl()->isInvalid() &&
!CS.isForCodeCompletion()) {
auto *hole = CS.createTypeVariable(locator, TVO_CanBindToHole);
(void)CS.recordFix(AllowRefToInvalidDecl::create(CS, locator));
CS.setType(E, hole);
return hole;
return invalidateReference();
}

// Create an overload choice referencing this declaration and immediately
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TypeVariableRefFinder : public ASTWalker {
if (auto *DRE = dyn_cast<DeclRefExpr>(expr)) {
auto *decl = DRE->getDecl();

if (auto type = CS.getTypeIfAvailable(DRE->getDecl())) {
if (auto type = CS.getTypeIfAvailable(decl)) {
auto &ctx = CS.getASTContext();
// If this is not one of the closure parameters which
// is inferrable from the body, let's replace type
Expand Down
13 changes: 13 additions & 0 deletions test/expr/closure/multi_statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,16 @@ func test_that_closures_are_attempted_in_order() {
return false
}
}

// https://github.com/apple/swift/issues/63455
func test_recursive_var_reference_in_multistatement_closure() {
func takeClosure(_ x: () -> Void) {}

func test(optionalInt: Int?) {
takeClosure {
let int = optionalInt { // expected-error {{cannot call value of non-function type 'Int?'}}
print(int)
}
}
}
}
25 changes: 25 additions & 0 deletions validation-test/IDE/crashers_2_fixed/issue-63455.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %swift-ide-test -code-completion -source-filename %s -code-completion-token COMPLETE

func sheet(onDismiss: () -> Void) -> EmptyView {
fatalError()
}

@resultBuilder struct ViewBuilder2 {
static func buildBlock(_ content: EmptyView) -> EmptyView {
return content
}
}

struct EmptyView {}

struct SettingsView {
var importedFile: Int?

@ViewBuilder2 var body2: EmptyView {
sheet {
#^COMPLETE^#if let url = self.importedFile {
print(url)
}
}
}
}