rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

`imports_granularity = "Module"` doesn't work if module path is too long

Open kenoss opened this issue 1 year ago • 2 comments

Repro steps

  1. Add the following inputs to tests/source/5131_module.rs.
  2. Execute cargo test test::system_tests and get output.

case 1. correct

Input:

use foo::{
    Foo,
    bar::Bar,
};

Output:

use foo::bar::Bar;
use foo::Foo;

case 2. wrong

Input:

use foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::{
    Foo,
    bar::Bar,
};

Output:

use foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::{
    Foo,
    bar::Bar,
};

Expected:

use foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::bar::Bar;
use foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::Foo;

case 3. wrong

Input:

use x::fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::{
    Foo,
    bar::Bar,
};

Output:

use x::fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::{
    Foo,
    bar::Bar,
};

Expected:

use x::fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::bar::Bar;
use x::fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::Foo;

Link: #4991

kenoss avatar May 15 '24 17:05 kenoss

I'm planning to investigate it.

kenoss avatar May 15 '24 17:05 kenoss

Failed here: https://github.com/rust-lang/rustfmt/blob/d5f1200ed6a8e375f963e0c59a8bee45c0018c55/src/imports.rs#L1088

[src/imports.rs:1089:9] "rewrite of UseTree starts" = "rewrite of UseTree starts"
[src/imports.rs:1095:13] &segment = foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
[src/imports.rs:1095:13] &segment_str = "foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
[src/imports.rs:1095:13] &result = "foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
[src/imports.rs:1099:17] &shape = Shape {
    width: 95,
    indent: Indent {
        block_indent: 0,
        alignment: 0,
    },
    offset: 4,
}
[src/imports.rs:1099:17] shape.offset_left(2 + segment_str.len()) = Some(
    Shape {
        width: 4,
        indent: Indent {
            block_indent: 0,
            alignment: 0,
        },
        offset: 95,
    },
)
[src/imports.rs:1099:17] segment_str.len() = 89
[src/imports.rs:1095:13] &segment = bar
[src/imports.rs:1095:13] &segment_str = "bar"
[src/imports.rs:1095:13] &result = "foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::bar"
[src/imports.rs:1099:17] &shape = Shape {
    width: 4,
    indent: Indent {
        block_indent: 0,
        alignment: 0,
    },
    offset: 95,
}
[src/imports.rs:1099:17] shape.offset_left(2 + segment_str.len()) = None
[src/imports.rs:1099:17] segment_str.len() = 3
[src/imports.rs:338:9] self.rewrite(context, shape.offset_left(vis.len())?) = None

TODO(tomorrow): Understand what Shape is.

kenoss avatar May 18 '24 16:05 kenoss