Skip to content

Commit 09cc860

Browse files
committed
Sema: Simplify recursion breaking for variables
We can use the existing logic for checking recursion in declaration validation. This eliminates some bogus diagnostics and allows some weird logic to be removed from typo correction.
1 parent 8a31911 commit 09cc860

File tree

5 files changed

+24
-30
lines changed

5 files changed

+24
-30
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,6 @@ NOTE(note_typo_candidate_implicit_member,none,
666666
"did you mean the implicitly-synthesized %1 '%0'?", (StringRef, StringRef))
667667
NOTE(note_remapped_type,none,
668668
"did you mean to use '%0'?", (StringRef))
669-
ERROR(identifier_init_failure,none,
670-
"could not infer type for %0", (Identifier))
671-
ERROR(pattern_used_in_type,none,
672-
"%0 used within its own type", (Identifier))
673669
NOTE(note_module_as_type,none,
674670
"cannot use module %0 as a type", (Identifier))
675671

lib/Sema/TypeCheckDecl.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7036,6 +7036,10 @@ void TypeChecker::validateDecl(ValueDecl *D) {
70367036
case DeclKind::Param: {
70377037
auto VD = cast<VarDecl>(D);
70387038

7039+
if (PatternBindingDecl *PBD = VD->getParentPatternBinding())
7040+
if (PBD->isBeingValidated())
7041+
return;
7042+
70397043
D->setIsBeingValidated();
70407044

70417045
if (!VD->hasInterfaceType()) {
@@ -7046,25 +7050,12 @@ void TypeChecker::validateDecl(ValueDecl *D) {
70467050
}
70477051
recordSelfContextType(cast<AbstractFunctionDecl>(VD->getDeclContext()));
70487052
} else if (PatternBindingDecl *PBD = VD->getParentPatternBinding()) {
7049-
if (PBD->isBeingValidated()) {
7050-
diagnose(VD, diag::pattern_used_in_type, VD->getName());
7051-
7052-
} else {
7053-
validatePatternBindingEntries(*this, PBD);
7054-
}
7053+
validatePatternBindingEntries(*this, PBD);
70557054

70567055
auto parentPattern = VD->getParentPattern();
70577056
if (PBD->isInvalid() || !parentPattern->hasType()) {
70587057
parentPattern->setType(ErrorType::get(Context));
70597058
setBoundVarsTypeError(parentPattern, Context);
7060-
7061-
// If no type has been set for the initializer, we need to diagnose
7062-
// the failure.
7063-
if (VD->getParentInitializer() &&
7064-
!VD->getParentInitializer()->getType()) {
7065-
diagnose(parentPattern->getLoc(), diag::identifier_init_failure,
7066-
parentPattern->getBoundName());
7067-
}
70687059
}
70697060
} else {
70707061
// FIXME: This case is hit when code completion occurs in a function
@@ -7172,6 +7163,14 @@ void TypeChecker::validateDecl(ValueDecl *D) {
71727163
auto *FD = cast<FuncDecl>(D);
71737164
assert(!FD->hasInterfaceType());
71747165

7166+
// Bail out if we're in a recursive validation situation.
7167+
if (auto accessor = dyn_cast<AccessorDecl>(FD)) {
7168+
auto *storage = accessor->getStorage();
7169+
validateDecl(storage);
7170+
if (!storage->hasValidSignature())
7171+
return;
7172+
}
7173+
71757174
checkDeclAttributesEarly(FD);
71767175
computeAccessLevel(FD);
71777176

@@ -7208,7 +7207,6 @@ void TypeChecker::validateDecl(ValueDecl *D) {
72087207
// FIXME: should this include the generic signature?
72097208
if (auto accessor = dyn_cast<AccessorDecl>(FD)) {
72107209
auto storage = accessor->getStorage();
7211-
validateDecl(storage);
72127210

72137211
// Note that it's important for correctness that we're filling in
72147212
// empty TypeLocs, because otherwise revertGenericFuncSignature might

test/ClangImporter/MixedSource/can_import_objc_idempotent.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
#endif
2727

2828
#if canImport(CoreGraphics)
29-
let square = CGRect(x: 100, y: 100, width: 100, height: 100) // expected-error {{use of unresolved identifier 'CGRect'}}
30-
// expected-error@-1 {{'square' used within its own type}}
31-
// expected-error@-2 {{could not infer type for 'square'}}
29+
let square = CGRect(x: 100, y: 100, width: 100, height: 100)
30+
// expected-error@-1 {{use of unresolved identifier 'CGRect'}}
31+
// expected-note@-2 {{'square' declared here}}
32+
3233
let (r, s) = square.divided(atDistance: 50, from: .minXEdge)
34+
// expected-error@-1 {{ambiguous use of 'square'}}
3335
#endif
3436

3537
#if canImport(MixedWithHeader)
3638
let object = NSObject() // expected-error {{use of unresolved identifier 'NSObject'}}
37-
// expected-error@-1 {{'object' used within its own type}}
38-
// expected-error@-2 {{could not infer type for 'object'}}
3939
let someAPI = Derived() // expected-error {{use of unresolved identifier 'Derived'}}
4040
#endif

test/NameBinding/scope_map_lookup.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,6 @@ protocol Fooable {
118118
struct S<T> // expected-error{{expected '{' in struct}}
119119
extension S // expected-error{{expected '{' in extension}}
120120

121-
let a = b ; let b = a // expected-error{{could not infer type for 'a'}}
122-
// expected-error@-1 {{'a' used within its own type}}
123-
// FIXME: That second error is bogus.
121+
let a = b ; let b = a
122+
// expected-note@-1 {{'a' declared here}}
123+
// expected-error@-2 {{ambiguous use of 'a'}}

test/decl/var/variables.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ class SomeClass {}
6565
// <rdar://problem/16877304> weak let's should be rejected
6666
weak let V = SomeClass() // expected-error {{'weak' must be a mutable variable, because it may change at runtime}}
6767

68-
let a = b ; let b = a // expected-error{{could not infer type for 'a'}}
69-
// expected-error@-1 {{'a' used within its own type}}
70-
// FIXME: That second error is bogus.
68+
let a = b ; let b = a
69+
// expected-note@-1 {{'a' declared here}}
70+
// expected-error@-2 {{ambiguous use of 'a'}}
7171

7272
// <rdar://problem/17501765> Swift should warn about immutable default initialized values
7373
let uselessValue : String?

0 commit comments

Comments
 (0)