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

Commit 2f9e558

Browse files
committed
add diagnostic for dangling dyn
1 parent da06b7c commit 2f9e558

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,25 @@ fn validate_trait_object_fn_ptr_ret_ty(ty: ast::FnPtrType, errors: &mut Vec<Synt
340340

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

356364
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) {}

0 commit comments

Comments
 (0)