Skip to content

[Sema] Compiler should diagnose non-unique raw values without crashing #64520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1342,9 +1342,7 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,

// Diagnose the duplicate value.
Diags.diagnose(diagLoc, diag::enum_raw_value_not_unique);
assert(lastExplicitValueElt &&
"should not be able to have non-unique raw values when "
"relying on autoincrement");

if (lastExplicitValueElt != elt &&
valueKind == AutomaticEnumValueKind::Integer) {
Diags.diagnose(uncheckedRawValueOf(lastExplicitValueElt)->getLoc(),
Expand All @@ -1356,6 +1354,7 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
diagLoc = uncheckedRawValueOf(foundElt)->isImplicit()
? foundElt->getLoc() : uncheckedRawValueOf(foundElt)->getLoc();
Diags.diagnose(diagLoc, diag::enum_raw_value_used_here);

if (foundElt != prevSource.lastExplicitValueElt &&
valueKind == AutomaticEnumValueKind::Integer) {
if (prevSource.lastExplicitValueElt)
Expand Down
19 changes: 19 additions & 0 deletions test/Parse/enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,25 @@ enum DuplicateMembers7 : String { // expected-error {{'DuplicateMembers7' declar
case Foo = "Bar" // expected-error {{invalid redeclaration of 'Foo'}}
}

enum DuplicateMembers8 : String { // expected-error {{'DuplicateMembers8' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}}
case Foo // expected-note {{'Foo' previously declared here}}
// expected-note@-1 {{raw value previously used here}}
case Foo // expected-error {{invalid redeclaration of 'Foo'}}
// expected-error@-1 {{raw value for enum case is not unique}}
}

enum DuplicateMembers9 : String { // expected-error {{'DuplicateMembers9' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}}
case Foo = "Foo" // expected-note {{'Foo' previously declared here}}
// expected-note@-1 {{raw value previously used here}}
case Foo = "Foo"// expected-error {{invalid redeclaration of 'Foo'}}
// expected-error@-1 {{raw value for enum case is not unique}}
}

enum DuplicateMembers10 : String {
case Foo // expected-note {{raw value previously used here}}
case Bar = "Foo" // expected-error {{raw value for enum case is not unique}}
}

// Refs to duplicated enum cases shouldn't crash the compiler.
// rdar://problem/20922401
func check20922401() -> String {
Expand Down