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

Validate uses of self and super #4246

Merged
merged 1 commit into from
May 1, 2020
Merged

Conversation

djrenren
Copy link
Contributor

@djrenren djrenren commented May 1, 2020

This change follows on the validation of the crate keyword in paths. It verifies the following things:

super:

  • May only be preceded by other super segments
  • If in a UseItem then all semantically preceding paths also consist only of super

self

  • May only be the start of a path

Just a note, a couple times while working on this I found myself really wanting a Visitor of some sort so that I could traverse descendants while skipping sub-trees that are unimportant. Iterators don't really work for this, so as you can see I reached for recursion. Considering paths are generally small a fancy debounced visitor probably isn't important but figured I'd say something in case we had something like this lying around and I wasn't using it.

@matklad
Copy link
Member

matklad commented May 1, 2020

Yeah, I somehow don't really like visitors. Like, you are supposed to use visitors in compilers, but, every time I did one, I reverted back to traversal and matching.

I think in this case we might want to preserve the caller-driven API of descendants, but allow some control over traversal. Not sure exactly how the API should look like, but I imagine something like this might be possible:

let traversal = Traversal::new(root);
let mut cntl = traversal.cntl();

for node in traversal.iter() {
    if cond {
        cntl.skip_children()
    }
}

the cntl think would hold a &Cell<bool>, which will be stored inside Traversal and checked on next. Not sure if it's a good idea or too complex, but I'd try this API over a visitor.

bors r+

@djrenren
Copy link
Contributor Author

djrenren commented May 1, 2020

Yeah I really like the iterator-style as well. I'd be happy if I never had to write a rustc style visitor again. I'll see if I can hack something like this up because it would be so nice.

@bors
Copy link
Contributor

bors bot commented May 1, 2020

@bors bors bot merged commit 21588e1 into rust-lang:master May 1, 2020
@matklad
Copy link
Member

matklad commented May 1, 2020

The source for the current traversal is here: https://github.com/rust-analyzer/rowan/blob/40fe55c450c88f4da7cf315bf027bec3250a7352/src/cursor.rs#L389-L412

We should lift the traversal to rowan eventually, but it makes sense to start experimenting with it here

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