Skip to content

[Parser] Remove custom diagnostic that relies on AST-level name lookup. #18470

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

Merged
Merged
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
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ ERROR(disallowed_init,none,
"initial value is not allowed here", ())
ERROR(var_init_self_referential,none,
"variable used within its own initial value", ())
ERROR(expected_self_before_reference,none,
"variable used within its own initial value; use 'self.' to refer to the %0", (DescriptiveDeclKind))

ERROR(disallowed_enum_element,none,
"enum 'case' is not allowed outside of an enum", ())
Expand Down
25 changes: 1 addition & 24 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2114,22 +2114,6 @@ DeclName Parser::parseUnqualifiedDeclName(bool afterDot,
return DeclName(Context, baseName, argumentLabels);
}

static bool shouldAddSelfFixit(DeclContext* Current, DeclName Name,
DescriptiveDeclKind &Kind) {
if (Current->isTypeContext() || !Current->getInnermostTypeContext())
return false;
if (auto *Nominal = Current->getInnermostTypeContext()->
getAsNominalTypeOrNominalTypeExtensionContext()){
// FIXME: we cannot resolve members appear later in the body of the nominal.
auto LookupResults = Nominal->lookupDirect(Name);
if (!LookupResults.empty()) {
Kind = LookupResults.front()->getDescriptiveKind();
return true;
}
}
return false;
}

/// expr-identifier:
/// unqualified-decl-name generic-args?
Expr *Parser::parseExprIdentifier() {
Expand Down Expand Up @@ -2180,14 +2164,7 @@ Expr *Parser::parseExprIdentifier() {
} else {
for (auto activeVar : DisabledVars) {
if (activeVar->getFullName() == name) {
DescriptiveDeclKind Kind;
if (DisabledVarReason.ID == diag::var_init_self_referential.ID &&
shouldAddSelfFixit(CurDeclContext, name, Kind)) {
diagnose(loc.getBaseNameLoc(), diag::expected_self_before_reference,
Kind).fixItInsert(loc.getBaseNameLoc(), "self.");
} else {
diagnose(loc.getBaseNameLoc(), DisabledVarReason);
}
diagnose(loc.getBaseNameLoc(), DisabledVarReason);
return new (Context) ErrorExpr(loc.getSourceRange());
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/Sema/diag_variable_used_in_initial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
class A1 {
func foo1() {}
func foo2() {
var foo1 = foo1() // expected-error {{variable used within its own initial value; use 'self.' to refer to the instance method}}{{16-16=self.}}
var foo1 = foo1() // expected-error {{variable used within its own initial value}}
}
}

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

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

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

extension E { // expected-error {{declaration is only valid at file scope}}
class A7 {
func foo1() {}
func foo2() {
var foo1 = foo1() // expected-error {{variable used within its own initial value; use 'self.' to refer to the instance method}}{{22-22=self.}}
var foo1 = foo1() // expected-error {{variable used within its own initial value}}
}
}
}
Expand Down