Skip to content

Commit 4351bd3

Browse files
committed
Use The Location of the Pattern Binding in Codable Fixit
The code here used to use the location of the nearest place to insert attributes, which makes no sense. Use the pattern binding's location instead to ensure that we actually replace the 'let' part of the pattern every time. rdar://69971194
1 parent abd4608 commit 4351bd3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,10 @@ deriveBodyDecodable_init(AbstractFunctionDecl *initDecl, void *) {
799799
diag::decodable_property_init_or_codingkeys_explicit,
800800
varDecl->getName());
801801
}
802-
varDecl->diagnose(diag::decodable_make_property_mutable)
803-
.fixItReplace(varDecl->getAttributeInsertionLoc(true), "var");
802+
if (auto *PBD = varDecl->getParentPatternBinding()) {
803+
varDecl->diagnose(diag::decodable_make_property_mutable)
804+
.fixItReplace(PBD->getLoc(), "var");
805+
}
804806

805807
continue;
806808
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %target-typecheck-verify-swift -verify-ignore-unknown
2+
3+
struct Foo : Codable {
4+
let x1: String = ""
5+
// expected-warning@-1 {{immutable property will not be decoded because it is declared with an initial value which cannot be overwritten}}
6+
// expected-note@-2 {{set the initial value via the initializer or explicitly define a CodingKeys enum including a 'x1' case to silence this warning}}
7+
// expected-note@-3 {{make the property mutable instead}}{{3-6=var}}
8+
9+
public let x2: String = ""
10+
// expected-warning@-1 {{immutable property will not be decoded because it is declared with an initial value which cannot be overwritten}}
11+
// expected-note@-2 {{set the initial value via the initializer or explicitly define a CodingKeys enum including a 'x2' case to silence this warning}}
12+
// expected-note@-3 {{make the property mutable instead}}{{10-13=var}}
13+
14+
internal let x3: String = ""
15+
// expected-warning@-1 {{immutable property will not be decoded because it is declared with an initial value which cannot be overwritten}}
16+
// expected-note@-2 {{set the initial value via the initializer or explicitly define a CodingKeys enum including a 'x3' case to silence this warning}}
17+
// expected-note@-3 {{make the property mutable instead}}{{12-15=var}}
18+
19+
fileprivate let x4: String = ""
20+
// expected-warning@-1 {{immutable property will not be decoded because it is declared with an initial value which cannot be overwritten}}
21+
// expected-note@-2 {{set the initial value via the initializer or explicitly define a CodingKeys enum including a 'x4' case to silence this warning}}
22+
// expected-note@-3 {{make the property mutable instead}}{{15-18=var}}
23+
24+
private let x5: String = ""
25+
// expected-warning@-1 {{immutable property will not be decoded because it is declared with an initial value which cannot be overwritten}}
26+
// expected-note@-2 {{set the initial value via the initializer or explicitly define a CodingKeys enum including a 'x5' case to silence this warning}}
27+
// expected-note@-3 {{make the property mutable instead}}{{11-14=var}}
28+
}

0 commit comments

Comments
 (0)