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
This commit is contained in:
Lukas Wirth 2025-03-25 07:49:45 +00:00 committed by GitHub
commit ed7e25a0c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 99 additions and 1 deletions

View file

@ -363,11 +363,40 @@ fn validate_trait_object_ty(ty: ast::DynTraitType) -> Option<SyntaxError> {
}
fn validate_impl_object_ty(ty: ast::ImplTraitType, errors: &mut Vec<SyntaxError>) {
if ty.type_bound_list().map_or(0, |tbl| tbl.bounds().count()) == 0 {
let Some(bound_list) = ty.type_bound_list() else {
errors.push(SyntaxError::new(
"At least one trait must be specified",
ty.syntax().text_range(),
));
return;
};
let bounds: Vec<_> = bound_list.bounds().collect();
if !bounds.iter().any(|b| !matches!(b.kind(), ast::TypeBoundKind::Lifetime(_))) {
errors.push(SyntaxError::new(
"At least one trait must be specified",
ty.syntax().text_range(),
));
return;
}
if bounds.len() == 1 {
return;
}
let Some(preceding_token) = ty
.impl_token()
.and_then(|token| token.prev_token())
.and_then(|prev| algo::skip_trivia_token(prev, Direction::Prev))
else {
return;
};
if !matches!(preceding_token.kind(), T!['('] | T![<] | T![=])
&& matches!(preceding_token.kind(), T![&])
{
errors.push(SyntaxError::new("ambiguous `+` in a type", ty.syntax().text_range()));
}
}