Skip to content

Commit 11db567

Browse files
sharkdpBurntSushi
andauthored
[ty] ty_ide: Hotfix for expression_scope_id panics (astral-sh#18455)
## Summary Implement a hotfix for the playground/LSP crashes related to missing `expression_scope_id`s. relates to: astral-sh/ty#572 ## Test Plan * Regression tests from astral-sh#18441 * Ran the playground locally to check if panics occur / completions still work. --------- Co-authored-by: Andrew Gallant <andrew@astral.sh>
1 parent 9f8c3de commit 11db567

4 files changed

Lines changed: 179 additions & 4 deletions

File tree

_typos.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ extend-exclude = [
44
"crates/ty_vendored/vendor/**/*",
55
"**/resources/**/*",
66
"**/snapshots/**/*",
7+
# Completion tests tend to have a lot of incomplete
8+
# words naturally. It's annoying to have to make all
9+
# of them actually words. So just ignore typos here.
10+
"crates/ty_ide/src/completion.rs",
711
]
812

913
[default.extend-words]

crates/ty_ide/src/completion.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,162 @@ print(f\"{some<CURSOR>
861861
");
862862
}
863863

864+
// Ref: https://github.com/astral-sh/ty/issues/572
865+
#[test]
866+
fn scope_id_missing_function_identifier1() {
867+
let test = cursor_test(
868+
"\
869+
def m<CURSOR>
870+
",
871+
);
872+
873+
assert_snapshot!(test.completions(), @"<No completions found>");
874+
}
875+
876+
// Ref: https://github.com/astral-sh/ty/issues/572
877+
#[test]
878+
fn scope_id_missing_function_identifier2() {
879+
let test = cursor_test(
880+
"\
881+
def m<CURSOR>(): pass
882+
",
883+
);
884+
885+
assert_snapshot!(test.completions(), @"<No completions found>");
886+
}
887+
888+
// Ref: https://github.com/astral-sh/ty/issues/572
889+
#[test]
890+
fn fscope_id_missing_function_identifier3() {
891+
let test = cursor_test(
892+
"\
893+
def m(): pass
894+
<CURSOR>
895+
",
896+
);
897+
898+
assert_snapshot!(test.completions(), @r"
899+
m
900+
");
901+
}
902+
903+
// Ref: https://github.com/astral-sh/ty/issues/572
904+
#[test]
905+
fn scope_id_missing_class_identifier1() {
906+
let test = cursor_test(
907+
"\
908+
class M<CURSOR>
909+
",
910+
);
911+
912+
assert_snapshot!(test.completions(), @"<No completions found>");
913+
}
914+
915+
// Ref: https://github.com/astral-sh/ty/issues/572
916+
#[test]
917+
fn scope_id_missing_type_alias1() {
918+
let test = cursor_test(
919+
"\
920+
Fo<CURSOR> = float
921+
",
922+
);
923+
924+
assert_snapshot!(test.completions(), @r"
925+
Fo
926+
float
927+
");
928+
}
929+
930+
// Ref: https://github.com/astral-sh/ty/issues/572
931+
#[test]
932+
fn scope_id_missing_import1() {
933+
let test = cursor_test(
934+
"\
935+
import fo<CURSOR>
936+
",
937+
);
938+
939+
assert_snapshot!(test.completions(), @"<No completions found>");
940+
}
941+
942+
// Ref: https://github.com/astral-sh/ty/issues/572
943+
#[test]
944+
fn scope_id_missing_import2() {
945+
let test = cursor_test(
946+
"\
947+
import foo as ba<CURSOR>
948+
",
949+
);
950+
951+
assert_snapshot!(test.completions(), @"<No completions found>");
952+
}
953+
954+
// Ref: https://github.com/astral-sh/ty/issues/572
955+
#[test]
956+
fn scope_id_missing_from_import1() {
957+
let test = cursor_test(
958+
"\
959+
from fo<CURSOR> import wat
960+
",
961+
);
962+
963+
assert_snapshot!(test.completions(), @"<No completions found>");
964+
}
965+
966+
// Ref: https://github.com/astral-sh/ty/issues/572
967+
#[test]
968+
fn scope_id_missing_from_import2() {
969+
let test = cursor_test(
970+
"\
971+
from foo import wa<CURSOR>
972+
",
973+
);
974+
975+
assert_snapshot!(test.completions(), @"<No completions found>");
976+
}
977+
978+
// Ref: https://github.com/astral-sh/ty/issues/572
979+
#[test]
980+
fn scope_id_missing_from_import3() {
981+
let test = cursor_test(
982+
"\
983+
from foo import wat as ba<CURSOR>
984+
",
985+
);
986+
987+
assert_snapshot!(test.completions(), @"<No completions found>");
988+
}
989+
990+
// Ref: https://github.com/astral-sh/ty/issues/572
991+
#[test]
992+
fn scope_id_missing_try_except1() {
993+
let test = cursor_test(
994+
"\
995+
try:
996+
pass
997+
except Type<CURSOR>:
998+
pass
999+
",
1000+
);
1001+
1002+
assert_snapshot!(test.completions(), @r"
1003+
Type
1004+
");
1005+
}
1006+
1007+
// Ref: https://github.com/astral-sh/ty/issues/572
1008+
#[test]
1009+
fn scope_id_missing_global1() {
1010+
let test = cursor_test(
1011+
"\
1012+
def _():
1013+
global fo<CURSOR>
1014+
",
1015+
);
1016+
1017+
assert_snapshot!(test.completions(), @"<No completions found>");
1018+
}
1019+
8641020
impl CursorTest {
8651021
fn completions(&self) -> String {
8661022
let completions = completion(&self.db, self.file, self.cursor_offset);

crates/ty_python_semantic/src/semantic_index.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ impl<'db> SemanticIndex<'db> {
259259
self.scopes_by_expression[&expression.into()]
260260
}
261261

262+
/// Returns the ID of the `expression`'s enclosing scope.
263+
pub(crate) fn try_expression_scope_id(
264+
&self,
265+
expression: impl Into<ExpressionNodeKey>,
266+
) -> Option<FileScopeId> {
267+
self.scopes_by_expression.get(&expression.into()).copied()
268+
}
269+
262270
/// Returns the [`Scope`] of the `expression`'s enclosing scope.
263271
#[allow(unused)]
264272
#[track_caller]

crates/ty_python_semantic/src/semantic_model.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,22 @@ impl<'db> SemanticModel<'db> {
4747
/// scope of this model's `File` are returned.
4848
pub fn completions(&self, node: ast::AnyNodeRef<'_>) -> Vec<Name> {
4949
let index = semantic_index(self.db, self.file);
50-
let file_scope = match node {
51-
ast::AnyNodeRef::Identifier(identifier) => index.expression_scope_id(identifier),
50+
51+
// TODO: We currently use `try_expression_scope_id` here as a hotfix for [1].
52+
// Revert this to use `expression_scope_id` once a proper fix is in place.
53+
//
54+
// [1] https://github.com/astral-sh/ty/issues/572
55+
let Some(file_scope) = (match node {
56+
ast::AnyNodeRef::Identifier(identifier) => index.try_expression_scope_id(identifier),
5257
node => match node.as_expr_ref() {
5358
// If we couldn't identify a specific
5459
// expression that we're in, then just
5560
// fall back to the global scope.
56-
None => FileScopeId::global(),
57-
Some(expr) => index.expression_scope_id(expr),
61+
None => Some(FileScopeId::global()),
62+
Some(expr) => index.try_expression_scope_id(expr),
5863
},
64+
}) else {
65+
return vec![];
5966
};
6067
let mut symbols = vec![];
6168
for (file_scope, _) in index.ancestor_scopes(file_scope) {

0 commit comments

Comments
 (0)