Skip to content

Commit f2bdce8

Browse files
mzprintaro
authored andcommitted
[SR-8340]Improve fix-it for var and subscript in Protocol (#19660)
* [Parser] Improve fix-it for subscription in protocol * [Sema] Add fix-it for property in protocol https://bugs.swift.org/browse/SR-8340
1 parent 6ab8389 commit f2bdce8

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6226,7 +6226,7 @@ Parser::parseDeclSubscript(ParseDeclOptions Flags,
62266226
if (!Status.isError()) {
62276227
if (Flags.contains(PD_InProtocol)) {
62286228
diagnose(Tok, diag::expected_lbrace_subscript_protocol)
6229-
.fixItInsertAfter(ElementTy.get()->getEndLoc(), " { get set }");
6229+
.fixItInsertAfter(ElementTy.get()->getEndLoc(), " { get <#set#> }");
62306230
} else {
62316231
diagnose(Tok, diag::expected_lbrace_subscript);
62326232
}

lib/Sema/CodeSynthesis.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,11 +2023,21 @@ void swift::maybeAddAccessorsToStorage(TypeChecker &TC,
20232023
} else if (isa<ProtocolDecl>(dc)) {
20242024
if (storage->hasStorage()) {
20252025
auto var = cast<VarDecl>(storage);
2026-
if (var->isLet())
2026+
2027+
if (var->isLet()) {
20272028
TC.diagnose(var->getLoc(),
2028-
diag::protocol_property_must_be_computed_var);
2029-
else
2030-
TC.diagnose(var->getLoc(), diag::protocol_property_must_be_computed);
2029+
diag::protocol_property_must_be_computed_var)
2030+
.fixItReplace(var->getParentPatternBinding()->getLoc(), "var")
2031+
.fixItInsertAfter(var->getTypeLoc().getLoc(), " { get }");
2032+
} else {
2033+
auto diag = TC.diagnose(var->getLoc(), diag::protocol_property_must_be_computed);
2034+
auto braces = var->getBracesRange();
2035+
2036+
if (braces.isValid())
2037+
diag.fixItReplace(braces, "{ get <#set#> }");
2038+
else
2039+
diag.fixItInsertAfter(var->getTypeLoc().getLoc(), " { get <#set#> }");
2040+
}
20312041
}
20322042

20332043
setProtocolStorageImpl(TC, storage);

test/decl/protocol/protocols.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ protocol Test {
1414
var creator: String { get }
1515
var major : Int { get }
1616
var minor : Int { get }
17-
var subminor : Int // expected-error {{property in protocol must have explicit { get } or { get set } specifier}}
18-
static var staticProperty: Int // expected-error{{property in protocol must have explicit { get } or { get set } specifier}}
17+
var subminor : Int // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{21-21= { get <#set#> \}}}
18+
static var staticProperty: Int // expected-error{{property in protocol must have explicit { get } or { get set } specifier}} {{33-33= { get <#set#> \}}}
19+
20+
let bugfix // expected-error {{type annotation missing in pattern}} expected-error {{immutable property requirement must be declared as 'var' with a '{ get }' specifier}}
21+
var comment // expected-error {{type annotation missing in pattern}} expected-error {{property in protocol must have explicit { get } or { get set } specifier}}
1922
}
2023

2124
protocol Test2 {
2225
var property: Int { get }
2326

24-
var title: String = "The Art of War" { get } // expected-error{{initial value is not allowed here}} expected-error {{property in protocol must have explicit { get } or { get set } specifier}}
25-
static var title2: String = "The Art of War" // expected-error{{initial value is not allowed here}} expected-error {{property in protocol must have explicit { get } or { get set } specifier}}
27+
var title: String = "The Art of War" { get } // expected-error{{initial value is not allowed here}} expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{20-20= { get <#set#> \}}}
28+
static var title2: String = "The Art of War" // expected-error{{initial value is not allowed here}} expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{28-28= { get <#set#> \}}}
2629

2730
associatedtype mytype
2831
associatedtype mybadtype = Int
@@ -454,7 +457,7 @@ protocol ShouldntCrash {
454457
let fullName: String { get } // expected-error {{'let' declarations cannot be computed properties}} {{3-6=var}}
455458

456459
// <rdar://problem/17200672> Let in protocol causes unclear errors and crashes
457-
let fullName2: String // expected-error {{immutable property requirement must be declared as 'var' with a '{ get }' specifier}}
460+
let fullName2: String // expected-error {{immutable property requirement must be declared as 'var' with a '{ get }' specifier}} {{3-6=var}} {{24-24= { get \}}}
458461

459462
// <rdar://problem/16789886> Assert on protocol property requirement without a type
460463
var propertyWithoutType { get } // expected-error {{type annotation missing in pattern}}
@@ -500,7 +503,7 @@ class C4 : P4 { // expected-error {{type 'C4' does not conform to protocol 'P4'}
500503
// <rdar://problem/25185722> Crash with invalid 'let' property in protocol
501504
protocol LetThereBeCrash {
502505
let x: Int
503-
// expected-error@-1 {{immutable property requirement must be declared as 'var' with a '{ get }' specifier}}
506+
// expected-error@-1 {{immutable property requirement must be declared as 'var' with a '{ get }' specifier}} {{13-13= { get \}}}
504507
// expected-note@-2 {{declared here}}
505508
}
506509

test/decl/subscript/subscripting.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ class Foo {
346346
protocol r23952125 {
347347
associatedtype ItemType
348348
var count: Int { get }
349-
subscript(index: Int) -> ItemType // expected-error {{subscript in protocol must have explicit { get } or { get set } specifier}} {{36-36= { get set \}}}
350-
351-
var c : Int // expected-error {{property in protocol must have explicit { get } or { get set } specifier}}
349+
subscript(index: Int) -> ItemType // expected-error {{subscript in protocol must have explicit { get } or { get set } specifier}} {{36-36= { get <#set#> \}}}
350+
351+
var c : Int // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{14-14= { get <#set#> \}}}
352352
}
353353

354354
// <rdar://problem/16812341> QoI: Poor error message when providing a default value for a subscript parameter

test/decl/var/lazy_properties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct S {
1010

1111
protocol SomeProtocol {
1212
lazy var x : Int // expected-error {{'lazy' isn't allowed on a protocol requirement}} {{3-8=}}
13-
// expected-error@-1 {{property in protocol must have explicit { get } or { get set } specifier}}
13+
// expected-error@-1 {{property in protocol must have explicit { get } or { get set } specifier}} {{19-19= { get <#set#> \}}}
1414
// expected-error@-2 {{lazy properties must have an initializer}}
1515
lazy var y : Int { get } // expected-error {{'lazy' isn't allowed on a protocol requirement}} {{3-8=}}
1616
// expected-error@-1 {{'lazy' must not be used on a computed property}}

test/decl/var/properties.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -850,10 +850,10 @@ struct WillSetDidSetDisambiguate3 {
850850
}
851851

852852
protocol ProtocolGetSet1 {
853-
var a: Int // expected-error {{property in protocol must have explicit { get } or { get set } specifier}}
853+
var a: Int // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{13-13= { get <#set#> \}}}
854854
}
855855
protocol ProtocolGetSet2 {
856-
var a: Int {} // expected-error {{property in protocol must have explicit { get } or { get set } specifier}}
856+
var a: Int {} // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{14-16={ get <#set#> \}}}
857857
}
858858
protocol ProtocolGetSet3 {
859859
var a: Int { get }
@@ -869,16 +869,20 @@ protocol ProtocolGetSet6 {
869869
}
870870

871871
protocol ProtocolWillSetDidSet1 {
872-
var a: Int { willSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} expected-error {{expected get or set in a protocol property}}
872+
var a: Int { willSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{14-25={ get <#set#> \}}} expected-error {{expected get or set in a protocol property}}
873873
}
874874
protocol ProtocolWillSetDidSet2 {
875-
var a: Int { didSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} expected-error {{expected get or set in a protocol property}}
875+
var a: Int { didSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{14-24={ get <#set#> \}}} expected-error {{expected get or set in a protocol property}}
876876
}
877877
protocol ProtocolWillSetDidSet3 {
878-
var a: Int { willSet didSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} expected-error 2 {{expected get or set in a protocol property}}
878+
var a: Int { willSet didSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{14-32={ get <#set#> \}}} expected-error 2 {{expected get or set in a protocol property}}
879+
879880
}
880881
protocol ProtocolWillSetDidSet4 {
881-
var a: Int { didSet willSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} expected-error 2 {{expected get or set in a protocol property}}
882+
var a: Int { didSet willSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{14-32={ get <#set#> \}}} expected-error 2 {{expected get or set in a protocol property}}
883+
}
884+
protocol ProtocolWillSetDidSet5 {
885+
let a: Int { didSet willSet } // expected-error {{property in protocol must have explicit { get } or { get set } specifier}} {{14-32={ get <#set#> \}}} {{none}} expected-error 2 {{expected get or set in a protocol property}} expected-error {{'let' declarations cannot be computed properties}} {{3-6=var}}
882886
}
883887

884888
var globalDidsetWillSet: Int { // expected-error {{non-member observing properties require an initializer}}

0 commit comments

Comments
 (0)