Skip to content

Commit 535364c

Browse files
committed
[Typechecker] We could also have a subscript when diagnosing SettableConflict, so guard against that
1 parent 4c8450e commit 535364c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,10 +2116,11 @@ diagnoseMatch(ModuleDecl *module, NormalProtocolConformance *conformance,
21162116
auto witness = match.Witness;
21172117
auto diag =
21182118
diags.diagnose(witness, diag::protocol_witness_settable_conflict);
2119-
auto VD = cast<VarDecl>(witness);
2120-
if (VD->hasStorage()) {
2121-
auto PBD = VD->getParentPatternBinding();
2122-
diag.fixItReplace(PBD->getStartLoc(), getTokenText(tok::kw_var));
2119+
if (auto VD = dyn_cast<VarDecl>(witness)) {
2120+
if (VD->hasStorage()) {
2121+
auto PBD = VD->getParentPatternBinding();
2122+
diag.fixItReplace(PBD->getStartLoc(), getTokenText(tok::kw_var));
2123+
}
21232124
}
21242125
break;
21252126
}
@@ -2166,7 +2167,7 @@ diagnoseMatch(ModuleDecl *module, NormalProtocolConformance *conformance,
21662167
case MatchKind::RethrowsConflict: {
21672168
auto witness = match.Witness;
21682169
auto diag =
2169-
diags.diagnose(match.Witness, diag::protocol_witness_rethrows_conflict);
2170+
diags.diagnose(witness, diag::protocol_witness_rethrows_conflict);
21702171
auto FD = cast<FuncDecl>(witness);
21712172
diag.fixItReplace(FD->getThrowsLoc(), getTokenText(tok::kw_rethrows));
21722173
break;

test/decl/protocol/req/witness_fix_its.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ struct ConformsToFoo: Foo { // expected-error {{type 'ConformsToFoo' does not co
2626
var bar5: Int { return 0 } // expected-note {{candidate is not settable, but protocol requires it}}{{none}}
2727
subscript(_ pos: Int) -> Int { return 0 } // expected-note {{candidate operates on an instance, not a type as required}}{{3-3=static}}
2828
}
29+
30+
protocol Foo1 {
31+
subscript(value: Bool) -> Bool { get set } // expected-note {{protocol requires subscript with type '(Bool) -> Bool'; do you want to add a stub?}}
32+
}
33+
34+
struct ConformsToFoo1: Foo1 { // expected-error {{type 'ConformsToFoo1' does not conform to protocol 'Foo1'}}
35+
subscript(value: Bool) -> Bool { return false } // expected-note {{candidate is not settable, but protocol requires it}}{{none}}
36+
}

0 commit comments

Comments
 (0)