-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add ast ident iterator #6100
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
Add ast ident iterator #6100
Conversation
r? @phansch (rust_highfive has picked a reviewer for you, use r? to override) |
Thanks for the PR! Unfortunately, I'm not too familiar with this part of the code, so maybe r? @flip1995 |
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.
So before reviewing all of this: IIUC the goal of this is to get an iterator over all ident
s in an expression. Is there a reason, that you need lazy evaluation here? Otherwise we could just use a Visitor
, which collects every Ident
in a Vec
and then just turns this Vec
into an iterator.
Downside is that you have to collect all Ident
s and have a worse performance if you often want to stop iterating after a few steps.
The advantage is, that you don't have to implement all of the expr/item/ty/... handling yourself, since it is already implemented by the Visitor
.
/// This is a convenience method to help with type inference. | ||
fn new_p(expr: &'expr P<Expr>) -> Self { | ||
Self::new(expr) | ||
} |
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.
Can you give an example. where this would be necessary. Using P<_>
in a type signature should not be possible, since it implements Deref
.
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.
Given exprs
is a &Vec<P<Expr>>
,
exprs.iter()
.flat_map(ExprIdentIter::new_p)
compiles, while using ExprIdentIter::new
does not:
exprs.iter()
.flat_map(ExprIdentIter::new)
IIRC this compiles:
exprs.iter()
.flat_map(|e| ExprIdentIter::new(e))
So ExprIdentIter::new_p
allows skipping writing the closure.
I'm willing to believe there might be a better way to do this. Maybe making ExprIdentIter::new
generic or something like that?
That is correct. And the main reason we want an is to create suggestions containing an expression with some of the
Hmm. Well, the part that actually creates the suggestion can stop early. But at least for the purposes of #6086, that iteration would be preceded by some form of full iteration over the expression in question anyway, so maybe that savings would be insignificant. O(n + n / 2) is still O(n). Given how much work handling all the possibilities myself has been so far, using a |
744e8fa
to
5ff2cd7
Compare
…-lint branch, since it has been ballooning in complexity.
…ny boxed Ident iterator
…t` and `TyKind::ImplTrait`
5ff2cd7
to
0da001f
Compare
Okay, I feel silly now for not using |
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.
Since the code is now that simple, I think you can squash it into one commit and include it in the other PR?
I'll do that. Thanks for all your help simplifying the code! |
While working on #6086 which is a PR meant to address #6039, I found that I needed a way to iterate through the identifiers in an expression. Implementing an
Ident
iterator has turned out to be sufficiently complicated that I thought it would be nicer for reviewers if those changes were in a separate PR. This is that PR.changelog: none