Repository navigation
fix: printf char constant follows the locale byte/char rule (Refs #2965) - #2976
Merged
Merged
Conversation
caomengxuan666
marked this pull request as ready for review
November 7, 2026 10:26
This was referenced Nov 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #2965
问题
printf %d "'é"在单字节 locale 下取了字符的码点(233),GNU 取首字节(195)。根因:printf_char_constant在文本层用chars().next()取码点,完全不看 locale,也不看原始字节(经载体标记携带的高字节参数被读成标记字符的码点)。GNU 锚点
builtins/printf.def:1522-1547 asciicode():数值 getter(getint:1416 /getintmax/getuintmax/getdouble/getfloatmax)在操作数首字节为'或"时走此路——对 arg[1] 起的字节串做mbrtowc(当前 locale):单字节 locale 得字节本身,多字节 locale 得解码字符,解码失败/不完整(MB_INVALIDCH,include/shmbutil.h:44)回退(unsigned char)arg[1];孤引号读 arg[1] 处的 NUL 得 0,无诊断。+'a/-'a/'a走 strtoimax 报invalid number(oracle 实测 rc 1)。%c的getchr(printf.def:1351-1366)返回 arg[0] 原始字节(引号操作数印出引号本身、多字节参数印首字节,双 locale 皆然);%s/%b经getstr逐字取参数。三面均已由钉锁定为对照。LC_ALL=...赋值即时生效(variables.c 的 sv_lc_all watcher;oracle 实测导出/未导出双向翻转)——经既有ShellLocaleGuard机制(rubash#1488)在 printf 唯一漏斗execute_with_io_and_store入口按传入的 env_vars 安装,谓词走active_locale_name()。改动
src/builtins/printf/number.rs:printf_char_constant改为字节级 asciicode 移植——先经locale_byte_span还原原始字节跨度(解载体标记),首字节为引号才入路;多字节生效时用decode_utf8_char解码(且要求规范编码长度,拒 glibc mbrtowc 同样拒绝的过长编码),失败回退首字节;返回值改u32,三处调用点(i64/u64/long double)随之调整。删除旧的码点口径注释与char返回形态。src/builtins/printf/value.rs:locale_byte_span可见性放宽为pub(super)供 number.rs 共用同一字节视图。src/builtins/printf.rs:execute_with_io_and_store入口安装ShellLocaleGuard(覆盖普通/管道/命令替换全部派发面,嵌套时恢复外层值)。tests/issue2965_printf_char_constant_locale_regressions.rs(14 测):C locale 首字节(é/ü/Ā/€/😀、双引号前缀、%x%o%u%i 共路、%f 共路)、C.UTF-8 码点、UTF-8 无效/截断字节回退首字节、C 高字节、ASCII 对照、符号/空格前缀 invalid number、%c/%s/%b 对照、脚本内 LC_ALL 赋值、管道面。fail-first 5 passed / 9 failed,修后 14 passed / 0 failed。bash:prefix; D1 argv[0] prolog renders the absolute binary path #2935 的 D1 prolog 签名与 locale 解码既有面);issue510/issue879 为cfg(windows)stem、Linux 0 测。rustfmt --edition 2021 --checkclean;RUSTFLAGS="-D warnings" cargo check --locked --lib --binsrc=0。本 PR 删除了什么
printf_char_constant的文本层码点实现(chars().next()口径,含其误导性注释「a single BMP char is its own code point」)与longdouble_char_constant(char)旧签名;无整文件删除。UNVERIFIED-BY-CAPTAIN:全部数字为车道原始计数,待队长中央复核。