Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c2a630e

Browse files
committed
Add dangling impl
- Adds dangling impl diagnostics - Rename validation test from dangling_impl to dangling_iml_ref
1 parent 1840f57 commit c2a630e

File tree

5 files changed

+54
-31
lines changed

5 files changed

+54
-31
lines changed

src/tools/rust-analyzer/crates/syntax/src/validation.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub(crate) fn validate(root: &SyntaxNode, errors: &mut Vec<SyntaxError>) {
3737
ast::FnPtrType(it) => validate_trait_object_fn_ptr_ret_ty(it, errors),
3838
ast::MacroRules(it) => validate_macro_rules(it, errors),
3939
ast::LetExpr(it) => validate_let_expr(it, errors),
40+
ast::ImplTraitType(it) => validate_impl_object_ty(it, errors),
4041
_ => (),
4142
}
4243
}
@@ -315,21 +316,10 @@ fn validate_path_keywords(segment: ast::PathSegment, errors: &mut Vec<SyntaxErro
315316
}
316317

317318
fn validate_trait_object_ref_ty(ty: ast::RefType, errors: &mut Vec<SyntaxError>) {
318-
match ty.ty() {
319-
Some(ast::Type::DynTraitType(ty)) => {
320-
if let Some(err) = validate_trait_object_ty(ty) {
321-
errors.push(err);
322-
}
323-
}
324-
Some(ast::Type::ImplTraitType(ty)) => {
325-
if ty.type_bound_list().map_or(0, |tbl| tbl.bounds().count()) == 0 {
326-
errors.push(SyntaxError::new(
327-
"At least one trait must be specified",
328-
ty.syntax().text_range(),
329-
));
330-
}
319+
if let Some(ast::Type::DynTraitType(ty)) = ty.ty() {
320+
if let Some(err) = validate_trait_object_ty(ty) {
321+
errors.push(err);
331322
}
332-
_ => {}
333323
}
334324
}
335325

@@ -372,6 +362,15 @@ fn validate_trait_object_ty(ty: ast::DynTraitType) -> Option<SyntaxError> {
372362
}
373363
}
374364

365+
fn validate_impl_object_ty(ty: ast::ImplTraitType, errors: &mut Vec<SyntaxError>) {
366+
if ty.type_bound_list().map_or(0, |tbl| tbl.bounds().count()) == 0 {
367+
errors.push(SyntaxError::new(
368+
"At least one trait must be specified",
369+
ty.syntax().text_range(),
370+
));
371+
}
372+
}
373+
375374
fn validate_macro_rules(mac: ast::MacroRules, errors: &mut Vec<SyntaxError>) {
376375
if let Some(vis) = mac.visibility() {
377376
errors.push(SyntaxError::new(
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
SOURCE_FILE@0..17
2-
FN@0..17
1+
SOURCE_FILE@0..16
2+
FN@0..16
33
FN_KW@0..2 "fn"
44
WHITESPACE@2..3 " "
55
NAME@3..4
66
IDENT@3..4 "f"
7-
PARAM_LIST@4..14
7+
PARAM_LIST@4..13
88
L_PAREN@4..5 "("
9-
PARAM@5..13
9+
PARAM@5..12
1010
WILDCARD_PAT@5..6
1111
UNDERSCORE@5..6 "_"
1212
COLON@6..7 ":"
1313
WHITESPACE@7..8 " "
14-
REF_TYPE@8..13
15-
AMP@8..9 "&"
16-
IMPL_TRAIT_TYPE@9..13
17-
IMPL_KW@9..13 "impl"
18-
TYPE_BOUND_LIST@13..13
19-
R_PAREN@13..14 ")"
20-
WHITESPACE@14..15 " "
21-
BLOCK_EXPR@15..17
22-
STMT_LIST@15..17
23-
L_CURLY@15..16 "{"
24-
R_CURLY@16..17 "}"
25-
error 9..13: At least one trait must be specified
14+
IMPL_TRAIT_TYPE@8..12
15+
IMPL_KW@8..12 "impl"
16+
TYPE_BOUND_LIST@12..12
17+
R_PAREN@12..13 ")"
18+
WHITESPACE@13..14 " "
19+
BLOCK_EXPR@14..16
20+
STMT_LIST@14..16
21+
L_CURLY@14..15 "{"
22+
R_CURLY@15..16 "}"
23+
error 8..12: At least one trait must be specified
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fn f(_: &impl) {}
1+
fn f(_: impl) {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
SOURCE_FILE@0..17
2+
FN@0..17
3+
FN_KW@0..2 "fn"
4+
WHITESPACE@2..3 " "
5+
NAME@3..4
6+
IDENT@3..4 "f"
7+
PARAM_LIST@4..14
8+
L_PAREN@4..5 "("
9+
PARAM@5..13
10+
WILDCARD_PAT@5..6
11+
UNDERSCORE@5..6 "_"
12+
COLON@6..7 ":"
13+
WHITESPACE@7..8 " "
14+
REF_TYPE@8..13
15+
AMP@8..9 "&"
16+
IMPL_TRAIT_TYPE@9..13
17+
IMPL_KW@9..13 "impl"
18+
TYPE_BOUND_LIST@13..13
19+
R_PAREN@13..14 ")"
20+
WHITESPACE@14..15 " "
21+
BLOCK_EXPR@15..17
22+
STMT_LIST@15..17
23+
L_CURLY@15..16 "{"
24+
R_CURLY@16..17 "}"
25+
error 9..13: At least one trait must be specified
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f(_: &impl) {}

0 commit comments

Comments
 (0)