mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Merge #4246
4246: Validate uses of self and super r=matklad a=djrenren 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. Co-authored-by: John Renner <john@jrenner.net>
This commit is contained in:
commit
21588e15df
9 changed files with 180 additions and 77 deletions
|
@ -1241,6 +1241,8 @@ pub struct PathSegment {
|
|||
impl PathSegment {
|
||||
pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) }
|
||||
pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) }
|
||||
pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) }
|
||||
pub fn super_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![super]) }
|
||||
pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
|
||||
pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
|
||||
pub fn type_arg_list(&self) -> Option<TypeArgList> { support::child(&self.syntax) }
|
||||
|
|
|
@ -96,7 +96,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
|
|||
ast::RecordField(it) => validate_numeric_name(it.name_ref(), &mut errors),
|
||||
ast::Visibility(it) => validate_visibility(it, &mut errors),
|
||||
ast::RangeExpr(it) => validate_range_expr(it, &mut errors),
|
||||
ast::PathSegment(it) => validate_crate_keyword_in_path_segment(it, &mut errors),
|
||||
ast::PathSegment(it) => validate_path_keywords(it, &mut errors),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
@ -224,59 +224,82 @@ fn validate_range_expr(expr: ast::RangeExpr, errors: &mut Vec<SyntaxError>) {
|
|||
}
|
||||
}
|
||||
|
||||
fn validate_crate_keyword_in_path_segment(
|
||||
segment: ast::PathSegment,
|
||||
errors: &mut Vec<SyntaxError>,
|
||||
) {
|
||||
const ERR_MSG: &str = "The `crate` keyword is only allowed as the first segment of a path";
|
||||
fn validate_path_keywords(segment: ast::PathSegment, errors: &mut Vec<SyntaxError>) {
|
||||
use ast::PathSegmentKind;
|
||||
|
||||
let crate_token = match segment.crate_token() {
|
||||
None => return,
|
||||
Some(it) => it,
|
||||
};
|
||||
let path = segment.parent_path();
|
||||
let is_path_start = segment.coloncolon_token().is_none() && path.qualifier().is_none();
|
||||
|
||||
// Disallow both ::crate and foo::crate
|
||||
let mut path = segment.parent_path();
|
||||
if segment.coloncolon_token().is_some() || path.qualifier().is_some() {
|
||||
errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range()));
|
||||
return;
|
||||
}
|
||||
if let Some(token) = segment.self_token() {
|
||||
if !is_path_start {
|
||||
errors.push(SyntaxError::new(
|
||||
"The `self` keyword is only allowed as the first segment of a path",
|
||||
token.text_range(),
|
||||
));
|
||||
}
|
||||
} else if let Some(token) = segment.crate_token() {
|
||||
if !is_path_start || use_prefix(path).is_some() {
|
||||
errors.push(SyntaxError::new(
|
||||
"The `crate` keyword is only allowed as the first segment of a path",
|
||||
token.text_range(),
|
||||
));
|
||||
}
|
||||
} else if let Some(token) = segment.super_token() {
|
||||
if !all_supers(&path) {
|
||||
errors.push(SyntaxError::new(
|
||||
"The `super` keyword may only be preceded by other `super`s",
|
||||
token.text_range(),
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
// For expressions and types, validation is complete, but we still have
|
||||
// to handle invalid UseItems like this:
|
||||
//
|
||||
// use foo:{crate::bar::baz};
|
||||
//
|
||||
// To handle this we must inspect the parent `UseItem`s and `UseTree`s
|
||||
// but right now we're looking deep inside the nested `Path` nodes because
|
||||
// `Path`s are left-associative:
|
||||
//
|
||||
// ((crate)::bar)::baz)
|
||||
// ^ current value of path
|
||||
//
|
||||
// So we need to climb to the top
|
||||
while let Some(parent) = path.parent_path() {
|
||||
path = parent;
|
||||
}
|
||||
|
||||
// Now that we've found the whole path we need to see if there's a prefix
|
||||
// somewhere in the UseTree hierarchy. This check is arbitrarily deep
|
||||
// because rust allows arbitrary nesting like so:
|
||||
//
|
||||
// use {foo::{{{{crate::bar::baz}}}}};
|
||||
for node in path.syntax().ancestors().skip(1) {
|
||||
match_ast! {
|
||||
match node {
|
||||
ast::UseTree(it) => if let Some(tree_path) = it.path() {
|
||||
// Even a top-level path exists within a `UseTree` so we must explicitly
|
||||
// allow our path but disallow anything else
|
||||
if tree_path != path {
|
||||
errors.push(SyntaxError::new(ERR_MSG, crate_token.text_range()));
|
||||
}
|
||||
},
|
||||
ast::UseTreeList(_it) => continue,
|
||||
_ => return,
|
||||
let mut curr_path = path;
|
||||
while let Some(prefix) = use_prefix(curr_path) {
|
||||
if !all_supers(&prefix) {
|
||||
errors.push(SyntaxError::new(
|
||||
"The `super` keyword may only be preceded by other `super`s",
|
||||
token.text_range(),
|
||||
));
|
||||
return;
|
||||
}
|
||||
curr_path = prefix;
|
||||
}
|
||||
}
|
||||
|
||||
fn use_prefix(mut path: ast::Path) -> Option<ast::Path> {
|
||||
for node in path.syntax().ancestors().skip(1) {
|
||||
match_ast! {
|
||||
match node {
|
||||
ast::UseTree(it) => if let Some(tree_path) = it.path() {
|
||||
// Even a top-level path exists within a `UseTree` so we must explicitly
|
||||
// allow our path but disallow anything else
|
||||
if tree_path != path {
|
||||
return Some(tree_path);
|
||||
}
|
||||
},
|
||||
ast::UseTreeList(_it) => continue,
|
||||
ast::Path(parent) => path = parent,
|
||||
_ => return None,
|
||||
}
|
||||
};
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
fn all_supers(path: &ast::Path) -> bool {
|
||||
let segment = match path.segment() {
|
||||
Some(it) => it,
|
||||
None => return false,
|
||||
};
|
||||
|
||||
if segment.kind() != Some(PathSegmentKind::SuperKw) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if let Some(ref subpath) = path.qualifier() {
|
||||
return all_supers(subpath);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue