Skip to content

Commit d23e92b

Browse files
committed
fixup
1 parent 0a1cb62 commit d23e92b

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

crates/codegen/src/compile.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,8 @@ impl Compiler {
10321032
}
10331033

10341034
/// Check if this is an inlined comprehension context (PEP 709).
1035-
/// Only inline in function-like scopes (fastlocals) — module/class
1036-
/// level uses STORE_NAME which is incompatible with LOAD_FAST_AND_CLEAR.
1035+
/// PEP 709: Inline comprehensions in function-like scopes.
1036+
/// TODO: Module/class scope inlining needs more work (Cell name resolution edge cases).
10371037
/// Generator expressions are never inlined.
10381038
fn is_inlined_comprehension_context(&self, comprehension_type: ComprehensionType) -> bool {
10391039
if comprehension_type == ComprehensionType::Generator {
@@ -9516,21 +9516,21 @@ impl Compiler {
95169516
Some('s') => {
95179517
chars.next();
95189518
segments.push(FormatSegment {
9519-
literal: std::mem::take(&mut current_literal),
9519+
literal: core::mem::take(&mut current_literal),
95209520
conversion: Some(oparg::ConvertValueOparg::Str),
95219521
});
95229522
}
95239523
Some('r') => {
95249524
chars.next();
95259525
segments.push(FormatSegment {
9526-
literal: std::mem::take(&mut current_literal),
9526+
literal: core::mem::take(&mut current_literal),
95279527
conversion: Some(oparg::ConvertValueOparg::Repr),
95289528
});
95299529
}
95309530
Some('a') => {
95319531
chars.next();
95329532
segments.push(FormatSegment {
9533-
literal: std::mem::take(&mut current_literal),
9533+
literal: core::mem::take(&mut current_literal),
95349534
conversion: Some(oparg::ConvertValueOparg::Ascii),
95359535
});
95369536
}

crates/codegen/src/ir.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ impl CodeInfo {
208208
// Peephole optimizer creates superinstructions matching CPython
209209
self.peephole_optimize();
210210

211-
// Always apply LOAD_FAST_BORROW optimization
211+
// insert_superinstructions (flowgraph.c): must run BEFORE optimize_load_fast
212+
self.combine_store_fast_load_fast();
213+
214+
// optimize_load_fast (flowgraph.c): LOAD_FAST → LOAD_FAST_BORROW
212215
self.optimize_load_fast_borrow();
213216

214217
// Post-codegen CFG analysis passes (flowgraph.c pipeline)
@@ -1473,14 +1476,9 @@ impl CodeInfo {
14731476

14741477
/// Optimize LOAD_FAST to LOAD_FAST_BORROW where safe.
14751478
///
1476-
/// A LOAD_FAST can be converted to LOAD_FAST_BORROW if its value is
1477-
/// consumed within the same basic block (not passed to another block).
1478-
/// This is a reference counting optimization in CPython; in RustPython
1479-
/// we implement it for bytecode compatibility.
1480-
/// Combine STORE_FAST + LOAD_FAST → STORE_FAST_LOAD_FAST.
1481-
/// Must run AFTER optimize_load_fast_borrow so that STORE_FAST + LOAD_FAST_BORROW
1482-
/// pairs are NOT combined (matching CPython's flowgraph.c behavior).
1483-
#[allow(dead_code)]
1479+
/// insert_superinstructions (flowgraph.c): Combine STORE_FAST + LOAD_FAST →
1480+
/// STORE_FAST_LOAD_FAST. Must run BEFORE optimize_load_fast_borrow so that
1481+
/// the borrow pass sees the combined instruction (matching flowgraph.c order).
14841482
fn combine_store_fast_load_fast(&mut self) {
14851483
for block in &mut self.blocks {
14861484
let mut i = 0;

crates/codegen/src/symboltable.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,14 +2170,24 @@ impl SymbolTableBuilder {
21702170
self.tables.last_mut().unwrap().is_generator = is_generator;
21712171

21722172
// PEP 709: Mark non-generator comprehensions for inlining.
2173-
// CPython inlines at ALL scopes (module, class, function, lambda).
2174-
// Only excluded: generator expressions, async comprehensions,
2173+
// Only in function-like scopes for now. Module/class scope inlining
2174+
// needs more work (Cell name resolution, __class__ handling).
2175+
// Also excluded: generator expressions, async comprehensions,
21752176
// and annotation scopes nested in classes (can_see_class_scope).
21762177
let element_has_await = expr_contains_await(elt1) || elt2.is_some_and(expr_contains_await);
21772178
if !is_generator && !has_async_gen && !element_has_await {
21782179
let parent = self.tables.iter().rev().nth(1);
21792180
let parent_can_see_class = parent.is_some_and(|t| t.can_see_class_scope);
2180-
if !parent_can_see_class {
2181+
let parent_is_func = parent.is_some_and(|t| {
2182+
matches!(
2183+
t.typ,
2184+
CompilerScope::Function
2185+
| CompilerScope::AsyncFunction
2186+
| CompilerScope::Lambda
2187+
| CompilerScope::Comprehension
2188+
)
2189+
});
2190+
if !parent_can_see_class && parent_is_func {
21812191
self.tables.last_mut().unwrap().comp_inlined = true;
21822192
}
21832193
}

crates/vm/src/stdlib/_symtable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ mod _symtable {
174174
.symtable
175175
.sub_tables
176176
.iter()
177+
.filter(|t| !t.comp_inlined)
177178
.map(|t| to_py_symbol_table(t.clone()).into_pyobject(vm))
178179
.collect();
179180
Ok(children)

0 commit comments

Comments
 (0)