Skip to content

Add diagnostic for missing ambiguity error for impl trait #19347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion crates/syntax/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f(_: &impl 'a + Sized) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f(_: &impl 'a) {}