Skip to content

fix(declare-local): skip readonly refusal when the operand creates a fresh local - #3610

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3609-local-readonly-shadow
Nov 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3609-local-readonly-shadow

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

A local/declare operand that creates a FRESH local inside a function binds the new mutable instance — an outer-scope readonly mark on the same name must not fire (GNU make_local_variable, declare.def:929+ / variables.c:5524+). The nested_readonly check consulted the flat READONLY_VARS name mark without regard to locally_scoped, so local x=(7 8 9) inside a caller holding local -r x spuriously reported x: readonly variable (while still binding).

local_context is also gated on the declare family: export/readonly operands always bind the inherited global (setattr.def:240-268), so their readonly refusal still fires — verified against GNU 5.3.20 on 4 probe shapes (fresh-local shadow / inner mutable shadow / export / declare -g on a global readonly), all byte-identical.

5 new pins in tests/issue3609_local_readonly_shadow.rs, all green; adjacent declare/readonly/local regression batches green.

Fixes #3609. Refs #2666.

…fresh local

A local/declare operand that creates a FRESH local inside a function
binds the new mutable instance — an outer-scope readonly mark on the
same name must not fire (GNU make_local_variable, declare.def:929+ /
variables.c:5524+). The nested_readonly check consulted the flat
READONLY_VARS name mark without regard to locally_scoped, so
'local x=(7 8 9)' inside a caller holding 'local -r x' spuriously
reported 'x: readonly variable' (while still binding).

Also gate local_context on the declare family: export/readonly operands
always bind the inherited (global) variable (setattr.def:240-268), so
the non-localizing readonly refusal still fires for them — verified
against GNU 5.3.20 (export/declare -g on a global readonly inside a
function: diagnostic, then the body continues).

Same-scope re-local on a readonly local still refuses; an inner-scope
readonly mark shadowed by a fresh local stays scoped to its instance.

Fixes #3609. Refs #2666 (varenv19.sub ccc).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 11:46
@caomengxuan666
caomengxuan666 merged commit 841e125 into master Nov 16, 2026
8 of 11 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3610 第1轮 — 建议:PASS(已合入)
head: f6836d5 base(merge-base): 27e1d0d 文件: 2 个 +104/-4
关联 issue: #3609(状态:open→本轮已 CLOSED;squash 841e125)

机制核查:

  • nested_readonly 改为 !locally_scoped && is_marked_var(READONLY_VARS, lhs):GNU make_local_variable(declare.def:929+、variables.c:5524+)为 fresh local 绑新实例,外层同名 readonly mark 不作用于它;只有绑定的实例本身(非局部化操作数的 global/outer 目标,或同 scope 已 readonly 的 local)才拒绝。语义正确。
  • local_context 增补 declare_family(declare_local.rs:611 !matches!(command_name, "export" | "readonly")):setattr.def:240-268 规定 export/readonly 操作数总绑继承的 global,不能因函数内就走局部化。与修法方向一致。
  • 无关改动:无;无桥接/兼容 shim。

腿:

  • 钉自跑(head):issue3609_local_readonly_shadow → test result: ok. 5 passed; 0 failed; 0 ignored。
  • fail-first(base 27e1d0d,钉原样放入):test result: FAILED. 4 passed; 1 failed; 0 ignored;红名 = {local_compound_shadowing_outer_readonly_is_silent}(headline 判别格);其余 4 格为守卫(same-scope readonly / export / declare -g / fresh local 可写),base 即绿,符合 PR 自述「守卫锁链尾」。
  • lib 双边(两边先 cargo build --locked --bin rubash):head 1350 passed; 1 failed; 4 ignored / base 1350 passed; 1 failed; 4 ignored;失败名集合都 = {api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell},差集 ∅。
  • -D warnings:cargo check --lib 0 警告;cargo check --test issue3609_local_readonly_shadow 0 警告。
  • 卫生:cargo fmt --check 因仓库已知 Windows os error 206 不可用,逐文件 rustfmt --check src/executor/declare_local.rs tests/issue3609_local_readonly_shadow.rs → CLEAN;marker-lint OK (117 registry consts, 268 allowlisted sites);utf8 scanned src / clean。
  • CI(head f6836d5):红 = GNU regression goldens and canaries、Rust tests、Rust tests (macOS);Rust tests job 日志红名 = api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell → 基线(另两 job 同形)。其余全绿。
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1)-release(脚本文件 target/devin-s9/probes/p3609_{a..d}.sh):
    • a(local x=(7 8 9) 遮蔽外层 local -r x)→ GNU ddd 0 / ccc 7 8 9 / x=zzz,rc 0,无诊断 / rubash 同 → 一致
    • b(同 scope 再 local x=1)→ GNU 诊断 local: x: readonly variable,stdout never / rubash 文本相同(仅 stdout/stderr 交织顺序差,已知平台限制)→ 一致
    • c(函数内 export x=1 撞 global readonly)→ GNU x: readonly variable + never / rubash 同 → 一致
    • d(函数内 declare -g x=1 撞 global readonly)→ GNU declare: x: readonly variable + never / rubash 同 → 一致

F 点:无。

合入:

未跑/VOID 的腿:无。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[compat] local/declare shadowing a readonly outer-scope var spuriously reports readonly variable

1 participant