From a8a18e7171dee302b0cbd609a0347f70ee3edfd3 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 21 Mar 2025 11:51:13 -0400 Subject: [PATCH] red_knot_python_semantic: remove `WithDiagnostic` trait I did this mostly because it wasn't buying us much, and I'm trying to simplify the public API of the types I'd like to refactor in order to make the refactor simpler. If we really want something like this, we can re-add it later. --- .../src/types/context.rs | 11 ++--------- .../red_knot_python_semantic/src/types/infer.rs | 16 +++++----------- .../src/types/unpacker.rs | 7 +++---- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types/context.rs b/crates/red_knot_python_semantic/src/types/context.rs index 3e7a47b1ed..c5ed58dbc6 100644 --- a/crates/red_knot_python_semantic/src/types/context.rs +++ b/crates/red_knot_python_semantic/src/types/context.rs @@ -59,11 +59,8 @@ impl<'db> InferContext<'db> { self.db } - pub(crate) fn extend(&mut self, other: &T) - where - T: WithDiagnostics, - { - self.diagnostics.get_mut().extend(other.diagnostics()); + pub(crate) fn extend(&mut self, other: &TypeCheckDiagnostics) { + self.diagnostics.get_mut().extend(other); } /// Reports a lint located at `ranged`. @@ -223,7 +220,3 @@ pub(crate) enum InNoTypeCheck { /// The inference is known to be in an `@no_type_check` decorated function. Yes, } - -pub(crate) trait WithDiagnostics { - fn diagnostics(&self) -> &TypeCheckDiagnostics; -} diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 3eefb1112c..4ee4d8a55a 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -88,7 +88,7 @@ use crate::util::subscript::{PyIndex, PySlice}; use crate::Db; use super::class_base::ClassBase; -use super::context::{InNoTypeCheck, InferContext, WithDiagnostics}; +use super::context::{InNoTypeCheck, InferContext}; use super::diagnostic::{ report_index_out_of_bounds, report_invalid_exception_caught, report_invalid_exception_cause, report_invalid_exception_raised, report_invalid_type_checking_constant, @@ -439,12 +439,6 @@ impl<'db> TypeInference<'db> { } } -impl WithDiagnostics for TypeInference<'_> { - fn diagnostics(&self) -> &TypeCheckDiagnostics { - &self.diagnostics - } -} - /// Whether the intersection type is on the left or right side of the comparison. #[derive(Debug, Clone, Copy)] enum IntersectionOn { @@ -567,7 +561,7 @@ impl<'db> TypeInferenceBuilder<'db> { .extend(inference.declarations.iter()); self.types.expressions.extend(inference.expressions.iter()); self.types.deferred.extend(inference.deferred.iter()); - self.context.extend(inference); + self.context.extend(inference.diagnostics()); } fn file(&self) -> File { @@ -1863,7 +1857,7 @@ impl<'db> TypeInferenceBuilder<'db> { let unpacked = infer_unpack_types(self.db(), unpack); let name_ast_id = name.scoped_expression_id(self.db(), self.scope()); if unpack_position == UnpackPosition::First { - self.context.extend(unpacked); + self.context.extend(unpacked.diagnostics()); } unpacked.expression_type(name_ast_id) } @@ -2673,7 +2667,7 @@ impl<'db> TypeInferenceBuilder<'db> { // Only copy the diagnostics if this is the first assignment to avoid duplicating the // unpack assignments. if unpack_position == UnpackPosition::First { - self.context.extend(unpacked); + self.context.extend(unpacked.diagnostics()); } let name_ast_id = name.scoped_expression_id(self.db(), self.scope()); @@ -2973,7 +2967,7 @@ impl<'db> TypeInferenceBuilder<'db> { TargetKind::Sequence(unpack_position, unpack) => { let unpacked = infer_unpack_types(self.db(), unpack); if unpack_position == UnpackPosition::First { - self.context.extend(unpacked); + self.context.extend(unpacked.diagnostics()); } let name_ast_id = name.scoped_expression_id(self.db(), self.scope()); unpacked.expression_type(name_ast_id) diff --git a/crates/red_knot_python_semantic/src/types/unpacker.rs b/crates/red_knot_python_semantic/src/types/unpacker.rs index b70f7913ce..b32218de45 100644 --- a/crates/red_knot_python_semantic/src/types/unpacker.rs +++ b/crates/red_knot_python_semantic/src/types/unpacker.rs @@ -11,7 +11,7 @@ use crate::types::{infer_expression_types, todo_type, Type, TypeCheckDiagnostics use crate::unpack::{UnpackKind, UnpackValue}; use crate::Db; -use super::context::{InferContext, WithDiagnostics}; +use super::context::InferContext; use super::diagnostic::INVALID_ASSIGNMENT; use super::{TupleType, UnionType}; @@ -283,10 +283,9 @@ impl<'db> UnpackResult<'db> { pub(crate) fn expression_type(&self, expr_id: ScopedExpressionId) -> Type<'db> { self.targets[&expr_id] } -} -impl WithDiagnostics for UnpackResult<'_> { - fn diagnostics(&self) -> &TypeCheckDiagnostics { + /// Returns the diagnostics in this unpacking assignment. + pub(crate) fn diagnostics(&self) -> &TypeCheckDiagnostics { &self.diagnostics } }