Skip to content

Commit

Permalink
[Sema] Fix a crash in use-before-declaration case (#18466)
Browse files Browse the repository at this point in the history
Check use-before-declaration early so we don't typecheck referenced decl
which might depends on the reference.

https://bugs.swift.org/browse/SR-7517
https://bugs.swift.org/browse/SR-8447
rdar://problem/39782719
  • Loading branch information
rintaro committed Aug 3, 2018
1 parent 53d0ef8 commit c329d72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ static bool findNonMembers(TypeChecker &TC,
}

ValueDecl *D = Result.getValueDecl();
if (!isValid(D))
return false;

if (!D->hasInterfaceType())
TC.validateDecl(D);

Expand All @@ -478,9 +481,6 @@ static bool findNonMembers(TypeChecker &TC,
continue;
}

if (!isValid(D))
return false;

if (matchesDeclRefKind(D, refKind))
ResultValues.push_back(D);
}
Expand Down
14 changes: 13 additions & 1 deletion test/Sema/diag_use_before_declaration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func test() {
guard let bar = foo else {
return
}
let foo = String(bar)
let foo = String(bar) // expected-warning {{initialization of immutable value 'foo' was never used; consider replacing with assignment to '_' or removing it}}
}

// SR-7660
Expand All @@ -30,6 +30,18 @@ class C {
}
}

// SR-7517
func testExample() {
let app = app2 // expected-error {{use of local variable 'app2' before its declaration}}
let app2 = app // expected-note {{'app2' declared here}}
}

// SR-8447
func test_circular() {
let obj = sr8447 // expected-error {{use of local variable 'sr8447' before its declaration}}
let _ = obj.prop, sr8447 // expected-note {{'sr8447' declared here}} expected-error {{type annotation missing in pattern}}
}

//===----------------------------------------------------------------------===//
// Nested scope
//===----------------------------------------------------------------------===//
Expand Down

0 comments on commit c329d72

Please sign in to comment.