From 536425619018d5531d7e5d5f66918735a8583724 Mon Sep 17 00:00:00 2001 From: Aria Desires Date: Tue, 25 Nov 2025 17:09:46 -0500 Subject: [PATCH] [ty] hotfix panic in semantic tokens (#21632) Fixes https://github.com/astral-sh/ty/issues/1637 --- crates/ty_ide/src/semantic_tokens.rs | 6 ++++++ crates/ty_python_semantic/src/semantic_model.rs | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/ty_ide/src/semantic_tokens.rs b/crates/ty_ide/src/semantic_tokens.rs index 25879fc746..0d05722bb0 100644 --- a/crates/ty_ide/src/semantic_tokens.rs +++ b/crates/ty_ide/src/semantic_tokens.rs @@ -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 "#); } diff --git a/crates/ty_python_semantic/src/semantic_model.rs b/crates/ty_python_semantic/src/semantic_model.rs index 0cbaa2bbae..24c3f09ecb 100644 --- a/crates/ty_python_semantic/src/semantic_model.rs +++ b/crates/ty_python_semantic/src/semantic_model.rs @@ -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)