Skip to content

Commit 9a9b4af

Browse files
committed
[Parser] Remove custom diagnostic that relies on AST-level name lookup.
The parser was using an AST-level name lookup operation to provide a slightly improved diagnostic (suggesting "self." if there was a member of that name in the enclosing type context). This is a fairly unfortunate layering violation: name lookup cannot produce correct results at this point. Remove the custom diagnostic and the lookup; this can come back when all lookup moves out of the parser.
1 parent 7d45ef5 commit 9a9b4af

File tree

3 files changed

+6
-31
lines changed

3 files changed

+6
-31
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ ERROR(disallowed_init,none,
223223
"initial value is not allowed here", ())
224224
ERROR(var_init_self_referential,none,
225225
"variable used within its own initial value", ())
226-
ERROR(expected_self_before_reference,none,
227-
"variable used within its own initial value; use 'self.' to refer to the %0", (DescriptiveDeclKind))
228226

229227
ERROR(disallowed_enum_element,none,
230228
"enum 'case' is not allowed outside of an enum", ())

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,22 +2114,6 @@ DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
21142114
return DeclName(Context, baseName, argumentLabels);
21152115
}
21162116

2117-
static bool shouldAddSelfFixit(DeclContext* Current, DeclName Name,
2118-
DescriptiveDeclKind &Kind) {
2119-
if (Current->isTypeContext() || !Current->getInnermostTypeContext())
2120-
return false;
2121-
if (auto *Nominal = Current->getInnermostTypeContext()->
2122-
getAsNominalTypeOrNominalTypeExtensionContext()){
2123-
// FIXME: we cannot resolve members appear later in the body of the nominal.
2124-
auto LookupResults = Nominal->lookupDirect(Name);
2125-
if (!LookupResults.empty()) {
2126-
Kind = LookupResults.front()->getDescriptiveKind();
2127-
return true;
2128-
}
2129-
}
2130-
return false;
2131-
}
2132-
21332117
/// expr-identifier:
21342118
/// unqualified-decl-name generic-args?
21352119
Expr *Parser::parseExprIdentifier() {
@@ -2180,14 +2164,7 @@ Expr *Parser::parseExprIdentifier() {
21802164
} else {
21812165
for (auto activeVar : DisabledVars) {
21822166
if (activeVar->getFullName() == name) {
2183-
DescriptiveDeclKind Kind;
2184-
if (DisabledVarReason.ID == diag::var_init_self_referential.ID &&
2185-
shouldAddSelfFixit(CurDeclContext, name, Kind)) {
2186-
diagnose(loc.getBaseNameLoc(), diag::expected_self_before_reference,
2187-
Kind).fixItInsert(loc.getBaseNameLoc(), "self.");
2188-
} else {
2189-
diagnose(loc.getBaseNameLoc(), DisabledVarReason);
2190-
}
2167+
diagnose(loc.getBaseNameLoc(), DisabledVarReason);
21912168
return new (Context) ErrorExpr(loc.getSourceRange());
21922169
}
21932170
}

test/Sema/diag_variable_used_in_initial.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
class A1 {
44
func foo1() {}
55
func foo2() {
6-
var foo1 = foo1() // expected-error {{variable used within its own initial value; use 'self.' to refer to the instance method}}{{16-16=self.}}
6+
var foo1 = foo1() // expected-error {{variable used within its own initial value}}
77
}
88
}
99

1010
class A2 {
1111
var foo1 = 2
1212
func foo2() {
1313
// FIXME: "the var" doesn't sound right.
14-
var foo1 = foo1 // expected-error {{variable used within its own initial value; use 'self.' to refer to the property}}{{16-16=self.}}
14+
var foo1 = foo1 // expected-error {{variable used within its own initial value}}
1515
}
1616
}
1717

@@ -33,21 +33,21 @@ func localContext() {
3333
class A5 {
3434
func foo1() {}
3535
func foo2() {
36-
var foo1 = foo1() // expected-error {{variable used within its own initial value; use 'self.' to refer to the instance method}}{{18-18=self.}}
36+
var foo1 = foo1() // expected-error {{variable used within its own initial value}}
3737
}
3838

3939
class A6 {
4040
func foo1() {}
4141
func foo2() {
42-
var foo1 = foo1() // expected-error {{variable used within its own initial value; use 'self.' to refer to the instance method}}{{20-20=self.}}
42+
var foo1 = foo1() // expected-error {{variable used within its own initial value}}
4343
}
4444
}
4545

4646
extension E { // expected-error {{declaration is only valid at file scope}}
4747
class A7 {
4848
func foo1() {}
4949
func foo2() {
50-
var foo1 = foo1() // expected-error {{variable used within its own initial value; use 'self.' to refer to the instance method}}{{22-22=self.}}
50+
var foo1 = foo1() // expected-error {{variable used within its own initial value}}
5151
}
5252
}
5353
}

0 commit comments

Comments
 (0)