Skip to content

Commit d944e0a

Browse files
codegen: match CPython priority/location for nonlocal-global conflicts
The nonlocal-vs-global cross-check ran before the parameter, use, annotation, and assignment checks in each symboltable arm, but CPython 3.14 runs it last: - 'global x; print(x); nonlocal x' must report "name 'x' is used prior to nonlocal declaration", not "name 'x' is nonlocal and global" - 'nonlocal x; x = 2; global x' must report "name 'x' is assigned to before global declaration" Move the cross-check to the bottom of the Global/Nonlocal arms. Also keep the location of the first global/nonlocal directive instead of overwriting it on each one, so a duplicated directive still points at the first occurrence, matching CPython (this also fixes the "no binding for nonlocal" location for duplicate directives). Assisted-by: Kimi Code CLI
1 parent f382c29 commit d944e0a

1 file changed

Lines changed: 44 additions & 12 deletions

File tree

‎crates/codegen/src/symboltable.rs‎

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,13 +3215,6 @@ impl SymbolTableBuilder {
32153215
}
32163216
match role {
32173217
SymbolUsage::Global if !symbol.is_global() => {
3218-
// CPython reports the stored location of the first directive.
3219-
if flags.contains(SymbolFlags::DEF_NONLOCAL) {
3220-
return Err(SymbolTableError {
3221-
error: format!("name '{name}' is nonlocal and global"),
3222-
location: symbol.location,
3223-
});
3224-
}
32253218
if flags.contains(SymbolFlags::DEF_PARAM) {
32263219
return Err(SymbolTableError {
32273220
error: format!("name '{name}' is parameter and global"),
@@ -3248,15 +3241,16 @@ impl SymbolTableBuilder {
32483241
location,
32493242
});
32503243
}
3251-
}
3252-
SymbolUsage::Nonlocal => {
3253-
// CPython reports the stored location of the first directive.
3254-
if flags.contains(SymbolFlags::DEF_GLOBAL) {
3244+
// CPython checks the nonlocal conflict last and reports the
3245+
// stored location of the first directive.
3246+
if flags.contains(SymbolFlags::DEF_NONLOCAL) {
32553247
return Err(SymbolTableError {
32563248
error: format!("name '{name}' is nonlocal and global"),
32573249
location: symbol.location,
32583250
});
32593251
}
3252+
}
3253+
SymbolUsage::Nonlocal => {
32603254
if flags.contains(SymbolFlags::DEF_PARAM) {
32613255
return Err(SymbolTableError {
32623256
error: format!("name '{name}' is parameter and nonlocal"),
@@ -3283,6 +3277,14 @@ impl SymbolTableBuilder {
32833277
location,
32843278
});
32853279
}
3280+
// CPython checks the global conflict last and reports the
3281+
// stored location of the first directive.
3282+
if flags.contains(SymbolFlags::DEF_GLOBAL) {
3283+
return Err(SymbolTableError {
3284+
error: format!("name '{name}' is nonlocal and global"),
3285+
location: symbol.location,
3286+
});
3287+
}
32863288
}
32873289
SymbolUsage::AnnotationAssigned
32883290
if current_scope != CompilerScope::Module
@@ -3323,7 +3325,10 @@ impl SymbolTableBuilder {
33233325
table.symbols.entry(name.into_owned()).or_insert(symbol)
33243326
};
33253327

3326-
if matches!(role, SymbolUsage::Global | SymbolUsage::Nonlocal) {
3328+
// Keep the location of the first global/nonlocal directive: CPython
3329+
// reports that one when a later directive conflicts with it.
3330+
if matches!(role, SymbolUsage::Global | SymbolUsage::Nonlocal) && symbol.location.is_none()
3331+
{
33273332
symbol.location = location;
33283333
}
33293334

@@ -3852,6 +3857,9 @@ def f(x=(lambda: 1)()):
38523857
for source in [
38533858
"def f():\n global x\n nonlocal x\n",
38543859
"def f():\n nonlocal x\n global x\n",
3860+
// Repeated directives: CPython reports the first one.
3861+
"def f():\n global x\n global x\n nonlocal x\n",
3862+
"def f():\n nonlocal x\n nonlocal x\n global x\n",
38553863
] {
38563864
let err = scan_source_result(source).unwrap_err();
38573865

@@ -3866,6 +3874,30 @@ def f(x=(lambda: 1)()):
38663874
}
38673875
}
38683876

3877+
#[test]
3878+
fn nonlocal_global_conflict_loses_to_earlier_flag_checks_like_cpython() {
3879+
// CPython checks parameter/use/annotation/assignment conflicts before
3880+
// the nonlocal-vs-global cross-check.
3881+
let err = scan_source_result("def f():\n global x\n print(x)\n nonlocal x\n")
3882+
.unwrap_err();
3883+
assert_eq!(err.error, "name 'x' is used prior to nonlocal declaration");
3884+
3885+
let err = scan_source_result(
3886+
"def f():\n x = 1\n def g():\n nonlocal x\n print(x)\n global x\n",
3887+
)
3888+
.unwrap_err();
3889+
assert_eq!(err.error, "name 'x' is used prior to global declaration");
3890+
3891+
let err = scan_source_result(
3892+
"def f():\n x = 1\n def g():\n nonlocal x\n x = 2\n global x\n",
3893+
)
3894+
.unwrap_err();
3895+
assert_eq!(
3896+
err.error,
3897+
"name 'x' is assigned to before global declaration"
3898+
);
3899+
}
3900+
38693901
#[test]
38703902
fn except_handler_name_error_location_uses_handler_location_like_cpython() {
38713903
let source = "try:\n pass\nexcept Exception as __debug__:\n pass\n";

0 commit comments

Comments
 (0)