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

オープン
#7,243 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
20

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/rustfmt のほかの issue

rust-lang/rustfmt の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。