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
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)