Skip to content

Commit 13549d8

Browse files
committed
Address code review feedback
1 parent ddecd7f commit 13549d8

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

‎crates/codegen/src/compile.rs‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,7 +3258,17 @@ impl Compiler {
32583258
CompilerScope::Annotation | CompilerScope::TypeParams
32593259
) {
32603260
SymbolScope::GlobalImplicit
3261-
} else if name.starts_with('_') {
3261+
} else if matches!(
3262+
name.as_ref(),
3263+
"__name__"
3264+
| "__module__"
3265+
| "__qualname__"
3266+
| "__firstlineno__"
3267+
| "__doc__"
3268+
| "__static_attributes__"
3269+
| "__classdictcell__"
3270+
| "__classcell__"
3271+
) {
32623272
SymbolScope::Unknown
32633273
} else {
32643274
return Err(self.error(CodegenErrorType::SyntaxError(format!(
@@ -13984,6 +13994,23 @@ def f(buffer, pos, last_char):
1398413994
})
1398513995
}
1398613996

13997+
fn localsplus_name(code: &CodeObject, idx: usize) -> Option<&str> {
13998+
if idx < code.varnames.len() {
13999+
return Some(code.varnames[idx].as_str());
14000+
}
14001+
14002+
let mut extra_idx = idx - code.varnames.len();
14003+
for cellvar in &code.cellvars {
14004+
if !code.varnames.iter().any(|varname| varname == cellvar) {
14005+
if extra_idx == 0 {
14006+
return Some(cellvar.as_str());
14007+
}
14008+
extra_idx -= 1;
14009+
}
14010+
}
14011+
code.freevars.get(extra_idx).map(|name| name.as_str())
14012+
}
14013+
1398714014
fn has_common_constant(code: &CodeObject, expected: bytecode::CommonConstant) -> bool {
1398814015
code.instructions.iter().any(|unit| match unit.op {
1398914016
Instruction::LoadCommonConstant { idx } => {
@@ -20932,12 +20959,16 @@ def f():
2093220959
class_code.freevars
2093320960
);
2093420961
assert!(
20935-
class_code
20936-
.instructions
20937-
.iter()
20938-
.any(|unit| matches!(unit.op, Instruction::StoreDeref { .. })),
20939-
"CPython routes __firstlineno__ through name resolution and emits STORE_DEREF, got ops={:?}",
20940-
class_code.instructions
20962+
class_code.instructions.iter().any(|unit| match unit.op {
20963+
Instruction::StoreDeref { i } => {
20964+
let idx = i.get(OpArg::new(u32::from(u8::from(unit.arg)))).as_usize();
20965+
localsplus_name(class_code, idx) == Some("__firstlineno__")
20966+
}
20967+
_ => false,
20968+
}),
20969+
"CPython routes __firstlineno__ through name resolution and emits STORE_DEREF for __firstlineno__, got ops={:?} freevars={:?}",
20970+
class_code.instructions,
20971+
class_code.freevars
2094120972
);
2094220973
assert!(
2094320974
!class_code.instructions.iter().any(|unit| {

0 commit comments

Comments
 (0)