Skip to content

normalize_doc_attributes sometimes comments out code #3988

Open

Description

@olivia-fl

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.

Activity

  1. added
    A-ASTArea: Abstract Syntax Tree (AST) related things
    only-with-optionRequires a non-default option value to reproduce
    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICE
    and removed
    A-ASTArea: Abstract Syntax Tree (AST) related things
    on Jan 4, 2020
  2. elinorbgr commented on Feb 3, 2020

    @elinorbgr

    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.

  3. added this to the 2.1.0 milestone on Jun 30, 2020
  4. burrbull commented on Apr 14, 2021

    @burrbull

    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() -> (){
        }
    }
  5. burrbull commented on Apr 27, 2021

    @burrbull

    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

  6. ytmimi commented on Jul 26, 2022

    @ytmimi
    Contributor

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

  7. ahl commented on Apr 23, 2024

    @ahl
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEUO-normalize_doc_attributesUnstable option: normalize_doc_attributesonly-with-optionRequires a non-default option value to reproduce

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions