Skip to content

Commit ee4f9b2

Browse files
committed
[Sema] Add fix-it for property in protocol
https://bugs.swift.org/browse/SR-8340
1 parent 67bf1a9 commit ee4f9b2

File tree

5 files changed

+37
-18
lines changed

5 files changed

+37
-18
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,11 +2023,27 @@ 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())
2027-
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);
2026+
auto braces = var->getBracesRange();
2027+
2028+
if (var->isLet()) {
2029+
auto diag = TC.diagnose(var->getLoc(),
2030+
diag::protocol_property_must_be_computed_var);
2031+
if (braces.isValid())
2032+
diag
2033+
.fixItReplace(var->getParentPatternBinding()->getLoc(), "var")
2034+
.fixItReplace(braces, " { get }");
2035+
else
2036+
diag
2037+
.fixItReplace(var->getParentPatternBinding()->getLoc(), "var")
2038+
.fixItInsertAfter(var->getTypeLoc().getLoc(), " { get }");
2039+
} else {
2040+
auto diag = TC.diagnose(var->getLoc(), diag::protocol_property_must_be_computed);
2041+
2042+
if (braces.isValid())
2043+
diag.fixItReplace(braces, " { get <#set#> }");
2044+
else
2045+
diag.fixItInsertAfter(var->getTypeLoc().getLoc(), " { get <#set#> }");
2046+
}
20312047
}
20322048

20332049
setProtocolStorageImpl(TC, storage);

test/decl/protocol/protocols.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ 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#> \}}}
1919
}
2020

2121
protocol Test2 {
2222
var property: Int { get }
2323

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}}
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}} {{20-20= { get <#set#> \}}}
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}} {{28-28= { get <#set#> \}}}
2626

2727
associatedtype mytype
2828
associatedtype mybadtype = Int
@@ -454,7 +454,7 @@ protocol ShouldntCrash {
454454
let fullName: String { get } // expected-error {{'let' declarations cannot be computed properties}} {{3-6=var}}
455455

456456
// <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}}
457+
let fullName2: String // expected-error {{immutable property requirement must be declared as 'var' with a '{ get }' specifier}} {{24-24= { get \}}}
458458

459459
// <rdar://problem/16789886> Assert on protocol property requirement without a type
460460
var propertyWithoutType { get } // expected-error {{type annotation missing in pattern}}
@@ -502,6 +502,7 @@ protocol LetThereBeCrash {
502502
let x: Int
503503
// expected-error@-1 {{immutable property requirement must be declared as 'var' with a '{ get }' specifier}}
504504
// expected-note@-2 {{declared here}}
505+
// {{13-13= { get \}}}
505506
}
506507

507508
extension LetThereBeCrash {

test/decl/subscript/subscripting.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ protocol r23952125 {
347347
associatedtype ItemType
348348
var count: Int { get }
349349
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}}
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ protocol SomeProtocol {
1212
lazy var x : Int // expected-error {{'lazy' isn't allowed on a protocol requirement}} {{3-8=}}
1313
// expected-error@-1 {{property in protocol must have explicit { get } or { get set } specifier}}
1414
// expected-error@-2 {{lazy properties must have an initializer}}
15+
// {{18-18= { get set \}}}
1516
lazy var y : Int { get } // expected-error {{'lazy' isn't allowed on a protocol requirement}} {{3-8=}}
1617
// expected-error@-1 {{'lazy' must not be used on a computed property}}
1718
// expected-error@-2 {{lazy properties must have an initializer}}

test/decl/var/properties.swift

Lines changed: 7 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,17 @@ 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}}
882883
}
883884

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

0 commit comments

Comments
 (0)