Skip to content

Commit 11b9773

Browse files
committed
Fix module docstring global name resolution
Assisted-by: Claude Code:claude-opus-5-5
1 parent 3ab5c0c commit 11b9773

2 files changed

Lines changed: 121 additions & 2 deletions

File tree

‎crates/codegen/src/compile.rs‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,8 +2996,7 @@ impl<'warnings> Compiler<'warnings> {
29962996
self.emit_load_const(ConstantData::Str {
29972997
value: value.into(),
29982998
});
2999-
let doc = self.name("__doc__");
3000-
emit!(self, Instruction::StoreName { namei: doc });
2999+
self.store_name(&"__doc__".into())?;
30013000
self.set_no_location();
30023001
self.set_source_range(saved_range);
30033002
}
@@ -20054,6 +20053,54 @@ class C:
2005420053
}));
2005520054
}
2005620055

20056+
fn find_module_doc_store(code: &CodeObject) -> Option<&Instruction> {
20057+
code.instructions.iter().find_map(|unit| match unit.op {
20058+
Instruction::StoreName { namei } | Instruction::StoreGlobal { namei } => {
20059+
let idx = namei.get(OpArg::new(u32::from(u8::from(unit.arg))));
20060+
(code.names[usize::try_from(idx).unwrap()].as_str() == "__doc__")
20061+
.then_some(&unit.op)
20062+
}
20063+
_ => None,
20064+
})
20065+
}
20066+
20067+
#[test]
20068+
fn module_docstring_store_uses_global_when_nested_scope_declares_global() {
20069+
let code = compile_exec(
20070+
"\
20071+
\"module doc\"
20072+
def f():
20073+
global __doc__
20074+
",
20075+
);
20076+
20077+
let op = find_module_doc_store(&code).expect("missing module docstring store");
20078+
assert!(
20079+
matches!(op, Instruction::StoreGlobal { .. }),
20080+
"module docstring store should resolve through name resolution to StoreGlobal \
20081+
when a nested scope declares `global __doc__`, got instructions={:?}",
20082+
code.instructions
20083+
);
20084+
}
20085+
20086+
#[test]
20087+
fn module_docstring_store_stays_name_without_nested_global() {
20088+
let code = compile_exec(
20089+
"\
20090+
\"module doc\"
20091+
x = 1
20092+
",
20093+
);
20094+
20095+
let op = find_module_doc_store(&code).expect("missing module docstring store");
20096+
assert!(
20097+
matches!(op, Instruction::StoreName { .. }),
20098+
"module docstring store should remain StoreName without a nested \
20099+
`global __doc__` declaration, got instructions={:?}",
20100+
code.instructions
20101+
);
20102+
}
20103+
2005720104
#[test]
2005820105
fn conditional_return_epilogue_is_duplicated() {
2005920106
let code = compile_exec(
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Regression test: a module docstring must be stored through module name
2+
# resolution, so a nested `global __doc__` declaration makes the module body
3+
# emit STORE_GLOBAL (writing module globals) instead of STORE_NAME (writing
4+
# the provided locals mapping). Mirrors CPython codegen_nameop behavior.
5+
6+
import dis
7+
8+
9+
def opnames(code):
10+
return [instr.opname for instr in dis.get_instructions(code)]
11+
12+
13+
def run_split(src, optimize=0):
14+
code = compile(src, "<test>", "exec", optimize=optimize)
15+
g, loc = {}, {}
16+
exec(code, g, loc)
17+
return code, g, loc
18+
19+
20+
# A nested function declaring `global __doc__` makes the module docstring
21+
# store target module globals.
22+
src_global = '"module doc"\ndef f():\n global __doc__\n'
23+
code, g, loc = run_split(src_global)
24+
ops = opnames(code)
25+
assert "STORE_GLOBAL" in ops, ops
26+
assert "STORE_NAME" not in ops or "__doc__" not in [
27+
instr.argval for instr in dis.get_instructions(code) if instr.opname == "STORE_NAME"
28+
], ops
29+
assert g.get("__doc__") == "module doc", g
30+
assert "__doc__" not in loc, loc
31+
32+
# The declaration may be nested more than one scope deep.
33+
src_deep = '"module doc"\ndef f():\n def g():\n global __doc__\n'
34+
code, g, loc = run_split(src_deep)
35+
assert "STORE_GLOBAL" in opnames(code), opnames(code)
36+
assert g.get("__doc__") == "module doc", g
37+
assert "__doc__" not in loc, loc
38+
39+
# Control: without a nested global declaration the docstring stays a
40+
# STORE_NAME and lands in the provided locals mapping.
41+
src_plain = '"module doc"\nx = 1\n'
42+
code, g, loc = run_split(src_plain)
43+
doc_stores = [
44+
instr.opname
45+
for instr in dis.get_instructions(code)
46+
if instr.argval == "__doc__" and instr.opname.startswith("STORE_")
47+
]
48+
assert doc_stores == ["STORE_NAME"], doc_stores
49+
assert loc.get("__doc__") == "module doc", loc
50+
assert "__doc__" not in g, g
51+
52+
# optimize=1 keeps the docstring and the same global resolution.
53+
code, g, loc = run_split(src_global, optimize=1)
54+
assert "STORE_GLOBAL" in opnames(code), opnames(code)
55+
assert g.get("__doc__") == "module doc", g
56+
57+
# optimize=2 strips the docstring entirely: no __doc__ store in either dict.
58+
code, g, loc = run_split(src_global, optimize=2)
59+
assert not any(
60+
instr.argval == "__doc__" and instr.opname.startswith("STORE_")
61+
for instr in dis.get_instructions(code)
62+
), opnames(code)
63+
assert "__doc__" not in g and "__doc__" not in loc, (g, loc)
64+
65+
# A class docstring still belongs to the class namespace even when a nested
66+
# scope declares `global __doc__`.
67+
src_class = '"module doc"\ndef f():\n global __doc__\nclass C:\n "class doc"\n'
68+
g, loc = {}, {}
69+
exec(compile(src_class, "<test>", "exec"), g, loc)
70+
assert g.get("__doc__") == "module doc", g
71+
assert loc["C"].__doc__ == "class doc", loc["C"].__doc__
72+
assert "__doc__" not in loc, loc

0 commit comments

Comments
 (0)