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

Adding binding rules to typeof(<enum>).min and max, and to <enum>.<member> so they work with extensions #1177

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

beta-ziliani
Copy link
Contributor

@beta-ziliani beta-ziliani commented Dec 2, 2024

Solves #1158.
Solves #1196.

Considerations:

  • It's built on top of Solidity binding fixes driven by Sanctuary #1149 It's rebased on master.
  • It's currently missing a test for max
  • I adapted an existing test, though maybe it's preferred to add a different one?
  • I use the same test for min and max, I think it makes sense to have them together. I'm not so sure where to put it though. It's testing these functions, but also in combination with using for, so it can very well be moved to the using folder.

EDITED

@beta-ziliani beta-ziliani requested review from a team as code owners December 2, 2024 17:04
Copy link

changeset-bot bot commented Dec 2, 2024

⚠️ No Changeset found

Latest commit: 1b73b89

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@OmarTawfik OmarTawfik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs rebasing on latest main.

cc @ggiraldez if you don't mind a quick review?

@OmarTawfik OmarTawfik requested a review from ggiraldez December 3, 2024 09:36
@beta-ziliani
Copy link
Contributor Author

I suppose it's best to wait for #1149 to be in sync with main, and then rebase this one on top of it

Copy link
Contributor

@ggiraldez ggiraldez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple of comments that need addressing. Please ping me if you have questions.

@@ -1,7 +1,17 @@
enum Answer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be preferable to have a separate test for this, and make it as small as possible. Another suggestion is to use a library for extending the type, as that approach is compatible with all versions of Solidity.

Comment on lines 2155 to 2161
edge type -> typeof
edge typeof -> built_in_member
edge built_in_member -> min_built_in
edge built_in_member -> max_built_in
edge min_built_in -> min_max_accessor
edge max_built_in -> min_max_accessor
edge min_max_accessor -> @enum.lexical_scope
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct. The trailing . in the path (the min_max_accessor node) allows binding min. with anything in the lexical scope, which is why the test case resolves correctly. But it would also bind to any symbol at that scope.

Keep in mind you should "replace" (as in the symbol stack, pop and then push) min and max with a @typeof -> X, where X is the argument to type() so that you can continue the binding resolution at X.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is a left over from #1196, but it doesn't harm to have it here

@beta-ziliani beta-ziliani changed the title Adding binding rules so typeof(enum).min and typeof(enum).max resolve to the enum's scope Adding binding rules to typeof(<enum>).min and max, and to <enum>.<member> so they work with extensions Dec 20, 2024
Copy link
Contributor

@ggiraldez ggiraldez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of suggestions.

Comment on lines +2075 to +2078
node minmax_type_of
attr (minmax_type_of) push_symbol = "@typeof"
node minmax_replace
attr (minmax_replace) push_symbol = (source-text @name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest extracting this path pushing @typeof -> ENUM_NAME and re-using it in each member rules below as well. Something like @enum.value_ref_typeof. This would be a bit simpler and avoid creating extra nodes for each of the enum elements.

Also not too keen on the _replace suffix. Maybe value_ref_typeof and value_ref_type since both nodes form a reference path to the type of a value of the enum.

Comment on lines +2069 to +2074
node built_in_member
attr (built_in_member) pop_symbol = "."
node min_built_in
attr (min_built_in) pop_symbol = "min"
node max_built_in
attr (max_built_in) pop_symbol = "max"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both min and max only exist in Solidity >= 0.6.8. It's not critical, but to be consistent with the built-in definitions these should be only added for those versions.

Comment on lines +2097 to +2103
node type_of
attr (type_of) push_symbol = "@typeof"
node type_def
attr (type_def) push_symbol = (source-text @name)
edge def -> type_of
edge type_of -> type_def
edge type_def -> @enum.lexical_scope
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here instead of repeating the path segment you can use the aforementioned path and do only add:

edge def -> @enum.value_ref_typeof`

You'll not need to capture the enum's name either.

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.

3 participants