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

Đang mở
#7,243 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
7.1k
Fork
1.2k
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
20

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/rustfmt

Tất cả issue của rust-lang/rustfmt

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.