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

Conversation

angela-laar
Copy link
Contributor

@angela-laar angela-laar commented Mar 21, 2023

Enums with raw type : String use the case name as the raw value if the case is undefined. So two identical case names causes a "raw value for enum case is not unique" error. Additionally, identical case names will always cause a separate redeclaration error regardless of the declared enum raw type.

enum Foo : String {
    case Bar
    case Bar // two errors:  non-unique raw value error and invalid redeclaration of 'Bar'
    case Bar = "FooBar" // one error: invalid redeclaration of 'Bar' because raw value is FooBar instead of Bar
}

This second error gets diagnosed in CheckRedeclarationRequest in the DeclVisitor. We could add a special case for this situation to ignore the raw value error and defer diagnosing to the DeclVisitor since fixing that error is most correct. For now, I'll fix the crash and let the compiler diagnose both errors.

Fixes rdar://105761206
Fixes #63829

@angela-laar
Copy link
Contributor Author

@swift-ci please smoke test

@angela-laar angela-laar requested review from ahoppen and removed request for rintaro, DougGregor, bnbarham, CodaFi and ahoppen March 21, 2023 22:18
Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having both errors is fine since there really are two errors: you're redeclaring a case with the same name, and the implicit raw value is also a duplicate

Enums with raw type `: String ` use the case name as the raw
value. So, two identical case names cause an identical raw value
error. Also, identical case names cause a separate redeclaration
error. The second error is diagnosed in a separate request in the
DeclVisitor. We may need a special case for this situation to
ignore the first error and defer diagnosing to the
CheckRedeclarationRequest. For now we will diagnose both errors.

`enum Foo : String {
    case Bar
    case Bar = "FooBar" // redclaration error only
    case Bar // redeclaration and raw value error
}`
@angela-laar angela-laar force-pushed the duplicate-raw-value-crash branch from 8d51c26 to 4308a14 Compare March 21, 2023 23:02
@angela-laar
Copy link
Contributor Author

@swift-ci please smoke test

@angela-laar
Copy link
Contributor Author

@swift-ci please smoke test macOS platform

@ahoppen ahoppen removed their request for review March 22, 2023 21:21
@angela-laar angela-laar removed request for hborla and xedin March 27, 2023 17:31
@angela-laar
Copy link
Contributor Author

@swift-ci please smoke test macOS platform

@hborla
Copy link
Member

hborla commented Apr 19, 2023

@swift-ci please smoke test macOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typechecking crashes when enum contains a duplicated string raw value
3 participants