Skip to content

fix(exec,declare): model GNU this_command_name lifecycle for expansion-time declaration diagnostics (Fixes #4161) - #4263

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/4161-declare-rejected-local-copy-prolog
Nov 28, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/4161-declare-rejected-local-copy-prolog

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Model GNU Bash's dynamic this_command_name lifecycle so expansion-time declaration diagnostics (nested_copy_refused, nested_readonly, array-conversion) use the exact GNU builtin_error_prolog command label instead of deriving it from function_name_stack (Fixes #4161).

GNU C Specification

  • third_party/bash/execute_cmd.c:4617 vs :4799-4800 (execute_simple_command()): expands the word list before installing this_command_name = words->word->word, so expansion-time declaration diagnostics on a function's first simple command still see the enclosing invocation's name (f).
  • third_party/bash/execute_cmd.c:4951-4952: normal completion discards the unwind frame and sets this_command_name = NULL unconditionally — subsequent simple commands in the same function therefore expand with this_command_name == NULL (bare r: readonly variable on line 4 of local r=(9); declare r=(4)).
  • third_party/bash/execute_cmd.c:4629-4631: assignment-only (null) commands clear this_command_name = NULL immediately.
  • third_party/bash/execute_cmd.c:3231,3931 (execute_arith_command) and :4164 (execute_cond_command): (( and [[ install their operator spelling and never clear it.
  • Pipelines and subshells execute in forked children and leave the parent's this_command_name intact.
  • Diagnostic path: subst.c:12949 expand_declaration_argument() -> variables.c:2658 make_local_variable() -> builtins/common.c:240 sh_readonly() -> builtins/common.c:83-94 builtin_error_prolog().

Changes

  1. src/shell/state.rs + src/executor/init.rs:
    • Added this_command_name and expansion_command_name (Rc<RefCell<Option<String>>>) to ShellState, deep-copied on Clone (fork independence) and included in InteriorSnapshot (snapshot_interior / restore_interior).
  2. src/executor/command_execute.rs:
    • Installed "((" / "[[" on arithmetic/conditional commands before dispatch (execute_cmd.c:3231/4164).
    • Added ThisCommandNameGuard at the simple-command funnel to unconditionally clear this_command_name on exit (execute_cmd.c:4952), and cleared both cells on assignment-only null commands (execute_cmd.c:4629-4631).
    • Captured the pre-install expansion-time label into expansion_command_name right before installing cmd.words[0] into this_command_name (execute_cmd.c:4617 vs :4800).
  3. src/executor/declare_local.rs:
    • Switched nested-declare diagnostic layers (nested_copy_refused, nested_readonly, array-conversion) to read expansion_command_name instead of function_name_stack.first().
  4. tests/issues_3900_4199.rs + tests/issues_3900_4199/issue4161_this_command_name_lifecycle.rs:
    • Added 8 integration pins covering the exact #4161 repro, first-command invocation label, prior simple command NULLing, assignment-only NULLing, ((/[[ operator labels, pipeline/subshell preservation, nested function calls, and non-materialization of rejected local copies.

What this PR deletes

  • Deletes the same_scope_local syntactic workaround predicate in src/executor/declare_local.rs (obsoleted by the real execute_cmd.c:4952 this_command_name lifecycle).
  • Deletes the function_name_stack.first() expansion-time prefix fallback in src/executor/declare_local.rs.

Verification

  • WSL GNU Bash 5.3.20 (/usr/local/bin/bash):
    • 20/20 shapes (required 13 shapes + assignment-only, pipelines, subshells, eval, ((, [[, comsub-assignment): 100% byte-exact (stdout + stderr + rc).
  • WSL Linux:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings).
    • cargo test --test issues_3900_4199 issue4161 --locked: 8/8 passed.
    • cargo test --test issues_2500_2699 issue2658 --locked (#4123 + #4160 coverage): 23/23 passed.
  • Windows:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings).
    • cargo test --lib --locked: 1,449/1,449 passed.
    • cargo test --test issues_3900_4199 issue4161 --locked: 8/8 passed.
    • cargo test --test issues_2500_2699 issue2658 --locked: 9/9 passed.
    • python scripts/check-rustfmt.py (226 targets), check-marker-literals.py, check-api-ledger.py: all clean.

Fixes #4161

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.

declare against a rejected local copy reports the wrong prolog category for the bare bind layer

1 participant