Skip to content

Commit b825cdc

Browse files
tonisuterrintaro
authored andcommitted
[Parse] [SR-5674] Add fix-it for computed 'let' declaration (#11527)
1 parent 1ab50e6 commit b825cdc

File tree

6 files changed

+17
-11
lines changed

6 files changed

+17
-11
lines changed

include/swift/Parse/Parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@ class Parser {
836836
SourceLoc staticLoc, ParsedAccessors &accessors);
837837
void parseAccessorBodyDelayed(AbstractFunctionDecl *AFD);
838838
VarDecl *parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
839-
SourceLoc StaticLoc, bool hasInitializer,
839+
SourceLoc StaticLoc, SourceLoc VarLoc,
840+
bool hasInitializer,
840841
const DeclAttributes &Attributes,
841842
SmallVectorImpl<Decl *> &Decls);
842843

lib/Parse/ParseDecl.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4035,7 +4035,8 @@ void Parser::parseAccessorBodyDelayed(AbstractFunctionDecl *AFD) {
40354035
/// \brief Parse the brace-enclosed getter and setter for a variable.
40364036
VarDecl *Parser::parseDeclVarGetSet(Pattern *pattern,
40374037
ParseDeclOptions Flags,
4038-
SourceLoc StaticLoc, bool hasInitializer,
4038+
SourceLoc StaticLoc, SourceLoc VarLoc,
4039+
bool hasInitializer,
40394040
const DeclAttributes &Attributes,
40404041
SmallVectorImpl<Decl *> &Decls) {
40414042
bool Invalid = false;
@@ -4090,12 +4091,15 @@ VarDecl *Parser::parseDeclVarGetSet(Pattern *pattern,
40904091

40914092
// Reject accessors on 'let's after parsing them (for better recovery).
40924093
if (PrimaryVar->isLet() && !Attributes.hasAttribute<SILStoredAttr>()) {
4094+
Diag<> DiagID;
40934095
if (accessors.WillSet || accessors.DidSet)
4094-
diagnose(accessors.LBLoc, diag::let_cannot_be_observing_property);
4096+
DiagID = diag::let_cannot_be_observing_property;
40954097
else if (accessors.Addressor || accessors.MutableAddressor)
4096-
diagnose(accessors.LBLoc, diag::let_cannot_be_addressed_property);
4098+
DiagID = diag::let_cannot_be_addressed_property;
40974099
else
4098-
diagnose(accessors.LBLoc, diag::let_cannot_be_computed_property);
4100+
DiagID = diag::let_cannot_be_computed_property;
4101+
4102+
diagnose(accessors.LBLoc, DiagID).fixItReplace(VarLoc, "var");
40994103
PrimaryVar->setSpecifier(VarDecl::Specifier::Var);
41004104
Invalid = true;
41014105
}
@@ -4598,7 +4602,8 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
45984602
} else if (Tok.is(tok::l_brace)) {
45994603
HasAccessors = true;
46004604

4601-
if (auto *boundVar = parseDeclVarGetSet(pattern, Flags, StaticLoc,
4605+
if (auto *boundVar = parseDeclVarGetSet(pattern, Flags,
4606+
StaticLoc, VarLoc,
46024607
PatternInit != nullptr,
46034608
Attributes, Decls)) {
46044609
if (PatternInit && !boundVar->hasStorage()) {

test/Sema/immutability.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
func markUsed<T>(_ t: T) {}
44

5-
let bad_property_1: Int { // expected-error {{'let' declarations cannot be computed properties}}
5+
let bad_property_1: Int { // expected-error {{'let' declarations cannot be computed properties}} {{1-4=var}}
66
get {
77
return 42
88
}
99
}
10-
let bad_property_2: Int = 0 { // expected-error {{'let' declarations cannot be computed properties}} expected-error {{variable with getter/setter cannot have an initial value}}
10+
let bad_property_2: Int = 0 { // expected-error {{'let' declarations cannot be computed properties}} {{1-4=var}} expected-error {{variable with getter/setter cannot have an initial value}}
1111
get {
1212
return 42
1313
}

test/decl/protocol/protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ struct X4 : P1 { // expected-error{{type 'X4' does not conform to protocol 'P1'}
448448

449449
protocol ShouldntCrash {
450450
// rdar://16109996
451-
let fullName: String { get } // expected-error {{'let' declarations cannot be computed properties}}
451+
let fullName: String { get } // expected-error {{'let' declarations cannot be computed properties}} {{3-6=var}}
452452

453453
// <rdar://problem/17200672> Let in protocol causes unclear errors and crashes
454454
let fullName2: String // expected-error {{immutable property requirement must be declared as 'var' with a '{ get }' specifier}}

test/decl/var/properties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ struct PropertiesWithOwnershipTypes {
10271027

10281028
// <rdar://problem/16608609> Assert (and incorrect error message) when defining a constant stored property with observers
10291029
class Test16608609 {
1030-
let constantStored: Int = 0 { // expected-error {{'let' declarations cannot be observing properties}}
1030+
let constantStored: Int = 0 { // expected-error {{'let' declarations cannot be observing properties}} {{4-7=var}}
10311031
willSet {
10321032
}
10331033
didSet {

test/decl/var/static_var.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ extension ProtoAdopter : ProtosEvilTwin {}
259259
public struct Foo { // expected-note {{to match this opening '{'}}}
260260
public static let S { a // expected-error{{computed property must have an explicit type}} {{22-22=: <# Type #>}}
261261
// expected-error@-1{{type annotation missing in pattern}}
262-
// expected-error@-2{{'let' declarations cannot be computed properties}}
262+
// expected-error@-2{{'let' declarations cannot be computed properties}} {{17-20=var}}
263263
// expected-error@-3{{use of unresolved identifier 'a'}}
264264
}
265265

0 commit comments

Comments
 (0)