Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/narrow.rs (Rust)

Code before:
    })
}

/// Check that all tuple elements at the given index have literal types.
///
/// For equality narrowing to be safe, we need to ensure that the element types
/// at the discriminating index are literals (which have well-defined equality).
/// Non-literal types (like `str` or `int`) could have subclasses that override
/// `__eq__` in unexpected ways.
fn all_matching_tuple_elements_have_literal_types<'db>(
    db: &'db dyn Db,
    union: UnionType<'db>,
    index: i32,
) -> bool {
    union.elements(db).iter().all(|elem| {
        elem.as_nominal_instance()
            .and_then(|inst| inst.tuple_spec(db))
            .and_then(|spec| spec.py_index(db, index).ok())
            .is_none_or(is_supported_tag_literal)
    })
}

/// Builder for computing the conservative set of places that could possibly be narrowed.
///
/// This mirrors the structure of `NarrowingConstraintsBuilder` but only computes which places
/// *could* be narrowed, without performing full type inference to determine the actual constraints.
pub(crate) struct PossiblyNarrowedPlacesBuilder<'db, 'a> {
    db: &'db dyn Db,
    places: &'a PlaceTableBuilder,
}

impl<'db, 'a> PossiblyNarrowedPlacesBuilder<'db, 'a> {
    pub(crate) fn new(db: &'db dyn Db, places: &'a PlaceTableBuilder) -> Self {
        Self { db, places }
    }

    /// Compute possibly narrowed places for an expression predicate.
    pub(crate) fn expression(self, expr: &ast::Expr) -> PossiblyNarrowedPlaces {
        self.expression_node(expr)
    }

    /// Compute possibly narrowed places for a pattern predicate.
    pub(crate) fn pattern(
        self,
        pattern: PatternPredicate<'db>,
        module: &ParsedModuleRef,
    ) -> PossiblyNarrowedPlaces {
        self.pattern_kind(pattern.kind(self.db), pattern.subject(self.db), module)
    }

    fn expression_node(&self, expr: &ast::Expr) -> PossiblyNarrowedPlaces {

Review comment (suggestion):
This does not perform *any* type inference, so maybe strengthen this statement?
```suggestion
/// *could* be narrowed, without performing type inference to determine the actual constraints.
```

Code after:
    })
}

/// Check that all tuple elements at the given index have literal types.
///
/// For equality narrowing to be safe, we need to ensure that the element types
/// at the discriminating index are literals (which have well-defined equality).
/// Non-literal types (like `str` or `int`) could have subclasses that override
/// `__eq__` in unexpected ways.
fn all_matching_tuple_elements_have_literal_types<'db>(
    db: &'db dyn Db,
    union: UnionType<'db>,
    index: i32,
) -> bool {
    union.elements(db).iter().all(|elem| {
        elem.as_nominal_instance()
            .and_then(|inst| inst.tuple_spec(db))
            .and_then(|spec| spec.py_index(db, index).ok())
            .is_none_or(is_supported_tag_literal)
    })
}

/// Builder for computing the conservative set of places that could possibly be narrowed.
///
/// This mirrors the structure of `NarrowingConstraintsBuilder` but only computes which places
/// *could* be narrowed, without performing type inference to determine the actual constraints.
pub(crate) struct PossiblyNarrowedPlacesBuilder<'db, 'a> {
    db: &'db dyn Db,
    places: &'a PlaceTableBuilder,
}

impl<'db, 'a> PossiblyNarrowedPlacesBuilder<'db, 'a> {
    pub(crate) fn new(db: &'db dyn Db, places: &'a PlaceTableBuilder) -> Self {
        Self { db, places }
    }

    /// Compute possibly narrowed places for an expression predicate.
    pub(crate) fn expression(self, expr: &ast::Expr) -> PossiblyNarrowedPlaces {
        self.expression_node(expr)
    }

    /// Compute possibly narrowed places for a pattern predicate.
    pub(crate) fn pattern(
        self,
        pattern: PatternPredicate<'db>,
        module: &ParsedModuleRef,
    ) -> PossiblyNarrowedPlaces {
        self.pattern_kind(pattern.kind(self.db), pattern.subject(self.db), module)
    }

    fn expression_node(&self, expr: &ast::Expr) -> PossiblyNarrowedPlaces {