Skip to content

Commit 9a8968f

Browse files
authored
Merge pull request #19265 from Shourya742/2025-03-01-add-dangling-dyn-diagnostic
feat: Add diagnostic for dangling dyn and impl
2 parents d137fee + c2a630e commit 9a8968f

File tree

7 files changed

+103
-9
lines changed

7 files changed

+103
-9
lines changed

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

Lines changed: 27 additions & 9 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
}
@@ -340,17 +341,34 @@ fn validate_trait_object_fn_ptr_ret_ty(ty: ast::FnPtrType, errors: &mut Vec<Synt
340341

341342
fn validate_trait_object_ty(ty: ast::DynTraitType) -> Option<SyntaxError> {
342343
let tbl = ty.type_bound_list()?;
343-
344-
if tbl.bounds().count() > 1 {
345-
let dyn_token = ty.dyn_token()?;
346-
let potential_parenthesis =
347-
algo::skip_trivia_token(dyn_token.prev_token()?, Direction::Prev)?;
348-
let kind = potential_parenthesis.kind();
349-
if !matches!(kind, T!['('] | T![<] | T![=]) {
350-
return Some(SyntaxError::new("ambiguous `+` in a type", ty.syntax().text_range()));
344+
let bounds_count = tbl.bounds().count();
345+
346+
match bounds_count {
347+
0 => Some(SyntaxError::new(
348+
"At least one trait is required for an object type",
349+
ty.syntax().text_range(),
350+
)),
351+
_ if bounds_count > 1 => {
352+
let dyn_token = ty.dyn_token()?;
353+
let preceding_token =
354+
algo::skip_trivia_token(dyn_token.prev_token()?, Direction::Prev)?;
355+
356+
if !matches!(preceding_token.kind(), T!['('] | T![<] | T![=]) {
357+
return Some(SyntaxError::new("ambiguous `+` in a type", ty.syntax().text_range()));
358+
}
359+
None
351360
}
361+
_ => None,
362+
}
363+
}
364+
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+
));
352371
}
353-
None
354372
}
355373

356374
fn validate_macro_rules(mac: ast::MacroRules, errors: &mut Vec<SyntaxError>) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
SOURCE_FILE@0..16
2+
FN@0..16
3+
FN_KW@0..2 "fn"
4+
WHITESPACE@2..3 " "
5+
NAME@3..4
6+
IDENT@3..4 "f"
7+
PARAM_LIST@4..13
8+
L_PAREN@4..5 "("
9+
PARAM@5..12
10+
WILDCARD_PAT@5..6
11+
UNDERSCORE@5..6 "_"
12+
COLON@6..7 ":"
13+
WHITESPACE@7..8 " "
14+
REF_TYPE@8..12
15+
AMP@8..9 "&"
16+
DYN_TRAIT_TYPE@9..12
17+
DYN_KW@9..12 "dyn"
18+
TYPE_BOUND_LIST@12..12
19+
R_PAREN@12..13 ")"
20+
WHITESPACE@13..14 " "
21+
BLOCK_EXPR@14..16
22+
STMT_LIST@14..16
23+
L_CURLY@14..15 "{"
24+
R_CURLY@15..16 "}"
25+
error 9..12: At least one trait is required for an object type
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f(_: &dyn) {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
SOURCE_FILE@0..16
2+
FN@0..16
3+
FN_KW@0..2 "fn"
4+
WHITESPACE@2..3 " "
5+
NAME@3..4
6+
IDENT@3..4 "f"
7+
PARAM_LIST@4..13
8+
L_PAREN@4..5 "("
9+
PARAM@5..12
10+
WILDCARD_PAT@5..6
11+
UNDERSCORE@5..6 "_"
12+
COLON@6..7 ":"
13+
WHITESPACE@7..8 " "
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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)