Skip to content

Commit ef2bdec

Browse files
fix some
1 parent 0dfb54c commit ef2bdec

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

pyrefly/lib/binding/bindings.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -893,19 +893,31 @@ impl<'a> BindingsBuilder<'a> {
893893
idx: Idx<Key>,
894894
style: FlowStyle,
895895
) -> Option<Idx<KeyAnnotation>> {
896-
let name = Hashed::new(name);
897-
let write_info = self
896+
let mut hashed_name = Hashed::new(name);
897+
let mut write_info = self
898898
.scopes
899-
.define_in_current_flow(name, idx, style)
900-
.unwrap_or_else(|| {
901-
panic!(
902-
"Name `{name}` not found in static scope of module `{}`.",
903-
self.module_info.name(),
904-
)
905-
});
899+
.define_in_current_flow(hashed_name.clone(), idx, style.clone());
900+
if write_info.is_none()
901+
&& self.errors_suppressed()
902+
&& self.should_bind_unreachable_branches()
903+
{
904+
let key_range = self.table.types.0.idx_to_key(idx).range();
905+
self.scopes.add_synthetic_definition(name, key_range);
906+
// Recreate the hash since it borrows `name` by reference and we just mutated state
907+
hashed_name = Hashed::new(name);
908+
write_info = self
909+
.scopes
910+
.define_in_current_flow(hashed_name.clone(), idx, style);
911+
}
912+
let write_info = write_info.unwrap_or_else(|| {
913+
panic!(
914+
"Name `{name}` not found in static scope of module `{}`.",
915+
self.module_info.name(),
916+
)
917+
});
906918
if let Some(range) = write_info.anywhere_range {
907919
self.table
908-
.record_bind_in_anywhere(name.into_key().clone(), range, idx);
920+
.record_bind_in_anywhere(hashed_name.into_key().clone(), range, idx);
909921
}
910922
write_info.annotation
911923
}

pyrefly/lib/binding/scope.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,22 @@ impl Scopes {
15111511
self.current_mut().stat.expr_lvalue(x);
15121512
}
15131513

1514+
/// Synthesize a static definition entry for `name` in the current scope if it
1515+
/// is missing. This is used when we deliberately analyze unreachable code for
1516+
/// IDE metadata; those code paths may not have been included in the up-front
1517+
/// static scan, so we add a lightweight placeholder on demand.
1518+
pub fn add_synthetic_definition(&mut self, name: &Name, range: TextRange) {
1519+
let hashed_ref = Hashed::new(name);
1520+
if self.current().stat.0.get_hashed(hashed_ref).is_some() {
1521+
return;
1522+
}
1523+
self.current_mut().stat.upsert(
1524+
Hashed::new(name.clone()),
1525+
range,
1526+
StaticStyle::SingleDef(None),
1527+
);
1528+
}
1529+
15141530
/// Add a loop exit point to the current innermost loop with the current flow.
15151531
///
15161532
/// Return a bool indicating whether we were in a loop (if we weren't, we do nothing).

pyrefly/lib/test/lsp/definition.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,15 @@ if not TYPE_CHECKING:
9595
# main.py
9696
5 | x = 1
9797
^
98-
Definition Result: None
98+
Definition Result:
99+
5 | x = 1
100+
^
99101
100102
7 | y = x
101103
^
102-
Definition Result: None
104+
Definition Result:
105+
5 | x = 1
106+
^
103107
"#
104108
.trim(),
105109
report.trim(),

pyrefly/lib/test/lsp/hover_type.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,35 +289,35 @@ if False:
289289
# main.py
290290
3 | def f():
291291
^
292-
Hover Result: None
292+
Hover Result: `() -> None`
293293
294294
7 | x = 3
295295
^
296-
Hover Result: None
296+
Hover Result: `Literal[3]`
297297
298298
9 | x
299299
^
300-
Hover Result: None
300+
Hover Result: `Literal[3]`
301301
302302
11 | f
303303
^
304-
Hover Result: None
304+
Hover Result: `() -> None`
305305
306306
14 | def f():
307307
^
308308
Hover Result: None
309309
310310
18 | x = 3
311311
^
312-
Hover Result: None
312+
Hover Result: `Literal[3]`
313313
314314
20 | x
315315
^
316-
Hover Result: None
316+
Hover Result: `Literal[3]`
317317
318318
22 | f
319319
^
320-
Hover Result: None
320+
Hover Result: `() -> None`
321321
"#
322322
.trim(),
323323
report.trim(),

0 commit comments

Comments
 (0)