-
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
Implement marker traits #6871
base: master
Are you sure you want to change the base?
Implement marker traits #6871
Conversation
CodSpeed Performance ReportMerging #6871 will not alter performanceComparing Summary
|
I'm not sure we want to restrict ABI errors to be necessarily Enums, what's the rationale behind having to check this particular constraints in a trait impl instead of being more generic? |
@IGI-111 The ABI errors are not necessarily Enums, on the contrary. Currently, in this PR, they are implemented only for string slices. Perhaps is the example given in the description misleading. The idea of the example was to demonstrate the usage of marker traits in general - writing generic code that has constraints on different type properties.
But the question leads me to the questionable purpose of the Since the full-blown Once started playing with it, I realized that it actually fits nicely with the This might be a far stretched use case for a real world application, but as with all new language features, it is hard to anticipate how far devs will take it. I am fine with removing I'll adapt the PR description to better convey the choices done in this PR and to avoid misleading example. |
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.
All looks good except CallPath::to_string_with_args
, which I think needs to be tweaked.
Description
This PR introduces the concept of marker traits to the language. It is the first step towards implementing the ABI errors RFC.
Marker traits are traits automatically generated by the compiler. They represent certain properties of types and cannot be explicitly implemented by developers.
The PR implements a common infrastructure for generating and type-checking marker traits as well as two concrete marker traits:
Error
: represents a type whose instances can be used as arguments for thepanic
expression. (Thepanic
expression is yet to be implemented.) In this PR, it is implemented only for string slices. In the upcoming PR it will be implemented for enums annotated with the#[error_type]
attribute.Enum
: represents an enum type. It is inspired by the Rust's experimentalTuple
marker trait and used in this PR as a sample marker trait, to develop and test the marker trait autogeneration.Marker traits allow developers expression constraints related to certain properties of Sway types. As an illustration, if developers want to express a constraint such is "the error type must be an error enum", they can do it by combining the two marker traits:
Note that the generic name
Enum
is sometimes used in our tests to represent a dummy enum. In tests, it is almost always defined locally, and sometimes explicitly imported, so it will never clash with theEnum
marker trait. A single test in which the clash occurred was easily adapted by explicitly importing the dummyEnum
.The PR is the first step in implementing #6765.
Checklist
Breaking*
orNew Feature
labels where relevant.