-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Enforce type annotations for constants #7054
base: master
Are you sure you want to change the base?
Enforce type annotations for constants #7054
Conversation
CodSpeed Performance ReportMerging #7054 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all looks great and probably would work, but since it's a breaking change, it needs to be behind a feature flag.
Instead of doing that, which is a pretty significant amount of work especially if you're not familiar with the infra for those, I think a workable alternative so we can get this merged is to maintain the current behavior but reliably produce a warning (not an error) when a type is missing.
We'll easily be able to turn the warning into an error at a later date.
Description
This PR introduces a breaking change that requires all const declarations to have a proper type annotation.
This has been implemented by throwing a new parser error when detecting a constant declaration that lacks proper type annotation, and changing the
ty_opt
member ofItemConst
to a non-optional type as it's no longer possible to omit it.The
ConvertParseTreeError::ConstantRequiresTypeAscription
has been removed as it's no longer allowed to omit the type of a const and relying on the initialization expression to deduce it.A new test has been added to make sure the parser errors when a const type is not explicitly stated.
The test raising the
ConstantRequiresTypeAscription
error has been modified to check for uninitialized associated const declarations instead.A regexp has been used to find all occurrences of incomplete const declarations and try to patch them all.
Some other parts of Sway may be affected such as eg. sway-by-example: https://github.com/FuelLabs/sway-by-example-lib/blob/main/examples/constants/src/main.sw#L7
Closes #5758
Checklist
Breaking*
orNew Feature
labels where relevant.