Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
40cbc39
Emit TO_BOOL before conditional jumps, fix class/module prologue
youknowone Mar 20, 2026
f52276e
Emit MAKE_CELL and COPY_FREE_VARS before RESUME
youknowone Mar 20, 2026
7a9c29d
Emit __static_attributes__ at end of class bodies
youknowone Mar 20, 2026
f8f7fbf
Remove expectedFailure from DictProxyTests iter tests
youknowone Mar 20, 2026
e3fa83e
Use 1-based stack indexing for LIST_EXTEND, SET_UPDATE, etc.
youknowone Mar 20, 2026
18c23e5
Use plain LOAD_ATTR + PUSH_NULL for calls on imported names
youknowone Mar 20, 2026
b2995bc
Duplicate return-None epilogue for fall-through blocks
youknowone Mar 20, 2026
35ac45d
Run cargo fmt on ir.rs
youknowone Mar 20, 2026
1092110
Remove expectedFailure from test_intrinsic_1 in test_dis
youknowone Mar 20, 2026
cc23051
Emit TO_BOOL before conditional jumps for all expressions including C…
youknowone Mar 20, 2026
643861b
Add __classdict__ cell for classes with function definitions
youknowone Mar 21, 2026
a4753e8
Emit __classdictcell__ store in class body epilogue
youknowone Mar 21, 2026
50eaae4
Always run DCE to remove dead code after terminal instructions
youknowone Mar 21, 2026
fdcccad
Restrict LOAD_ATTR plain mode to module/class scope imports
youknowone Mar 21, 2026
83a548e
Eliminate unreachable blocks after jump normalization
youknowone Mar 21, 2026
6a592dd
Fold BUILD_TUPLE 0 into LOAD_CONST empty tuple
youknowone Mar 21, 2026
1d83df2
Handle __classcell__ and __classdictcell__ in type.__new__
youknowone Mar 21, 2026
e1bf8d1
Revert __classdict__ cell and __classdictcell__ changes
youknowone Mar 21, 2026
add881c
Fix unreachable block elimination with fixpoint reachability
youknowone Mar 21, 2026
3181725
Check enclosing scopes for IMPORTED flag in LOAD_ATTR mode
youknowone Mar 22, 2026
c345999
Add __classdict__ cell for classes with function definitions
youknowone Mar 22, 2026
11b4b18
Store __classdictcell__ in class body epilogue
youknowone Mar 22, 2026
0793cb2
Fix clippy collapsible_if warnings and cargo fmt
youknowone Mar 22, 2026
d29b2a9
Revert __classdict__ and __classdictcell__ changes (cause import fail…
youknowone Mar 22, 2026
d5afad9
Revert type.__new__ __classcell__ removal and __classdictcell__ handling
youknowone Mar 22, 2026
c921c8b
Re-add __classdict__ cell and __classdictcell__ store
youknowone Mar 22, 2026
60103dc
Reorder MakeFunctionFlag to match CPython's SET_FUNCTION_ATTRIBUTE
youknowone Mar 22, 2026
560d8ae
Use CPython-compatible power-of-two encoding for SET_FUNCTION_ATTRIBUTE
youknowone Mar 22, 2026
16ce7fb
Remove expectedFailure from test_elim_jump_after_return1 and test_no_…
youknowone Mar 22, 2026
9820993
Remove __classcell__ and __classdictcell__ from class dict in type.__…
youknowone Mar 22, 2026
35a8f7e
Remove expectedFailure from test___classcell___expected_behaviour, ca…
youknowone Mar 22, 2026
e0c7326
Handle MakeCell and CopyFreeVars as no-ops in JIT
youknowone Mar 22, 2026
c958739
Remove expectedFailure from test_load_fast_known_simple
youknowone Mar 23, 2026
926a984
Restore expectedFailure for test_load_fast_known_simple
youknowone Mar 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Emit TO_BOOL before conditional jumps, fix class/module prologue
- Emit TO_BOOL before POP_JUMP_IF_TRUE/FALSE in the general case
  of compile_jump_if (Compare expressions excluded since they
  already produce a bool)
- Module-level __doc__: use STORE_NAME instead of STORE_GLOBAL
- Class body __module__: use LOAD_NAME instead of LOAD_GLOBAL
- Class body: store __firstlineno__ before __doc__
  • Loading branch information
youknowone committed Mar 22, 2026
commit 40cbc3963ce58d8c1888e9f2b45706f6421b91fc
26 changes: 15 additions & 11 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ impl Compiler {
value: value.into(),
});
let doc = self.name("__doc__");
emit!(self, Instruction::StoreGlobal { namei: doc })
emit!(self, Instruction::StoreName { namei: doc })
}

