[ty] Improve specialization-error diagnostics

This commit is contained in:
David Peter
2025-09-10 11:43:21 +02:00
parent 0f371f6efd
commit 6a3101b0be
6 changed files with 19 additions and 13 deletions

View File

@@ -74,7 +74,7 @@ error[invalid-argument-type]: Argument to function `f` is incorrect
10 | reveal_type(f(True)) # revealed: Literal[True]
11 | # error: [invalid-argument-type]
12 | reveal_type(f("string")) # revealed: Unknown
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy upper bound of type variable `T`
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy upper bound of type variable `T` (int)
|
info: Type variable defined here
--> src/mdtest_snippet.py:4:1

View File

@@ -89,7 +89,7 @@ error[invalid-argument-type]: Argument to function `f` is incorrect
11 | reveal_type(f(None)) # revealed: None
12 | # error: [invalid-argument-type]
13 | reveal_type(f("string")) # revealed: Unknown
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy constraints of type variable `T`
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy constraints of type variable `T` (int, None)
|
info: Type variable defined here
--> src/mdtest_snippet.py:4:1

View File

@@ -71,7 +71,7 @@ error[invalid-argument-type]: Argument to function `f` is incorrect
7 | reveal_type(f(True)) # revealed: Literal[True]
8 | # error: [invalid-argument-type]
9 | reveal_type(f("string")) # revealed: Unknown
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy upper bound of type variable `T`
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy upper bound of type variable `T` (int)
|
info: Type variable defined here
--> src/mdtest_snippet.py:3:7

View File

@@ -86,7 +86,7 @@ error[invalid-argument-type]: Argument to function `f` is incorrect
8 | reveal_type(f(None)) # revealed: None
9 | # error: [invalid-argument-type]
10 | reveal_type(f("string")) # revealed: Unknown
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy constraints of type variable `T`
| ^^^^^^^^ Argument type `Literal["string"]` does not satisfy constraints of type variable `T` (int, None)
|
info: Type variable defined here
--> src/mdtest_snippet.py:3:7

View File

@@ -188,7 +188,7 @@ error[invalid-argument-type]: Argument to function `f4` is incorrect
58 | # error: [call-non-callable] "Object of type `Literal[5]` is not callable"
59 | # error: [call-non-callable] "Object of type `PossiblyNotCallable` is not callable (possibly unbound `__call__` method)"
60 | x = f(3)
| ^ Argument type `Literal[3]` does not satisfy upper bound of type variable `T`
| ^ Argument type `Literal[3]` does not satisfy upper bound of type variable `T` (str)
|
info: Type variable defined here
--> src/mdtest_snippet.py:13:8

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| 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()))