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

Code before:

    if any_has_method {
        let mut builder = IntersectionBuilder::new(db);
        let mut filtered_errors = Vec::new();

        for error in errors {
            if error.any_method_available() {
                builder = builder.add_positive(error.result_type());
                filtered_errors.extend(
                    error
                        .into_errors()
                        .into_iter()
                        .filter(SubscriptErrorKind::method_available),
                );
            }
        }

        return Err(SubscriptError::with_errors(
            builder.build(),
            filtered_errors,
        ));
    }

    // No element has the method. Combine all errors.
    let Some(first) = errors.first() else {
        unreachable!("intersection should have at least one positive element")
    };

    let result_ty = first.result_type();
    let all_errors = errors
        .into_iter()
        .flat_map(SubscriptError::into_errors)
        .collect();

    Err(SubscriptError::with_errors(result_ty, all_errors))
}

impl<'db> Type<'db> {
    pub(super) fn subscript(
        self,
        db: &'db dyn Db,
        slice_ty: Type<'db>,
        expr_context: ast::ExprContext,
    ) -> Result<Type<'db>, SubscriptError<'db>> {
        let value_ty = self;

        let inferred = match (value_ty, slice_ty) {
            (Type::Dynamic(_) | Type::Never, _) => Some(Ok(value_ty)),

            (Type::TypeAlias(alias), _) => {
                Some(alias.value_type(db).subscript(db, slice_ty, expr_context))

Review comment (suggestion):
This isn't true: it's very possible for an intersection to have 0 positive elements if it has >=1 negative elements (in which case, for our purposes here we should just treat it the same as `object`). This is causing at least one of the ecosystem panics, and I suspect all of them 

Code after:
    // (even if the call failed). If so, filter out `NotSubscriptable` errors
    // for elements that lack the method.
    let any_has_method = errors.iter().any(SubscriptError::any_method_available);

    let mut builder = IntersectionBuilder::new(db);
    let mut collected_errors = Vec::new();

    for error in errors {
        if !any_has_method || error.any_method_available() {
            builder = builder.add_positive(error.result_type());
            let error_iter = error.into_errors().into_iter();
            if any_has_method {
                collected_errors.extend(error_iter.filter(SubscriptErrorKind::method_available));
            } else {
                collected_errors.extend(error_iter);
            }
        }
    }

    Err(SubscriptError::with_errors(
        builder.build(),
        collected_errors,
    ))
}

impl<'db> Type<'db> {
    pub(super) fn subscript(
        self,
        db: &'db dyn Db,
        slice_ty: Type<'db>,
        expr_context: ast::ExprContext,
    ) -> Result<Type<'db>, SubscriptError<'db>> {
        let value_ty = self;

        let inferred = match (value_ty, slice_ty) {
            (Type::Dynamic(_) | Type::Never, _) => Some(Ok(value_ty)),

            (Type::TypeAlias(alias), _) => {
                Some(alias.value_type(db).subscript(db, slice_ty, expr_context))
            }

            (_, Type::TypeAlias(alias)) => {
                Some(value_ty.subscript(db, alias.value_type(db), expr_context))
            }

            (Type::Union(union), _) => Some(map_union_subscript(db, union, |element| {
                element.subscript(db, slice_ty, expr_context)
            })),

            (_, Type::Union(union)) => Some(map_union_subscript(db, union, |element| {
                value_ty.subscript(db, element, expr_context)