Skip to content

Commit e01021b

Browse files
authored
Fix module docstring global name resolution (#9095)
* Fix module docstring global name resolution Assisted-by: Claude Code:claude-opus-5-5 * Move module docstring test into syntax_doc.py Assisted-by: Claude Code:claude-opus-5-5
1 parent 8de2cbe commit e01021b

2 files changed

Lines changed: 123 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(

‎extra_tests/snippets/syntax_doc.py‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,77 @@ def f2():
1313
"""
1414

1515
assert f2.__doc__ == '\nx\n y\n'
16+
17+
18+
# Regression test: a module docstring must be stored through module name
19+
# resolution, so a nested `global __doc__` declaration makes the module body
20+
# emit STORE_GLOBAL (writing module globals) instead of STORE_NAME (writing
21+
# the provided locals mapping). Mirrors CPython codegen_nameop behavior.
22+
23+
import dis
24+
25+
26+
def opnames(code):
27+
return [instr.opname for instr in dis.get_instructions(code)]
28+
29+
30+
def run_split(src, optimize=0):
31+
code = compile(src, "<test>", "exec", optimize=optimize)
32+
g, loc = {}, {}
33+
exec(code, g, loc)
34+
return code, g, loc
35+
36+
37+
# A nested function declaring `global __doc__` makes the module docstring
38+
# store target module globals.
39+
src_global = '"module doc"\ndef f():\n global __doc__\n'
40+
code, g, loc = run_split(src_global)
41+
ops = opnames(code)
42+
assert "STORE_GLOBAL" in ops, ops
43+
assert "STORE_NAME" not in ops or "__doc__" not in [
44+
instr.argval for instr in dis.get_instructions(code) if instr.opname == "STORE_NAME"
45+
], ops
46+
assert g.get("__doc__") == "module doc", g
47+
assert "__doc__" not in loc, loc
48+
49+
# The declaration may be nested more than one scope deep.
50+
src_deep = '"module doc"\ndef f():\n def g():\n global __doc__\n'
51+
code, g, loc = run_split(src_deep)
52+
assert "STORE_GLOBAL" in opnames(code), opnames(code)
53+
assert g.get("__doc__") == "module doc", g
54+
assert "__doc__" not in loc, loc
55+
56+
# Control: without a nested global declaration the docstring stays a
57+
# STORE_NAME and lands in the provided locals mapping.
58+
src_plain = '"module doc"\nx = 1\n'
59+
code, g, loc = run_split(src_plain)
60+
doc_stores = [
61+
instr.opname
62+
for instr in dis.get_instructions(code)
63+
if instr.argval == "__doc__" and instr.opname.startswith("STORE_")
64+
]
65+
assert doc_stores == ["STORE_NAME"], doc_stores
66+
assert loc.get("__doc__") == "module doc", loc
67+
assert "__doc__" not in g, g
68+
69+
# optimize=1 keeps the docstring and the same global resolution.
70+
code, g, loc = run_split(src_global, optimize=1)
71+
assert "STORE_GLOBAL" in opnames(code), opnames(code)
72+
assert g.get("__doc__") == "module doc", g
73+
74+
# optimize=2 strips the docstring entirely: no __doc__ store in either dict.
75+
code, g, loc = run_split(src_global, optimize=2)
76+
assert not any(
77+
instr.argval == "__doc__" and instr.opname.startswith("STORE_")
78+
for instr in dis.get_instructions(code)
79+
), opnames(code)
80+
assert "__doc__" not in g and "__doc__" not in loc, (g, loc)
81+
82+
# A class docstring still belongs to the class namespace even when a nested
83+
# scope declares `global __doc__`.
84+
src_class = '"module doc"\ndef f():\n global __doc__\nclass C:\n "class doc"\n'
85+
g, loc = {}, {}
86+
exec(compile(src_class, "<test>", "exec"), g, loc)
87+
assert g.get("__doc__") == "module doc", g
88+
assert loc["C"].__doc__ == "class doc", loc["C"].__doc__
89+
assert "__doc__" not in loc, loc

0 commit comments

Comments
 (0)