Skip to content

Commit 1840f57

Browse files
committed
add diagnostic for dangling impl
1 parent 2f9e558 commit 1840f57

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,21 @@ fn validate_path_keywords(segment: ast::PathSegment, errors: &mut Vec<SyntaxErro
315315
}
316316

317317
fn validate_trait_object_ref_ty(ty: ast::RefType, errors: &mut Vec<SyntaxError>) {
318-
if let Some(ast::Type::DynTraitType(ty)) = ty.ty() {
319-
if let Some(err) = validate_trait_object_ty(ty) {
320-
errors.push(err);
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+
}
321331
}
332+
_ => {}
322333
}
323334
}
324335

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)