rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.55k stars 12.74k forks source link

Pretty-printer inserts an extra newline after multiline trailing comments #37201

Closed petrochenkov closed 3 years ago

petrochenkov commented 8 years ago

Example:

fn main() {
    let a = 10; // Comment 1
                // Comment 2
}

=>

fn main() {
    let a =
        10; // Comment 1
            // Comment 2

}

Affected tests: [pretty] pretty\issue-20427.rs [pretty] pretty\issue-22992.rs [pretty] pretty\issue-23338-ensure-param-drop-order.rs [pretty] pretty\issue-27401-dropflag-reinit.rs

The issue disappears if comments are not packed into ibox (see try!(self.ibox(0)) and self.end() in pprust.rs/fn print_comment/comments::Trailing), but formatting become worse and other tests fail. I spent whole day investigating how these boxes work and reading Derek C. Oppen's "Pretty Printing" but still have no idea why this extra newline appears.

jyn514 commented 3 years ago

Triage: the output looks a little odd because it doesn't keep the alignment, but the bug reported here is now fixed:

$  rustc --pretty=normal pretty.rs  -Zunstable-options
fn main() {
    let a = 10; // Comment 1
    // Comment 2
}
petrochenkov commented 3 years ago

If this is fixed, then the tests need to be un-ignored, reopening until that is done.