Compare commits

..

1 Commits

Author SHA1 Message Date
Alex Waygood
598f05793e [ty] Does this make diagnostics go away on prefect? 2026-01-03 13:29:15 +00:00
6 changed files with 34 additions and 18 deletions

View File

@@ -7267,7 +7267,10 @@ impl<'db> Type<'db> {
}
(Some(Place::Defined(new_method, ..)), Place::Defined(init_method, ..)) => {
let callable = UnionType::from_elements(db, [new_method, init_method]);
let callable = UnionBuilder::new(db)
.add(*new_method)
.add(*init_method)
.build();
let new_method_bindings = new_method
.bindings(db)
@@ -10755,7 +10758,11 @@ fn walk_type_var_constraints<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
impl<'db> TypeVarConstraints<'db> {
fn as_type(self, db: &'db dyn Db) -> Type<'db> {
UnionType::from_elements(db, self.elements(db))
let mut builder = UnionBuilder::new(db);
for ty in self.elements(db) {
builder = builder.add(*ty);
}
builder.build()
}
fn to_instance(self, db: &'db dyn Db) -> Option<TypeVarConstraints<'db>> {
@@ -14105,6 +14112,9 @@ impl<'db> UnionType<'db> {
db: &'db dyn Db,
mut f: impl FnMut(&Type<'db>) -> bool,
) -> Type<'db> {
if self.elements(db).iter().all(|ty| f(ty)) {
return Type::Union(self);
}
self.elements(db)
.iter()
.filter(|ty| f(ty))

View File

@@ -8,7 +8,6 @@ use super::{
SubclassOfType, Truthiness, Type, TypeQualifiers, class_base::ClassBase,
function::FunctionType,
};
use crate::FxOrderMap;
use crate::place::TypeOrigin;
use crate::semantic_index::definition::{Definition, DefinitionState};
use crate::semantic_index::scope::{NodeWithScopeKind, Scope, ScopeKind};
@@ -162,8 +161,8 @@ fn fields_cycle_initial<'db>(
_self: ClassLiteral<'db>,
_specialization: Option<Specialization<'db>>,
_field_policy: CodeGeneratorKind<'db>,
) -> FxOrderMap<Name, Field<'db>> {
FxOrderMap::default()
) -> FxIndexMap<Name, Field<'db>> {
FxIndexMap::default()
}
/// A category of classes with code generation capabilities (with synthesized methods).
@@ -3148,7 +3147,7 @@ impl<'db> ClassLiteral<'db> {
db: &'db dyn Db,
specialization: Option<Specialization<'db>>,
field_policy: CodeGeneratorKind<'db>,
) -> FxOrderMap<Name, Field<'db>> {
) -> FxIndexMap<Name, Field<'db>> {
if field_policy == CodeGeneratorKind::NamedTuple {
// NamedTuples do not allow multiple inheritance, so it is sufficient to enumerate the
// fields of this class only.
@@ -3196,8 +3195,8 @@ impl<'db> ClassLiteral<'db> {
db: &'db dyn Db,
specialization: Option<Specialization<'db>>,
field_policy: CodeGeneratorKind,
) -> FxOrderMap<Name, Field<'db>> {
let mut attributes = FxOrderMap::default();
) -> FxIndexMap<Name, Field<'db>> {
let mut attributes = FxIndexMap::default();
let class_body_scope = self.body_scope(db);
let table = place_table(db, class_body_scope);

View File

@@ -31,7 +31,7 @@ use crate::types::{
protocol_class::ProtocolClass,
};
use crate::types::{DataclassFlags, KnownInstanceType, MemberLookupPolicy, TypeVarInstance};
use crate::{Db, DisplaySettings, FxOrderMap, Program, declare_lint};
use crate::{Db, DisplaySettings, FxIndexMap, Program, declare_lint};
use itertools::Itertools;
use ruff_db::{
diagnostic::{Annotation, Diagnostic, Span, SubDiagnostic, SubDiagnosticSeverity},
@@ -3001,7 +3001,7 @@ pub(crate) fn report_instance_layout_conflict(
/// The inner data is an `IndexMap` to ensure that diagnostics regarding conflicting disjoint bases
/// are reported in a stable order.
#[derive(Debug, Default)]
pub(super) struct IncompatibleBases<'db>(FxOrderMap<DisjointBase<'db>, IncompatibleBaseInfo<'db>>);
pub(super) struct IncompatibleBases<'db>(FxIndexMap<DisjointBase<'db>, IncompatibleBaseInfo<'db>>);
impl<'db> IncompatibleBases<'db> {
pub(super) fn insert(

View File

@@ -2,7 +2,7 @@ use ruff_python_ast::name::Name;
use rustc_hash::FxHashMap;
use crate::{
Db, FxOrderMap,
Db, FxIndexMap,
place::{Place, PlaceAndQualifiers, place_from_bindings, place_from_declarations},
semantic_index::{place_table, use_def_map},
types::{
@@ -13,7 +13,7 @@ use crate::{
#[derive(Debug, PartialEq, Eq, salsa::Update)]
pub(crate) struct EnumMetadata<'db> {
pub(crate) members: FxOrderMap<Name, Type<'db>>,
pub(crate) members: FxIndexMap<Name, Type<'db>>,
pub(crate) aliases: FxHashMap<Name, Name>,
}
@@ -22,7 +22,7 @@ impl get_size2::GetSize for EnumMetadata<'_> {}
impl EnumMetadata<'_> {
fn empty() -> Self {
EnumMetadata {
members: FxOrderMap::default(),
members: FxIndexMap::default(),
aliases: FxHashMap::default(),
}
}
@@ -253,7 +253,7 @@ pub(crate) fn enum_metadata<'db>(
Some((name.clone(), value_ty))
})
.collect::<FxOrderMap<_, _>>();
.collect::<FxIndexMap<_, _>>();
if members.is_empty() {
// Enum subclasses without members are not considered enums.

View File

@@ -1083,9 +1083,13 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
&mut self.inner_expression_inference_state,
InnerExpressionInferenceState::Get,
);
let union = union.map(self.db(), |element| {
self.infer_subscript_type_expression(subscript, *element)
});
let union = union
.elements(self.db())
.iter()
.fold(UnionBuilder::new(self.db()), |builder, elem| {
builder.add(self.infer_subscript_type_expression(subscript, *elem))
})
.build();
self.inner_expression_inference_state = previous_slice_inference_state;
union
}

View File

@@ -926,7 +926,10 @@ impl<'db, 'ast> NarrowingConstraintsBuilder<'db, 'ast> {
.build();
// Keep order: first literal complement, then broader arms.
let result = UnionType::from_elements(self.db, [narrowed_single, rest_union]);
let result = UnionBuilder::new(self.db)
.add(narrowed_single)
.add(rest_union)
.build();
Some(result)
} else {
None