// Handle annotations based on future_annotations flag
Expand Down Expand Up @@ -4561,9 +4561,9 @@ impl Compiler {
// 2. Set up class namespace
let (doc_str, body) = split_doc(body, &self.opts);

// Load (global) __name__ and store as __module__
// Load __name__ and store as __module__
let dunder_name = self.name("__name__");
self.emit_load_global(dunder_name, false);
emit!(self, Instruction::LoadName { namei: dunder_name });
let dunder_module = self.name("__module__");
emit!(
self,
Expand All @@ -4584,14 +4584,7 @@ impl Compiler {
}
);

// Store __doc__ only if there's an explicit docstring
if let Some(doc) = doc_str {
self.emit_load_const(ConstantData::Str { value: doc.into() });
let doc_name = self.name("__doc__");
emit!(self, Instruction::StoreName { namei: doc_name });
}

// Store __firstlineno__ (new in Python 3.12+)
// Store __firstlineno__ before __doc__
self.emit_load_const(ConstantData::Integer {
value: BigInt::from(firstlineno),
});
Expand All @@ -4603,6 +4596,13 @@ impl Compiler {
}
);

// Store __doc__ only if there's an explicit docstring
if let Some(doc) = doc_str {
self.emit_load_const(ConstantData::Str { value: doc.into() });
let doc_name = self.name("__doc__");
emit!(self, Instruction::StoreName { namei: doc_name });
}

// Set __type_params__ if we have type parameters
if type_params.is_some() {
// Load .type_params from enclosing scope
Expand Down Expand Up @@ -6742,6 +6742,10 @@ impl Compiler {
_ => {
// Fall back case which always will work!
self.compile_expression(expression)?;
// Compare already produces a bool; everything else needs TO_BOOL
if !matches!(expression, ast::Expr::Compare(_)) {
emit!(self, Instruction::ToBool);
}
Comment on lines +6804 to +6807
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

The Compare rationale is inaccurate.

Rich comparisons can return arbitrary objects; the reason this fast path is safe is the jump opcode’s truthiness handling, not that Compare itself always yields a bool. Please reword the comment so future refactors do not depend on the wrong invariant.

Suggested comment update
-                // Compare already produces a bool; everything else needs TO_BOOL
+                // Rich comparisons may return non-bool objects; skipping TO_BOOL
+                // here is only correct because PopJumpIf* performs truthiness
+                // conversion itself.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Compare already produces a bool; everything else needs TO_BOOL
if !matches!(expression, ast::Expr::Compare(_)) {
emit!(self, Instruction::ToBool);
}
// Rich comparisons may return non-bool objects; skipping TO_BOOL
// here is only correct because PopJumpIf* performs truthiness
// conversion itself.
if !matches!(expression, ast::Expr::Compare(_)) {
emit!(self, Instruction::ToBool);
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/codegen/src/compile.rs` around lines 6732 - 6735, The comment above
the ToBool emission is misleading because ast::Expr::Compare may produce
non-bool values; update the comment near the if !matches!(expression,
ast::Expr::Compare) check to state that the Compare fast path is safe due to the
jump opcode’s truthiness handling (not because Compare always yields a bool),
and clarify that everything else needs Instruction::ToBool before branching;
reference the emit!(self, Instruction::ToBool) call and the ast::Expr::Compare
pattern in the comment.

if condition {
emit!(
self,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.