Skip to content

Commit df2b98c

Browse files
fix some
1 parent b8128e1 commit df2b98c

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
@@ -876,19 +876,31 @@ impl<'a> BindingsBuilder<'a> {
876876
idx: Idx<Key>,
877877
style: FlowStyle,
878878
) -> Option<Idx<KeyAnnotation>> {
879-
let name = Hashed::new(name);
880-
let write_info = self
879+
let mut hashed_name = Hashed::new(name);
880+
let mut write_info = self
881881
.scopes
882-
.define_in_current_flow(name, idx, style)
883-
.unwrap_or_else(|| {
884-
panic!(
885-
"Name `{name}` not found in static scope of module `{}`.",
886-
self.module_info.name(),
887-
)
888-
});
882+
.define_in_current_flow(hashed_name.clone(), idx, style.clone());
883+
if write_info.is_none()
884+
&& self.errors_suppressed()
885+
&& self.should_bind_unreachable_branches()
886+
{
887+
let key_range = self.table.types.0.idx_to_key(idx).range();
888+
self.scopes.add_synthetic_definition(name, key_range);
889+
// Recreate the hash since it borrows `name` by reference and we just mutated state
890+
hashed_name = Hashed::new(name);
891+
write_info = self
892+
.scopes
893+
.define_in_current_flow(hashed_name.clone(), idx, style);
894+
}
895+
let write_info = write_info.unwrap_or_else(|| {
896+
panic!(
897+
"Name `{name}` not found in static scope of module `{}`.",
898+
self.module_info.name(),
899+
)
900+
});
889901
if let Some(range) = write_info.anywhere_range {
890902
self.table
891-
.record_bind_in_anywhere(name.into_key().clone(), range, idx);
903+
.record_bind_in_anywhere(hashed_name.into_key().clone(), range, idx);
892904
}
893905
write_info.annotation
894906
}

pyrefly/lib/binding/scope.rs

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

1421+
/// Synthesize a static definition entry for `name` in the current scope if it
1422+
/// is missing. This is used when we deliberately analyze unreachable code for
1423+
/// IDE metadata; those code paths may not have been included in the up-front
1424+
/// static scan, so we add a lightweight placeholder on demand.
1425+
pub fn add_synthetic_definition(&mut self, name: &Name, range: TextRange) {
1426+
let hashed_ref = Hashed::new(name);
1427+
if self.current().stat.0.get_hashed(hashed_ref).is_some() {
1428+
return;
1429+
}
1430+
self.current_mut().stat.upsert(
1431+
Hashed::new(name.clone()),
1432+
range,
1433+
StaticStyle::SingleDef(None),
1434+
);
1435+
}
1436+
14211437
/// Add a loop exit point to the current innermost loop with the current flow.
14221438
///
14231439
/// 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)