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

SourceRef in expressions #1357

Merged
merged 26 commits into from
May 26, 2024
Merged

Conversation

gzanitti
Copy link
Contributor

@gzanitti gzanitti commented May 7, 2024

This PR solves issue #1293.

Includes PRs that modify some Expression in order to match fields. It is recommended to review/merge those first:

All comments are welcome :)

@gzanitti gzanitti marked this pull request as ready for review May 7, 2024 03:14
@lvella lvella marked this pull request as draft May 7, 2024 10:43
@lvella
Copy link
Member

lvella commented May 7, 2024

Let's merge the dependency PRs before undrafting this one.

@chriseth
Copy link
Member

chriseth commented May 10, 2024

I think once the children function for Expression uses a simple match, we can implement #1169, i.e. remove the Box (and probably also on the whole trait)

ast/src/parsed/mod.rs Outdated Show resolved Hide resolved
ast/src/parsed/mod.rs Outdated Show resolved Hide resolved
ast/src/parsed/mod.rs Outdated Show resolved Hide resolved
parser/src/powdr.lalrpop Outdated Show resolved Hide resolved
parser/src/lib.rs Outdated Show resolved Hide resolved
@gzanitti gzanitti marked this pull request as ready for review May 23, 2024 15:53
@gzanitti gzanitti requested a review from chriseth May 23, 2024 16:25
ast/src/parsed/display.rs Outdated Show resolved Hide resolved
ast/src/parsed/display.rs Outdated Show resolved Hide resolved
ast/src/parsed/mod.rs Outdated Show resolved Hide resolved
(Expression::MatchExpression(_, a), Expression::MatchExpression(_, b)) => a == b,
(Expression::IfExpression(_, a), Expression::IfExpression(_, b)) => a == b,
(Expression::BlockExpression(_, a), Expression::BlockExpression(_, b)) => a == b,
_ => false,
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be quite dangerous. If we add something to Expression, we will probably not extend this implementation.

Is there a different way of doing this? We probably don't want to clone and set the source ref, but maybe some simple macro?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up implementing all the comparison inside a macro and using all the variants to make the match exhaustive (without using wildcards), in case we add a new variant to Expressions, the match will complain.

What do you think?

ast/src/parsed/mod.rs Outdated Show resolved Hide resolved
ast/src/parsed/mod.rs Outdated Show resolved Hide resolved
ast/src/parsed/mod.rs Outdated Show resolved Hide resolved
ast/src/parsed/mod.rs Outdated Show resolved Hide resolved
linker/src/lib.rs Outdated Show resolved Hide resolved
parser/src/test_utils.rs Outdated Show resolved Hide resolved
@gzanitti gzanitti requested a review from chriseth May 24, 2024 23:08
}
}
}

impl_source_reference!(
Copy link
Member

Choose a reason for hiding this comment

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

I trink this is good for now, but i think in general, it would be better to have a macro that allows “iterating” over the elements of Expression.

I.e. it would be much better readable in my opinion if the point of macro call would look like the current macro definition and the macro definition would list the enum variants instead (maybe we can even get that information without listing the names? I saw something like that in some recent code by @leonardoalt ). Then we could reuse the same macro to define the children or even display function of expression

$(
(Expression::$variant(_, a), Expression::$variant(_, b)) => a == b,
)*
// This catches the case where variants are different and returns false
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

}
}
}

impl_partial_eq_for_expression!(
Copy link
Member

Choose a reason for hiding this comment

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

I think the macro I mentioned could be used here as well

@chriseth chriseth added this pull request to the merge queue May 26, 2024
Merged via the queue into powdr-labs:main with commit 0d9669e May 26, 2024
6 checks passed
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.

None yet

3 participants