Skip to content

Commit a473281

Browse files
authored
Merge pull request #64520 from angela-laar/duplicate-raw-value-crash
[Sema] Compiler should diagnose non-unique raw values without crashing
2 parents d20b8d2 + 4308a14 commit a473281

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,9 +1343,7 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
13431343

13441344
// Diagnose the duplicate value.
13451345
Diags.diagnose(diagLoc, diag::enum_raw_value_not_unique);
1346-
assert(lastExplicitValueElt &&
1347-
"should not be able to have non-unique raw values when "
1348-
"relying on autoincrement");
1346+
13491347
if (lastExplicitValueElt != elt &&
13501348
valueKind == AutomaticEnumValueKind::Integer) {
13511349
Diags.diagnose(uncheckedRawValueOf(lastExplicitValueElt)->getLoc(),
@@ -1357,6 +1355,7 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
13571355
diagLoc = uncheckedRawValueOf(foundElt)->isImplicit()
13581356
? foundElt->getLoc() : uncheckedRawValueOf(foundElt)->getLoc();
13591357
Diags.diagnose(diagLoc, diag::enum_raw_value_used_here);
1358+
13601359
if (foundElt != prevSource.lastExplicitValueElt &&
13611360
valueKind == AutomaticEnumValueKind::Integer) {
13621361
if (prevSource.lastExplicitValueElt)

test/Parse/enum.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,25 @@ enum DuplicateMembers7 : String { // expected-error {{'DuplicateMembers7' declar
364364
case Foo = "Bar" // expected-error {{invalid redeclaration of 'Foo'}}
365365
}
366366

367+
enum DuplicateMembers8 : String { // expected-error {{'DuplicateMembers8' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}}
368+
case Foo // expected-note {{'Foo' previously declared here}}
369+
// expected-note@-1 {{raw value previously used here}}
370+
case Foo // expected-error {{invalid redeclaration of 'Foo'}}
371+
// expected-error@-1 {{raw value for enum case is not unique}}
372+
}
373+
374+
enum DuplicateMembers9 : String { // expected-error {{'DuplicateMembers9' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}}
375+
case Foo = "Foo" // expected-note {{'Foo' previously declared here}}
376+
// expected-note@-1 {{raw value previously used here}}
377+
case Foo = "Foo"// expected-error {{invalid redeclaration of 'Foo'}}
378+
// expected-error@-1 {{raw value for enum case is not unique}}
379+
}
380+
381+
enum DuplicateMembers10 : String {
382+
case Foo // expected-note {{raw value previously used here}}
383+
case Bar = "Foo" // expected-error {{raw value for enum case is not unique}}
384+
}
385+
367386
// Refs to duplicated enum cases shouldn't crash the compiler.
368387
// rdar://problem/20922401
369388
func check20922401() -> String {

0 commit comments

Comments
 (0)