Skip to content

Commit 309c517

Browse files
committed
[ty] Consolidate rebound-typevar reporting into a single function
Merge `report_rebound_typevar` and `report_rebound_typevar_function` into a single `report_shadowed_type_variable` that takes the entity kind ("class"/"function"), name, and range as parameters. https://claude.ai/code/session_01Eojz9eLtLnKtAF6LcxZczF
1 parent e61132f commit 309c517

2 files changed

Lines changed: 21 additions & 48 deletions

File tree

crates/ty_python_semantic/src/types/diagnostic.rs

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,54 +4942,23 @@ pub(crate) fn report_invalid_type_param_order<'db>(
49424942
}
49434943
}
49444944

4945-
pub(crate) fn report_rebound_typevar<'db>(
4945+
pub(crate) fn report_shadowed_type_variable<'db>(
49464946
context: &InferContext<'db, '_>,
49474947
typevar_name: &ast::name::Name,
4948-
class: StaticClassLiteral<'db>,
4949-
class_node: &ast::StmtClassDef,
4950-
other_typevar: BoundTypeVarInstance<'db>,
4951-
) {
4952-
let db = context.db();
4953-
let Some(builder) = context.report_lint(&SHADOWED_TYPE_VARIABLE, class.header_range(db)) else {
4954-
return;
4955-
};
4956-
let mut diagnostic = builder.into_diagnostic(format_args!(
4957-
"Generic class `{}` uses type variable `{typevar_name}` already bound by an enclosing scope",
4958-
class_node.name,
4959-
));
4960-
diagnostic.set_primary_message(format_args!(
4961-
"`{typevar_name}` used in class definition here"
4962-
));
4963-
let Some(other_definition) = other_typevar.binding_context(db).definition() else {
4964-
return;
4965-
};
4966-
let span = match binding_type(db, other_definition) {
4967-
Type::ClassLiteral(class) => class.header_span(db),
4968-
Type::FunctionLiteral(function) => function.spans(db).signature,
4969-
_ => return,
4970-
};
4971-
diagnostic.annotate(Annotation::secondary(span).message(format_args!(
4972-
"Type variable `{typevar_name}` is bound in this enclosing scope",
4973-
)));
4974-
}
4975-
4976-
pub(crate) fn report_rebound_typevar_function<'db>(
4977-
context: &InferContext<'db, '_>,
4978-
typevar_name: &ast::name::Name,
4979-
function_node: &ast::StmtFunctionDef,
4948+
kind: &str,
4949+
name: &ast::name::Name,
4950+
range: TextRange,
49804951
other_typevar: BoundTypeVarInstance<'db>,
49814952
) {
49824953
let db = context.db();
4983-
let Some(builder) = context.report_lint(&SHADOWED_TYPE_VARIABLE, function_node.name.range())
4984-
else {
4954+
let Some(builder) = context.report_lint(&SHADOWED_TYPE_VARIABLE, range) else {
49854955
return;
49864956
};
49874957
let mut diagnostic = builder.into_diagnostic(format_args!(
4988-
"Generic function `{}` uses type variable `{typevar_name}` already bound by an enclosing scope",
4989-
function_node.name,
4958+
"Generic {kind} `{name}` uses type variable `{typevar_name}` already bound by an enclosing scope",
49904959
));
49914960
diagnostic.set_primary_message(format_args!(
4992-
"`{typevar_name}` used in function definition here"
4961+
"`{typevar_name}` used in {kind} definition here"
49934962
));
49944963
let Some(other_definition) = other_typevar.binding_context(db).definition() else {
49954964
return;

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ use crate::types::diagnostic::{
105105
report_match_pattern_against_typed_dict, report_named_tuple_field_with_leading_underscore,
106106
report_namedtuple_field_without_default_after_field_with_default, report_not_subscriptable,
107107
report_possibly_missing_attribute, report_possibly_unresolved_reference,
108-
report_rebound_typevar, report_rebound_typevar_function,
109-
report_unsupported_augmented_assignment, report_unsupported_base,
108+
report_shadowed_type_variable, report_unsupported_augmented_assignment,
109+
report_unsupported_base,
110110
report_unsupported_binary_operation, report_unsupported_comparison,
111111
};
112112
use crate::types::enums::is_enum_class_by_inheritance;
@@ -1453,11 +1453,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
14531453
if let Some(other_typevar) =
14541454
enclosing.binds_named_typevar(self.db(), name)
14551455
{
1456-
report_rebound_typevar(
1456+
report_shadowed_type_variable(
14571457
&self.context,
14581458
name,
1459-
class,
1460-
class_node,
1459+
"class",
1460+
&class_node.name.id,
1461+
class.header_range(self.db()),
14611462
other_typevar,
14621463
);
14631464
}
@@ -1472,11 +1473,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
14721473
for enclosing in enclosing_generic_contexts(self.db(), self.index, parent) {
14731474
if let Some(other_typevar) = enclosing.binds_typevar(self.db(), typevar)
14741475
{
1475-
report_rebound_typevar(
1476+
report_shadowed_type_variable(
14761477
&self.context,
14771478
typevar.name(self.db()),
1478-
class,
1479-
class_node,
1479+
"class",
1480+
&class_node.name.id,
1481+
class.header_range(self.db()),
14801482
other_typevar,
14811483
);
14821484
}
@@ -3295,10 +3297,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
32953297
if let Some(other_typevar) =
32963298
enclosing.binds_named_typevar(self.db(), &param_name.id)
32973299
{
3298-
report_rebound_typevar_function(
3300+
report_shadowed_type_variable(
32993301
&self.context,
33003302
&param_name.id,
3301-
function,
3303+
"function",
3304+
&function.name.id,
3305+
function.name.range(),
33023306
other_typevar,
33033307
);
33043308
}

0 commit comments

Comments
 (0)