Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 constraints for unnamed enums #3884
base: main
Are you sure you want to change the base?
Enforce constraints for unnamed enums #3884
Changes from 4 commits
651538a
6deae2f
bfdf606
e410ad8
029a6e3
042fe5c
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
There's no test for these client changes? Also bot posts no codegen diff, which is concerning since it'd mean we're not exercising this.
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.
Good point. Let me add a test for this.
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.
Ah, now I remember—there’s already a test that verifies
From<&str>
andFromStr
are implemented for unnamed enums.This test for unnamed enums generates the following:
The
.parse
method calls theFromStr
implementation, which in turn callsFrom<&str>
.I’ll also update the test to ensure no validation occurs on the client side.
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.
Huh, I wonder why we have this class at all i.e. why we don't test directly against
InfallibleEnumType
. It feels wrong to copy over the implementations from the "real" classes to this class.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.
I agree with you on this. I'll see if it can be easily fixed in this PR, otherwise will raise another one for this.
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.
InfallibleEnumType
allowsUnknownVariants
, whereasTestEnumType
panics when an unknown variant is encountered. I tried composingTestEnumType
to useInfallibleEnumType
internally, butInfallibleEnumType
is defined incodegen-client
, whileTestEnumType
is incodegen-core
.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 is first converting to a heap-allocated
String
and only then matching on the enum values. So invalid enum values would unnecessarily be heap-allocated. We should makeTryFrom<String>
andTryFrom<&str>
instead delegate toFromStr
, which should only heap-allocate when the enum value is valid.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.
Unlike named enums, which can use
&str
for comparison and directly return an enum variant, unnamed enums need to store an owned, heap-allocatedString
. TheTryFrom<String>
implementation already receives an ownedString
as a parameter, so delegating toFromStr
would result in an unnecessary heap allocation. Additionally,ConstraintViolation
takes ownership of a heap-allocatedString
. Therefore, both code paths (valid and invalid enum values) require a heap-allocatedString
. Callingto_owned
withinTryFrom<&str>
only shifts the allocation slightly earlier without adding any additional allocation.