`max_width` is exceeded by one column when a struct literal's `{` ends the first line

未关闭
#7,243 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
tooling

调研方向

Start with the reproducer's input.rs and rustfmt.toml, then find the rustfmt formatting tests and code that decide whether a struct literal breaks after =. Run the reproducer with max_width = 120; done means the 118-, 119-, and 120-column cases keep their first line, the two longest break after =, and no formatted line exceeds the limit.

由索引模型根据 Issue 内容生成。

描述

C-bug SO-max_width

Summary

I tried to format this code:

pub(crate) const SETTINGS_SOURCE: crate::Parameters<crate::DefaultVariant> = crate::Parameters::<crate::DefaultVariant> { first: Some(1), last: Some(2) };

with max_width = 120.

Expected behavior

I expected the item to break after =, as the same item does when the name is one column longer:

pub(crate) const SETTINGS_SOURCEX: crate::Parameters<crate::DefaultVariant> =
    crate::Parameters::<crate::DefaultVariant> {
        first: Some(1),
        last: Some(2),
    };
Actual behavior

The first line stays at 121 columns, one past max_width, with the opening { as its last character, and the item is not broken after =:

pub(crate) const SETTINGS_SOURCE: crate::Parameters<crate::DefaultVariant> = crate::Parameters::<crate::DefaultVariant> {
    first: Some(1),
    last: Some(2),
};

With the name padded so that the first line would end in { at column 118, 119, 120, 121 and 122, the five items keep that first line at 118, 119, 120 and 121, and the one at 122 is broken after =. The same 121 columns with a call on the right, = crate::Settings::new(first, last), are broken after =.

Configuration

rustfmt cli options used:

$ rustfmt --edition 2024 --config-path rustfmt.toml input.rs

rustfmt.toml:

max_width = 120

Reproduction Steps

  1. Clone the reproducer gist, git clone https://gist.github.com/YevheniiKotyrlo/2ae6769cd363cf59cf3c4bed2e9ff43f; it holds input.rs, rustfmt.toml and repro.sh. input.rs holds five items whose first line would end in { at 118 to 122 columns, and the call item above.
  2. In the clone, run bash repro.sh, which formats input.rs with rustfmt --edition 2024 --config-path rustfmt.toml and takes the rustfmt binary from its first argument, else the RUSTFMT environment variable, else rustfmt on PATH.
  3. It prints the first line of each item with its width, then every line longer than max_width, and ends with:
== the lines past 120
121 pub(crate) const SETTINGS_SOURCE: crate::Parameters<crate::DefaultVariant> = crate::Parameters::<crate::DefaultVariant> {
RESULT: the defect shows — a formatted line is past max_width

It exits 1 while a line is longer than max_width, and 0 after a fix, where the items at 118, 119 and 120 columns keep their first line and the two longest break after =.

Meta

rustfmt --version:

rustfmt 1.10.0-stable (58cf8f9c35 2026-10-13)

rustc --version: rustc 1.99.1 (58cf8f9c3 2026-10-13).

Reproduced on main 0acb4720feb76e209e7c61f0133efbe628a4641b as well, where rustfmt --version prints rustfmt 1.12.0-nightly (0acb4720fe 2026-11-10).

Host: Windows 11, x86_64-pc-windows-msvc.

主要语言
Rust
星标
7.1k
派生
1.2k
平均合并
1 天 4 小时
30 天内合并 PR
20

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/rustfmt 的其他 Issue

查看 rust-lang/rustfmt 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。