Skip to content

rubash#3605 run-nquote: don't count expansion-introduced newlines as input lines - #3678

Merged
caomengxuan666 merged 1 commit into
masterfrom
devin/alias-expansion-line-stamps
Nov 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
devin/alias-expansion-line-stamps

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Refs #3605 (class B, run-nquote).

GNU pushes alias-expansion text onto the input (parse.y:2055 push_string) and consumes it through shell_getc; the line_number bump fires only inside yy_readline_get on a REAL input fetch (parse.y:2504). A \n inside an alias value is therefore a command separator but not a line advance: every command born from one expansion stamps the alias word's line, and the next real input line advances from there.

Rubash splices expansion text into a flat buffer the batch lexer counts normally, inflating stamps by the number of expansion newlines (nquote4.sub reported line 8 where GNU reports line 6).

Changes

  • src/lexer/alias_stream.rs: expand_aliases_in_source_marked returns the expanded text plus ascending byte offsets of newlines the splice introduced; splice_alias_value records each spliced \n char offset and shifts recorded marks by the same delta bounds get. The streaming MainAliasScanner drops them — its String-only consumers never see expansion text.
  • src/lexer/mod.rs: push_line_counted gates the line_number bump in push_main_line/feed_awaiting_body; the tokenize_with_heredocs split loop marks a piece uncounted when its terminating newline's byte offset is in the set. tokenize_comsub_body_with_origin_marked / tokenize_with_initial_posix_marked carry the set through.
  • src/script_driver.rs, src/builtins/source/execution.rs: expand_group_aliases_marked + run_source_with_line_offset_marked plumb the set from both batch expansion sites (run_history_group, run_source_groups). Token reuse is refused when marks are non-empty — certified feeder tokens have inflated stamps (whitelist narrowing; a false negative costs the fresh re-lex, never semantics).

Verification

  • vs GNU bash 5.3.20 (script files): nquote4.sub shape plus mid-line (x=1; foo), value-trailing-newline, and multi-newline variants — byte-for-byte identical including line N: quux: command not found stamps.
  • niubash gate: run-nquote PASS (was FAIL); run-alias/run-errors diffs unchanged (pre-existing class items owned by other classes), run-type/run-quote PASS.
  • tests/issue3605_alias_expansion_lines.rs: 3 regressions pin the stamp rule.
  • Cargo regression slice (alias/line-stamp test files): all green except the known macOS Mach-O cannot execute binary file environmental failures (fix(executor): admit Mach-O images in the exec gates on macOS (Refs #3585) #3586).

Scope note

expand_comsub_alias_bodies ($(/${ pre-pass) newlines are not tracked — a second-order gap; comsub bodies re-lex at runtime with their own line base.

…input lines

GNU pushes alias-expansion text onto the input (parse.y:2055
push_string) and consumes it through shell_getc; the line_number bump
fires only inside yy_readline_get on a REAL input fetch
(parse.y:2504). A newline inside an alias value is therefore a command
separator but not a line advance: every command born from one
expansion stamps the alias word's line, and the next real input line
advances from there.

Rubash splices expansion text into a flat buffer the batch lexer
counts normally, inflating stamps by the number of expansion newlines
(nquote4.sub reported 'line 8' where GNU reports 'line 6').

- alias_stream: expand_aliases_in_source_marked returns the expanded
  text plus ascending byte offsets of newlines the splice introduced;
  splice_alias_value records each spliced '\n' char offset and shifts
  recorded marks by the same delta bounds get (streaming MainAliasScanner
  drops them — its String-only consumers never see expansion text).
- lexer: push_line_counted gates the line_number bump in
  push_main_line/feed_awaiting_body; the tokenize_with_heredocs split
  loop marks a piece uncounted when its terminating newline's byte
  offset is in the set. tokenize_comsub_body_with_origin_marked /
  tokenize_with_initial_posix_marked carry the set through.
- script_driver/execution.rs: expand_group_aliases_marked +
  run_source_with_line_offset_marked plumb the set from both batch
  expansion sites (run_history_group, run_source_groups); reuse is
  refused when marks are non-empty (certified feeder tokens have
  inflated stamps).

Verified vs GNU bash 5.3.20: nquote4.sub shape plus mid-line,
value-trailing-newline and multi-newline variants match byte-for-byte;
niubash gate run-nquote PASS, run-alias/run-errors diffs unchanged
(pre-existing class items). tests/issue3605_alias_expansion_lines.rs
pins the stamp rule on three shapes.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3678 第1轮 — 建议:PASS
head: 8258740 base(merge-base): 96802ca 文件: 5 个 +335/-17
关联 issue: #3605(open,核于 2026-11-16 — 追踪目录 issue,合并不关单)

机制核查:

  • expand_aliases_in_source_marked 返回展开文本 + 展开引入换行的字节偏移;splice_alias_value 在 buf 位移时同步平移早先 mark(与 bounds 同 delta);tokenize_with_heredocs 逐 piece 按 piece_start + piece.len() 命中 mark 即 push_line_counted(counted=false) — 把 line_number bump 收窄到「真实输入行」,对应 GNU shell_getc/yy_readline_get 只在真实 fetch 时 bump(parse.y:2504)的机制。两条白名单式收敛:feeder 复用在 marks 非空时拒绝,落到重新 lexer,语义不变。
  • 诚实声明的残差:expand_comsub_alias_bodies($(/${ ` 预扫)的换行未跟踪 — 正文已声明为二阶缺口。
  • 无关改动:无;无桥接层。

腿:

  • 钉自跑:issue3605_alias_expansion_lines 3/3 绿
  • fail-first:base 上 1 passed / 2 failed,红名 = {alias_embedded_newlines_stamp_alias_word_line, mid_line_alias_expansion_same_stamp};real_lines_after_expansion_keep_counting 在 base 上即绿(守护性子钉,文件整体可判别)
  • lib 双边:head {issue2084_session_end_restores_host_process_state} / base 同 / 差集 {}(Windows 本机 flake,双侧相同)
  • -D warnings:lib+bins 0;钉 stem 编译 0
  • 卫生:PR 全部文件 rustfmt --check 0 diff;CI ubuntu job UTF-8/marker 两步 success(cargo fmt 全库在本机因 os error 206 不可用,逐文件替代)
  • CI:红 = 3 job 仅 issue2875 基线;census 等后续 step skipped
  • GNU 对照(WSL GNU bash 5.3.20 脚本文件,.audit-s6/probes/):foo(多行 alias)→ GNU/head line 4: quux 一致;alias foo=$'echo a\\necho b\\necho c'+foo/m1/m2 → GNU/head line 4:m1 line5:m2 一致;x=1; foo 中行形 → GNU/head line 3:quux line4:badcmd_end 一致

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.

1 participant