-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 the location of crate
in paths
#4178
Conversation
There's an alternate implementation that checks that the let segment = path.segment();
let first_element_as_token = segment.as_ref().and_then(|it| it.syntax().first_child_or_token()?.into_token());
let last_element_as_token = segment.as_ref().and_then(|it| it.syntax().last_child_or_token()?.into_token());
let start_of_path = path.qualifier().is_none();
// `crate` is allowed to be the start of the path, so we can't error if this is true
if start_of_path && first_element_as_token == last_element_as_token {
return
}
let child_tokens = segment
.iter()
.flat_map(|seg| seg.syntax().children_with_tokens())
.flat_map(|it| it.into_token());
// Beyond the first position, `crate` is never allowed to appear
for child in child_tokens {
if child.kind() == CRATE_KW {
errors.push(SyntaxError::new("`crate` keyword is only allowed as the first segment of a path", child.text_range()))
}
} but I wasn't sure what was preferable. |
Was thinking about this more last night, and this code is mainly weird because there's no accessor on |
Definitelly, this should work by adding
Not that I know for sure :D My gut feeling would probably try to push all diagnostics as early as possible, and, in this case, this means syntax tree validation. |
88857a2
to
c647e40
Compare
Okay looks like it should handle everything! And the code looks so much cleaner. |
c647e40
to
0af727d
Compare
bors r+ The next step would be to check self and super as well |
Build succeeded: |
Went to work on self/super and found a bug! I've submitted a fix #4227 Sorry about that. |
Next step: #4246 |
This solution does not fully handle
use
statements. See belowThis pull requests implements simple validation of usages of the
crate
keyword inPath
s. Specifically it validates that:PathSegment
is starts with thecrate
keyword, it is also the first segment of thePath
crate
inPath
s are considered errors.This aligns with
rustc
's rules. Unlike rustc this implementation does not issue a special error message in the case of::crate
but it does catch the error.Furthermore, this change does not cover all error cases. Specifically the following is not caught:
This is because this check is context sensitive. From an AST perspective,
crate
is the root of thePath
. Only by inspecting the fullUseItem
do we see that it is not in fact the root. This problem becomes worse becauseUseTree
s are allowed to be arbitrarily nested:So this is a hard problem to solve without essentially a breadth-first search. In a traditional compiler, I'd say this error is most easily found during the AST -> HIR conversion pass but within rust-analyzer I'm not sure where it belongs.
Under the implementation in this PR, such errors are ignored so we're more correct just not entirely correct.