Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normalize_doc_attributes sometimes comments out code #3988

Open
olivia-fl opened this issue Jan 1, 2020 · 5 comments
Open

normalize_doc_attributes sometimes comments out code #3988

olivia-fl opened this issue Jan 1, 2020 · 5 comments
Labels
bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce
Milestone

Comments

@olivia-fl
Copy link

olivia-fl commented Jan 1, 2020

The simplest test case I've been able to come up with so far is this:

mod a {
    mod b {
        #[doc = "test"]fn foo() -> () { }
    }
}

with config

normalize_doc_attributes=true
max_width=10

This example gets reformatted to the following, although it does report an error because the line exceeds the maximum width.

mod a {
    mod b {
        ///testfn foo() -> (){
        }
    }
}

I'm still not sure exactly what triggers this bug, but have found a lot of variants for this example which don't trigger it. Here are a few that might be significant:

#[doc = "test"]fn foo() -> () { }
mod a {
    mod b {
        #[doc = "test"]fn foo() { }
    }
}
mod a {
    mod b {
        #[doc = "test"]
        fn foo() -> () { }
    }
}
mod a {
    mod b {
        /// test
        fn foo() -> () { }
    }
}

In these examples, removing max_width=10 fixes the issue, but I encountered this originally in a situation where rustfmt was inexplicably being run on autogenerated files in the target directory that were part of the wayland-protocol crate. I haven't figured out what was causing rustfmt to process those files in the first place, but in that case I did have max_width=80 instead.

I have no clue what's happening or how to fix it at this point, but I'm pretty sure it has to do with formatting lines that are longer than max_width combined with some unknown other requirement. I'll try to look into this more soon and see if I can figure out how to fix it.

@topecongiro topecongiro added a-chains only-with-option requires a non-default option value to reproduce bug Panic, non-idempotency, invalid code, etc. and removed a-chains labels Jan 4, 2020
@elinorbgr
Copy link

elinorbgr commented Feb 3, 2020

An other example that triggers this:

This original (generated) code:

pub mod zwp_fullscreen_shell_v1 { impl ZwpFullscreenShellV1 { # [ doc = "present surface for display at a particular mode\n\nIf the value of wl_output.scale differs from wl_surface.buffer_scale,\nthen the compositor may choose a mode that matches either the buffer\nsize or the surface size.  In either case, the surface will fill the\noutput." ] pub fn present_surface_for_mode < F > () where F : FnOnce () -> super :: zwp_fullscreen_shell_mode_feedback_v1 :: ZwpFullscreenShellModeFeedbackV1 { } } }

gets formatted as

pub mod zwp_fullscreen_shell_v1 {
    impl ZwpFullscreenShellV1 {
        ///present surface for display at a particular mode
        ///
        ///If the value of wl_output.scale differs from
        /// wl_surface.buffer_scale, then the compositor may choose a
        /// mode that matches either the buffer size or the surface
        /// size.  In either case, the surface will fill the output.pub fn present_surface_for_mode < F > () where F : FnOnce () -> super :: zwp_fullscreen_shell_mode_feedback_v1 :: ZwpFullscreenShellModeFeedbackV1{
        }
    }
}

when run using these rustfmt options:

wrap_comments = true
normalize_doc_attributes = true

As seen, this comments part of the code.

@burrbull
Copy link

burrbull commented Apr 14, 2021

Looks like this issue not in "normalize_doc_attributes" but in "max_width"
Because using max_width=10 only produces

mod a {
    mod b {
        #[doc = "test"]        fn foo() -> (){
        }
    }
}

instead of to add newline
I think it works incorrect with any attribute:

mod a {
    #[myattr]fn foo() -> () { }
}

with max_width=7 produces

mod a {
    #[myattr]
    fn foo(
    ) -> (
    )
    {
    }
}

But with max_width=6:

mod a {
    #[myattr]    fn foo() -> (){
    }
}

@burrbull
Copy link

burrbull commented Apr 27, 2021

Without attributes
Input:

mod a {
    fn foo() -> () { }
}

Output with with max_width=10:

mod a {
    fn foo(
    ) -> ()
    {
    }
}

Output with with max_width=7:

mod a {
    fn foo(
    ) -> (
    )
    {
    }
}

Output with with max_width=6 and less:

mod a {
    fn foo() -> (){
    }
}

6 can be calculated as 4 (result indent of first line) + 2

@ytmimi
Copy link
Contributor

ytmimi commented Jul 26, 2022

confirming this is still reproducible with rustfmt 1.5.1-nightly (a451a39d 2022-07-25)

@ahl
Copy link
Contributor

ahl commented Apr 23, 2024

I hit this today as well:

input:

impl Client {
    #[doc = "comment"]    pub async fn key_changes_survey_response_education_organization_target_associations < 'a > (& 'a self , limit : Option < i64 > , max_change_version : Option < i64 > , min_change_version : Option < i64 > , offset : Option < i32 > , total_count : Option < bool > , snapshot_identifier : Option < & 'a str >) -> Result < ResponseValue < Vec < types :: TrackedChangesEdFiSurveyResponseEducationOrganizationTargetAssociationKeyChange > > , Error < () > , >{
        todo!()
    }
}

output:

impl Client {
    ///comment    pub async fn key_changes_survey_response_education_organization_target_associations < 'a > (& 'a self , limit : Option < i64 > , max_change_version : Option < i64 > , min_change_version : Option < i64 > , offset : Option < i32 > , total_count : Option < bool > , snapshot_identifier : Option < & 'a str >) -> Result < ResponseValue < Vec < types :: TrackedChangesEdFiSurveyResponseEducationOrganizationTargetAssociationKeyChange > > , Error < () > , >{
        todo!()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants