From dc611047262ce324ad4d7fd1336abdcea863d889 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 9 Jan 2026 18:37:36 +0000 Subject: [PATCH] [ty] Derive `Default` in a few more places in `place.rs` (#22481) --- crates/ty_python_semantic/src/place.rs | 28 +++++-------------- crates/ty_python_semantic/src/types/class.rs | 2 +- .../src/types/infer/builder.rs | 13 ++++----- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/crates/ty_python_semantic/src/place.rs b/crates/ty_python_semantic/src/place.rs index 5c586884d3..bd1a616d59 100644 --- a/crates/ty_python_semantic/src/place.rs +++ b/crates/ty_python_semantic/src/place.rs @@ -156,9 +156,10 @@ impl<'db> DefinedPlace<'db> { /// bound_or_declared: Place::Defined(DefinedPlace { ty: Literal[1], origin: TypeOrigin::Inferred, definedness: Definedness::PossiblyUndefined, .. }), /// non_existent: Place::Undefined, /// ``` -#[derive(Debug, Clone, Copy, PartialEq, Eq, salsa::Update, get_size2::GetSize)] +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, salsa::Update, get_size2::GetSize)] pub(crate) enum Place<'db> { Defined(DefinedPlace<'db>), + #[default] Undefined, } @@ -592,6 +593,7 @@ type DeclaredTypeAndConflictingTypes<'db> = ( ); /// The result of looking up a declared type from declarations; see [`place_from_declarations`]. +#[derive(Debug, Default)] pub(crate) struct PlaceFromDeclarationsResult<'db> { place_and_quals: PlaceAndQualifiers<'db>, conflicting_types: Option>>>, @@ -641,21 +643,12 @@ impl<'db> PlaceFromDeclarationsResult<'db> { /// that this comes with a [`CLASS_VAR`] type qualifier. /// /// [`CLASS_VAR`]: crate::types::TypeQualifiers::CLASS_VAR -#[derive(Debug, Clone, Copy, PartialEq, Eq, salsa::Update, get_size2::GetSize)] +#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, salsa::Update, get_size2::GetSize)] pub(crate) struct PlaceAndQualifiers<'db> { pub(crate) place: Place<'db>, pub(crate) qualifiers: TypeQualifiers, } -impl Default for PlaceAndQualifiers<'_> { - fn default() -> Self { - PlaceAndQualifiers { - place: Place::Undefined, - qualifiers: TypeQualifiers::empty(), - } - } -} - impl<'db> PlaceAndQualifiers<'db> { /// Constructor that creates a [`PlaceAndQualifiers`] instance with a [`TodoType`] type /// and no qualifiers. @@ -669,10 +662,7 @@ impl<'db> PlaceAndQualifiers<'db> { } pub(crate) fn unbound() -> Self { - PlaceAndQualifiers { - place: Place::Undefined, - qualifiers: TypeQualifiers::empty(), - } + Self::default() } pub(crate) fn is_undefined(&self) -> bool { @@ -1567,11 +1557,7 @@ fn place_from_declarations_impl<'db>( } } } else { - PlaceFromDeclarationsResult { - place_and_quals: Place::Undefined.into(), - conflicting_types: None, - first_declaration: None, - } + PlaceFromDeclarationsResult::default() } } @@ -1893,7 +1879,7 @@ mod tests { let ty1 = Type::IntLiteral(1); let ty2 = Type::IntLiteral(2); - let unbound = || Place::Undefined.with_qualifiers(TypeQualifiers::empty()); + let unbound = || PlaceAndQualifiers::default(); let possibly_unbound_ty1 = || { Place::Defined(DefinedPlace { diff --git a/crates/ty_python_semantic/src/types/class.rs b/crates/ty_python_semantic/src/types/class.rs index 5e0339b53f..15266a422e 100644 --- a/crates/ty_python_semantic/src/types/class.rs +++ b/crates/ty_python_semantic/src/types/class.rs @@ -3479,7 +3479,7 @@ impl<'db> ClassLiteral<'db> { } if union.is_empty() { - Place::Undefined.with_qualifiers(TypeQualifiers::empty()) + PlaceAndQualifiers::default() } else { let boundness = if is_definitely_bound { Definedness::AlwaysDefined diff --git a/crates/ty_python_semantic/src/types/infer/builder.rs b/crates/ty_python_semantic/src/types/infer/builder.rs index ebcf245411..7ab259b82d 100644 --- a/crates/ty_python_semantic/src/types/infer/builder.rs +++ b/crates/ty_python_semantic/src/types/infer/builder.rs @@ -1800,12 +1800,11 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { .index .place_table(scope) .place(declaration.place(self.db())); - if let PlaceExprRef::Symbol(symbol) = &place { - if scope.is_global() { - module_type_implicit_global_symbol(self.db(), symbol.name()) - } else { - Place::Undefined.into() - } + + if let PlaceExprRef::Symbol(symbol) = &place + && scope.is_global() + { + module_type_implicit_global_symbol(self.db(), symbol.name()) } else { Place::Undefined.into() } @@ -9704,7 +9703,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { } } - PlaceAndQualifiers::from(Place::Undefined) + PlaceAndQualifiers::default() // If we're in a class body, check for implicit class body symbols first. // These take precedence over globals. .or_fall_back_to(db, || {