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

Avoid name clashes with field names #15

Merged
merged 9 commits into from
Jan 9, 2024

Conversation

ijackson
Copy link
Contributor

@ijackson ijackson commented Jan 8, 2024

Hi. This is an alternative approach to #14, which I think fixes the problem definitively.

After 983a143 we still have name clashes if enum fields are things like v_builder_. That's a slightly odd field name, but I don't think it's reasonable to demand that a user of educe avoid it.

In this branch, I:

  • revert the code changes in 983a143
  • fix the implementations of Debug Hash Clone and the comparison traits for enums
  • provide a test case, which fails with 983a143, and also fails with 983a143~, and passes at the tip of the branch

To fix the enum impls I generally:

  • use format_ident! to generate derived names like _FIELD.
  • used the names field_name_real and field_name_var for the actual name, and the local variable name (pattern binding), respectively (rather than, say field_name and field_name2).
  • where the existing impl already had field_name2 because it needs two lots of field names in scope, used two format_ident! calls to make two lots of field_name_var_...
  • sometimes, renamed some variables to give them more descriptive names (e.g. rather than just with and without a 2 suffix)

I looked at the code for structs, and as far as I can see it doesn't bind the struct fields to local variable names, so it's not affected by issues analogous to #14. I also looked at the implementations of Default, Deref[Mut] and Into. These don't have any local variables, so although they use the struct field names directly as local variables, they avoid #14. I think it might be worth considering derived identifiers for these cases too, in case we should ever introduce local variables into the generated code for these - right now doing so would introduce the #14 bug again. But I'm not sure now is the right time to do that.

Please let me know what you think.

This partially reverts commit 983a143.

We retain the version number change.
Fixes magiclen#14 completely, so there is now no risk of clashes between our
different local variables, even though some of them are constructed
from field names.
This is a local variable name, not the field name (since the fields
are numbered, their "names" are the indices).
Fixes the analogous bug to magiclen#14 with fields called `state`.
This prevents name clashes with `source`.  Because of the way
clone_from works, we need to be able to bind both source and
destination field names at once.

I chose to rename some variables to reflect their purpose, rather than
relying on `2` suffixes.
@magiclen
Copy link
Owner

magiclen commented Jan 9, 2024

Thank you for providing this alternative approach to address the issues raised in #14. I appreciate the thorough explanation of the changes made in this branch.

I've reviewed the changes you made, including the reversion of code changes in 983a143, the adjustments to implementations of Debug, Hash, Clone, PartialEq, PartialOrd and Ord for enums, and the introduction of format_ident! for generating derived names.

It seems like you've taken a careful approach to avoid name clashes and have provided a comprehensive test case to demonstrate the effectiveness of the proposed solution. I also appreciate your consideration of potential issues with struct fields and the implementations of Default, Deref, DerefMut, and Into.

Regarding the suggestion to consider derived identifiers for cases involving local variables in struct fields and the mentioned traits, I agree that it's worth exploring to prevent potential future issues.

Overall, the changes look promising, and I'd be in favor of merging this alternative approach.

@magiclen magiclen merged commit 2b05f98 into magiclen:master Jan 9, 2024
58 of 155 checks passed
@ijackson ijackson deleted the ticket-14-redux branch February 26, 2024 17:55
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.

2 participants