Skip to content
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

Error for duplicate enum values #7872

Open
jez opened this issue May 3, 2024 · 0 comments
Open

Error for duplicate enum values #7872

jez opened this issue May 3, 2024 · 0 comments
Labels
enhancement New feature or surprising current feature good first issue Good for newcomers

Comments

@jez
Copy link
Collaborator

jez commented May 3, 2024

Input

→ View on sorbet.run

# typed: strict
extend T::Sig

class MyEnum < T::Enum
  enums do
    Foo = new
    Bar = new
    Foo = new
  end
end

Observed output

No errors! Great job.

Expected behavior

Sorbet does not in general report constant redefinition errors, because it does not track code loading order:

https://sorbet.org/docs/unsupported#tracking-code-loading-order

There are a couple limited forms of this problem that Sorbet could nonetheless solve:

  1. Warn for duplicate enum values (potentially encoding sorbet-runtime's serialized_val computation to know that Foo1 = new; Foo2 = new('Foo1') is an error as well
  2. Warn for re-assigned constants if the redefinition happens within a single file

Either of these would help bring another class of runtime errors into the static checks.


This becomes especially useful the larger the enum is, because the two duplicates might be at arbitrary spots in the list of values.

@jez jez added bug Something isn't working enhancement New feature or surprising current feature and removed bug Something isn't working labels May 3, 2024
@jez jez changed the title Warn for duplicate enum values Error for duplicate enum values May 3, 2024
@jez jez added the good first issue Good for newcomers label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or surprising current feature good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant