Skip to content

Commit 29a04d4

Browse files
committed
[Diags] Don't error twice for incomplete computed properties in extensions
If you have an empty computed property in an extension: ```swift struct S {} extension S { var foo: Int { } } ``` Then Swift will give 2 conflicting errors: one that calls it a computed property and another that calls it a stored property. ``` 5:5: error: computed property must have accessors specified } ^ 4:9: error: extensions must not contain stored properties var foo: Int { ^ ``` This removes the unhelpful second diagnostic by adding a getter to the property when the first diagnostic is in an extension. This happens to me frequently. I'll decide to add a computed property, write its declaration, then pause to decide how to implement it. The presence of a 2nd error is both distracting and confusing.
1 parent b121ce9 commit 29a04d4

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5448,6 +5448,16 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags,
54485448
if (parsingLimitedSyntax)
54495449
return makeParserSuccess();
54505450

5451+
// In an extension, add a getter to prevent an "extensions must not contain
5452+
// stored properties" error.
5453+
if (Flags.contains(PD_InExtension)) {
5454+
auto getter = createAccessorFunc(
5455+
Tok.getLoc(), /*ValueNamePattern*/ nullptr, GenericParams, Indices,
5456+
StaticLoc, Flags, AccessorKind::Get, storage, this,
5457+
/*AccessorKeywordLoc*/ SourceLoc());
5458+
accessors.add(getter);
5459+
}
5460+
54515461
diagnose(accessors.RBLoc, diag::computed_property_no_accessors,
54525462
/*subscript*/ Indices != nullptr);
54535463
return makeParserError();

test/decl/var/properties.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ var x12: X {
380380

381381
var x13: X {} // expected-error {{computed property must have accessors specified}}
382382

383+
struct X14 {}
384+
extension X14 {
385+
var x14: X {
386+
} // expected-error {{computed property must have accessors specified}}
387+
}
388+
383389
// Type checking problems
384390
struct Y { }
385391
var y: Y

0 commit comments

Comments
 (0)