Skip to content

Commit 64e7e1a

Browse files
dcreagerAlexWaygoodcarljm
authored
[red-knot] Add Type::TypeVar variant (#17102)
This adds a new `Type` variant for holding an instance of a typevar inside of a generic function or class. We don't handle specializing the typevars yet, but this should implement most of the typing rules for inside the generic function/class, where we don't know yet which specific type the typevar will be specialized to. This PR does _not_ yet handle the constraint that multiple occurrences of the typevar must be specialized to the _same_ time. (There is an existing test case for this in `generics/functions.md` which is still marked as TODO.) --------- Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: Carl Meyer <[email protected]>
1 parent 45c4373 commit 64e7e1a

File tree

9 files changed

+990
-122
lines changed

9 files changed

+990
-122
lines changed

crates/red_knot_ide/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ impl HasNavigationTargets for Type<'_> {
162162
| Type::PropertyInstance(_)
163163
| Type::Tuple(_) => self.to_meta_type(db.upcast()).navigation_targets(db),
164164

165+
Type::TypeVar(var) => {
166+
let definition = var.definition(db);
167+
let full_range = definition.full_range(db.upcast());
168+
169+
NavigationTargets::single(NavigationTarget {
170+
file: full_range.file(),
171+
focus_range: definition.focus_range(db.upcast()).range(),
172+
full_range: full_range.range(),
173+
})
174+
}
175+
165176
Type::Intersection(intersection) => intersection.navigation_targets(db),
166177

167178
Type::Dynamic(_)

crates/red_knot_python_semantic/resources/mdtest/generics/functions.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ def good_return[T: int](x: T) -> T:
107107
return x
108108

109109
def bad_return[T: int](x: T) -> T:
110-
# TODO: error: int is not assignable to T
111-
# error: [unsupported-operator] "Operator `+` is unsupported between objects of type `T` and `Literal[1]`"
110+
# error: [invalid-return-type] "Object of type `int` is not assignable to return type `T`"
112111
return x + 1
113112
```
114113

0 commit comments

Comments
 (0)