[ty] Improve concise diagnostics for invalid exceptions when a user catches a tuple of objects (#21578)

This commit is contained in:
Alex Waygood 2025-11-22 13:46:46 +00:00 committed by GitHub
parent f495c6d4ae
commit f2ce5e561a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View File

@ -2436,6 +2436,7 @@ pub(super) fn report_possibly_missing_attribute(
pub(super) fn report_invalid_exception_tuple_caught<'db, 'ast>( pub(super) fn report_invalid_exception_tuple_caught<'db, 'ast>(
context: &InferContext<'db, 'ast>, context: &InferContext<'db, 'ast>,
node: &'ast ast::ExprTuple, node: &'ast ast::ExprTuple,
node_type: Type<'db>,
invalid_tuple_nodes: impl IntoIterator<Item = (&'ast ast::Expr, Type<'db>)>, invalid_tuple_nodes: impl IntoIterator<Item = (&'ast ast::Expr, Type<'db>)>,
) { ) {
let Some(builder) = context.report_lint(&INVALID_EXCEPTION_CAUGHT, node) else { let Some(builder) = context.report_lint(&INVALID_EXCEPTION_CAUGHT, node) else {
@ -2443,6 +2444,10 @@ pub(super) fn report_invalid_exception_tuple_caught<'db, 'ast>(
}; };
let mut diagnostic = builder.into_diagnostic("Invalid tuple caught in an exception handler"); let mut diagnostic = builder.into_diagnostic("Invalid tuple caught in an exception handler");
diagnostic.set_concise_message(format_args!(
"Cannot catch object of type `{}` in an exception handler",
node_type.display(context.db())
));
for (sub_node, ty) in invalid_tuple_nodes { for (sub_node, ty) in invalid_tuple_nodes {
let span = context.span(sub_node); let span = context.span(sub_node);

View File

@ -3053,7 +3053,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
.iter() .iter()
.map(|(index, ty)| (&tuple.elts[*index], **ty)); .map(|(index, ty)| (&tuple.elts[*index], **ty));
report_invalid_exception_tuple_caught(&self.context, tuple, invalid_elements); report_invalid_exception_tuple_caught(
&self.context,
tuple,
node_ty,
invalid_elements,
);
} else { } else {
report_invalid_exception_caught(&self.context, node, node_ty); report_invalid_exception_caught(&self.context, node, node_ty);
} }