[ty] Improve invalid-argument-type diagnostics where a union type was provided (#21044)

This commit is contained in:
Alex Waygood 2025-10-23 14:16:21 +01:00 committed by GitHub
parent 01695513ce
commit dab3d4e917
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 189 additions and 0 deletions

View file

@ -3519,6 +3519,36 @@ impl<'db> BindingError<'db> {
"Expected `{expected_ty_display}`, found `{provided_ty_display}`"
));
if let Type::Union(union) = provided_ty {
let union_elements = union.elements(context.db());
let invalid_elements: Vec<Type<'db>> = union
.elements(context.db())
.iter()
.filter(|element| !element.is_assignable_to(context.db(), *expected_ty))
.copied()
.collect();
let first_invalid_element = invalid_elements[0].display(context.db());
if invalid_elements.len() < union_elements.len() {
match &invalid_elements[1..] {
[] => diag.info(format_args!(
"Element `{first_invalid_element}` of this union \
is not assignable to `{expected_ty_display}`",
)),
[single] => diag.info(format_args!(
"Union elements `{first_invalid_element}` and `{}` \
are not assignable to `{expected_ty_display}`",
single.display(context.db()),
)),
rest => diag.info(format_args!(
"Union element `{first_invalid_element}`, \
and {} more union elements, \
are not assignable to `{expected_ty_display}`",
rest.len(),
)),
}
}
}
if let Some(matching_overload) = matching_overload {
if let Some((name_span, parameter_span)) =
matching_overload.get(context.db()).and_then(|overload| {