[ty] hotfix panic in semantic tokens (#21632)

Fixes https://github.com/astral-sh/ty/issues/1637
This commit is contained in:
Aria Desires 2025-11-25 17:09:46 -05:00 committed by GitHub
parent 81c97e9e94
commit 5364256190
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View File

@ -1579,6 +1579,8 @@ z = "int"
w1: "int | str" = "hello"
w2: "int | sr" = "hello"
w3: "int | " = "hello"
w4: "float"
w5: "float
"#,
);
@ -1604,6 +1606,10 @@ w3: "int | " = "hello"
"w3" @ 86..88: Variable [definition]
"\"int | \"" @ 90..98: String
"\"hello\"" @ 101..108: String
"w4" @ 109..111: Variable [definition]
"float" @ 114..119: Class
"w5" @ 121..123: Variable [definition]
"float" @ 126..131: Class
"#);
}

View File

@ -408,7 +408,13 @@ pub trait HasDefinition {
impl HasType for ast::ExprRef<'_> {
fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
let index = semantic_index(model.db, model.file);
let file_scope = index.expression_scope_id(&model.expr_ref_in_ast(*self));
// TODO(#1637): semantic tokens is making this crash even with
// `try_expr_ref_in_ast` guarding this, for now just use `try_expression_scope_id`.
// The problematic input is `x: "float` (with a dangling quote). I imagine the issue
// is we're too eagerly setting `is_string_annotation` in inference.
let Some(file_scope) = index.try_expression_scope_id(&model.expr_ref_in_ast(*self)) else {
return Type::unknown();
};
let scope = file_scope.to_scope_id(model.db, model.file);
infer_scope_types(model.db, scope).expression_type(*self)