Formatter drops comments in dict patterns, variant spread patterns and dict literal spreads
维护者通常 1 天内回复
已经有一个关联 PR 被合并了。
- #8850 来自 @cknitt —— 已合并
评估
调研方向
Start with res_comments_table.ml and res_printer.ml at the locations linked in the issue, then review the comment fixtures under tests/syntax_tests/data/printer/comments/. Reproduce with the provided cm.res command and add fixture cases for dict patterns and variant spread patterns. Done means formatting preserves every listed comment and the fixtures catch losses on the first print.
由索引模型根据 Issue 内容生成。
描述
Found by adversarial experiments on semantic analysts.
What is broken
The formatter deletes comments in these positions:
- Dict pattern key:
| dict{"a" /* k */: b}→| dict{"a": b} - Dict pattern value:
| dict{"a": /* c */ a}→| dict{"a": a}(the record pattern{a: /* c */ a}keeps its comment) - Empty dict pattern:
| dict{/* e */}→| dict{} - Variant spread pattern:
| .../* c */ t→| ...t - Polymorphic variant type pattern:
| #.../* c */ t→| #...t - Dict literal with a spread: an own-line comment before the first row after a spread is dropped (
/* x */on the same line is kept)
Reproduction
cm.res:
let f = d =>
switch d {
| dict{"a" /* k */: b} => b
| dict{"a": /* c */ a} => a
| dict{/* e */} => 0
| _ => 1
}
type t = A | B
type u = | ...t | C
let g = x =>
switch x {
| .../* c */ t => 0
| #.../* c */ t => 0
| C => 1
}
let b = dict{
...base,
// first
"a": 1,
}
./_build/default/compiler/syntax/cli/res_cli.exe cm.res
Observed: every comment above is missing from the output. Expected: each comment is printed.
Where
Ppat_typeandPpat_variant_spreadattach no comments:
https://github.com/rescript-lang/rescript/blob/8d51b9ce7/compiler/syntax/src/res_comments_table.ml#L2056- Dict pattern rows are walked as record rows, so key comments are attached to the key location:
https://github.com/rescript-lang/rescript/blob/8d51b9ce7/compiler/syntax/src/res_comments_table.ml#L2057-L2063 print_pattern_dict_rowprints the key withprint_longident, which does not consult the comment table. The record row printer usesprint_lident_path ... cmt_tbl.
https://github.com/rescript-lang/rescript/blob/8d51b9ce7/compiler/syntax/src/res_printer.ml#L3051-L3076- The
Ppat_dictcase has noprint_comments_inside:
https://github.com/rescript-lang/rescript/blob/8d51b9ce7/compiler/syntax/src/res_printer.ml#L2845-L2864 - In a dict literal with spreads, each run of rows becomes a synthetic expression. Leading comments attach to that expression (https://github.com/rescript-lang/rescript/blob/8d51b9ce7/compiler/syntax/src/res_comments_table.ml#L1402-L1410), but
print_spread_dict_exprprints the run without printing them (https://github.com/rescript-lang/rescript/blob/8d51b9ce7/compiler/syntax/src/res_printer.ml#L1782-L1786). This cause is from reading the code.
No comment fixture covers dict patterns or variant spread patterns (tests/syntax_tests/data/printer/comments/). The roundtrip test compares the first formatting with the second, so a comment lost on the first print passes. The fix should add these cases to the comment fixtures.
Introduced by
The same output is produced at ece8b148, before the dict pattern and variant spread nodes existed. The current code comes from b081cd19c (#8749) and bffd18e7b (#8742).
Impact
Running the formatter silently deletes user comments.
- 主要语言
- OCaml
- 星标
- 7.5k
- 派生
- 484
- 平均合并
- 20 小时 27 分钟
- 30 天内合并 PR
- 120
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
rescript-lang/rescript 的其他 Issue
-
难度 5/5 一周以上 新手友好度 25/100
rescript-lang/rescript#8833 · 1 个 reaction ·
维护者通常 1 天内回复
-
rewatch watch on Linux stops watching a source folder when an overlapping recursive source folder is removed from rescript.json可能已有人在做 @cknitt 于 1 天前认领。 未关闭
难度 4/5 1-2 天 新手友好度 35/100
rescript-lang/rescript#8825 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 35/100
rescript-lang/rescript#8806 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 35/100
rescript-lang/rescript#8798 ·
维护者通常 1 天内回复
-
难度 3/5 半天 新手友好度 58/100
rescript-lang/rescript#8757 ·
维护者通常 1 天内回复
查看 rescript-lang/rescript 的全部 Issue
相似的 Issue
-
beginner bug
难度 1/5 1 小时以内 新手友好度 88/100
hiero-ledger/hiero-sdk-js#4517 ·
维护者通常 1 天内回复
-
bug misleading tooling trivial
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 62/100
vitest-dev/vitest#11577 ·
维护者通常 1 天内回复
-
e2e-failure ready-to-code
难度 2/5 1-3 小时 新手友好度 72/100
redhat-developer/rhdh-plugin-export-overlays#4391 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 62/100