[ty] Improve specialization-error diagnostics (#20326)
Some checks are pending
CI / cargo build (release) (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / mkdocs (push) Waiting to run
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary

Add information about the upper bound or the constraints of the type
variable to the `SpecializationError` diagnostics.
This commit is contained in:
David Peter 2025-09-10 14:01:23 +02:00 committed by GitHub
parent b85c995927
commit 2b51ec6531
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 21 additions and 15 deletions

View file

@ -3086,14 +3086,20 @@ impl<'db> BindingError<'db> {
String::new()
}
));
diag.set_primary_message(format_args!(
"Argument type `{argument_ty_display}` does not satisfy {} of type variable `{}`",
match error {
SpecializationError::MismatchedBound {..} => "upper bound",
SpecializationError::MismatchedConstraint {..} => "constraints",
},
typevar.name(context.db()),
));
let typevar_name = typevar.name(context.db());
match error {
SpecializationError::MismatchedBound { .. } => {
diag.set_primary_message(format_args!("Argument type `{argument_ty_display}` does not satisfy upper bound `{}` of type variable `{typevar_name}`",
typevar.upper_bound(context.db()).expect("type variable should have an upper bound if this error occurs").display(context.db())
));
}
SpecializationError::MismatchedConstraint { .. } => {
diag.set_primary_message(format_args!("Argument type `{argument_ty_display}` does not satisfy constraints ({}) of type variable `{typevar_name}`",
typevar.constraints(context.db()).expect("type variable should have constraints if this error occurs").iter().map(|ty| format!("`{}`", ty.display(context.db()))).join(", ")
));
}
}
if let Some(typevar_definition) = typevar.definition(context.db()) {
let module = parsed_module(context.db(), typevar_definition.file(context.db()))