fix panic

This commit is contained in:
Alex Waygood 2025-08-14 21:54:14 +01:00
parent 5b0a93b3c0
commit 7c34ed3374
2 changed files with 22 additions and 6 deletions

View File

@ -133,6 +133,17 @@ def _[T](x: A | B):
reveal_type(x) # revealed: A[int] | B
```
## Narrowing for tuple
An early version of <https://github.com/astral-sh/ruff/pull/19920> caused us to crash on this:
```py
def _(val):
if type(val) is tuple:
# TODO: better would be `Unknown & tuple[object, ...]`
reveal_type(val) # revealed: Unknown & tuple[Unknown, ...]
```
## Limitations
```py

View File

@ -353,12 +353,17 @@ impl<'db> GenericContext<'db> {
db: &'db dyn Db,
types: Box<[Type<'db>]>,
) -> Specialization<'db> {
assert_eq!(self.variables(db).len(), types.len());
debug_assert!(
matches!(self.inner(db), GenericContextInner::NonTuple(_)),
"Should never call `GenericContext::specialize` on a tuple context"
);
Specialization::new(db, self, SpecializationInner::NonTuple(types))
match self.inner(db) {
GenericContextInner::Tuple { .. } => {
assert_eq!(types.len(), 1);
let tuple = TupleType::homogeneous(db, types[0]);
Specialization::new(db, self, SpecializationInner::Tuple(tuple))
}
GenericContextInner::NonTuple(variables) => {
assert_eq!(variables.len(), types.len());
Specialization::new(db, self, SpecializationInner::NonTuple(types))
}
}
}
/// Creates a specialization of this generic context for the `tuple` class.