Skip to content

Commit ed7e25a

Browse files
authored
Merge pull request #19347 from Shourya742/2025-03-13-add-diagnostic-for-dnagling-impl-with-lifetime
Add diagnostic for missing ambiguity error for impl trait
2 parents fbad4e6 + df56707 commit ed7e25a

5 files changed

+99
-1
lines changed

crates/syntax/src/validation.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,40 @@ fn validate_trait_object_ty(ty: ast::DynTraitType) -> Option<SyntaxError> {
363363
}
364364

365365
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 {
366+
let Some(bound_list) = ty.type_bound_list() else {
367367
errors.push(SyntaxError::new(
368368
"At least one trait must be specified",
369369
ty.syntax().text_range(),
370370
));
371+
return;
372+
};
373+
374+
let bounds: Vec<_> = bound_list.bounds().collect();
375+
376+
if !bounds.iter().any(|b| !matches!(b.kind(), ast::TypeBoundKind::Lifetime(_))) {
377+
errors.push(SyntaxError::new(
378+
"At least one trait must be specified",
379+
ty.syntax().text_range(),
380+
));
381+
return;
382+
}
383+
384+
if bounds.len() == 1 {
385+
return;
386+
}
387+
388+
let Some(preceding_token) = ty
389+
.impl_token()
390+
.and_then(|token| token.prev_token())
391+
.and_then(|prev| algo::skip_trivia_token(prev, Direction::Prev))
392+
else {
393+
return;
394+
};
395+
396+
if !matches!(preceding_token.kind(), T!['('] | T![<] | T![=])
397+
&& matches!(preceding_token.kind(), T![&])
398+
{
399+
errors.push(SyntaxError::new("ambiguous `+` in a type", ty.syntax().text_range()));
371400
}
372401
}
373402

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f(_: &impl 'a + Sized) {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f(_: &impl 'a) {}

0 commit comments

Comments
 (0)