Skip to content

rustfmt silently deletes the word const when it appears before impl while using the experimental const_trait_impl feature #4084

@slightlyoutofphase

Description

@slightlyoutofphase

For example, this is valid / working code on nightly:

#![allow(incomplete_features)]
#![feature(const_trait_impl)]

#[derive(Debug)]
struct Struct {
    f: f32,
}

impl const std::default::Default for Struct {
    #[inline]
    fn default() -> Self {
        Self { f: 12.5 }
    }
}

const S: Struct = Default::default();

fn main() {
    println!("{:?}", S);
}

However, if you run rustfmt, it will not error but simply quietly delete the word const between impl and std::default. This isn't great when using rustfmt at the crate level as it will of course delete all instances of const for trait impls, most likely breaking the build (and causing you to have to add them all back in).

The deletion seems like odd behavior in general, IMO: I'd expect rustfmt to give a hard error on syntax it thinks is legitimately invalid (as it usually does), and just do its best to normally format anything else.

Activity

calebcartwright

calebcartwright commented on Mar 14, 2020

@calebcartwright
Member

rustfmt uses the internal rustc parser (via the rustc-ap-* crates) to generate the AST for the input file(s), and then uses that AST to apply formatting. As such the version of the parser and corresponding AST that rustfmt sees lag a little behind the latest nightly compiler, and it takes a bit for rustfmt to support formatting new syntax.

The current version of the rustc parser that rustfmt is using predates rust-lang/rust#68847 which, I believe, is what added support for const_trait_impl, so that syntax isn't yet supported within rustfmt.

Regarding the absence of an error, if rustfmt's version of the rustc parser encounters any errors parsing the input files then those parser errors will be surfaced, and rustfmt will exit with an error code. You aren't seeing any errors because the rustc parser isn't surfacing any, but const isn't in the generated AST which is why it's not in the output emitted by rustfmt.

Support for const_trait_impl will be added to rustfmt when we update the version of the rustc parser used within rustfmt. For full transparency however, it's probably going to take a while because there's been a lot of upstream changes within the parser that will require a lot of work within rustfmt to make that upgrade.

For the time being you may want to consider leveraging the #[rustfmt::skip] attribute to prevent rustfmt from dropping const

slightlyoutofphase

slightlyoutofphase commented on Mar 14, 2020

@slightlyoutofphase
Author

Thanks for the thorough explanation. That it's more a case of const just not existing in the AST than it being actively removed makes a lot more sense.

I did actually go ahead and add #[rustfmt::skip] shortly after posting this, which seems to work fine for now.

added
bugPanic, non-idempotency, invalid code, etc.
on Mar 31, 2020
added a commit that references this issue on May 30, 2020
20aacb8
added a commit that references this issue on May 31, 2020
c6c9c7f
BellaCoola

BellaCoola commented on Dec 25, 2020

@BellaCoola

Hello, I am sorry to post here again, since it seems that this issue had been already closed, but in my experience rustfmt still silently deletes the const keyword when it appears before the impl keyword. Please, is there something else I should do (use some configuration flag) to make this work?

Tested with rustfmt 1.4.30-nightly.

Thank you very much and have a nice day!

calebcartwright

calebcartwright commented on Dec 26, 2020

@calebcartwright
Member

thanks for reaching out @BellaCoola. the issue was closed because it's been resolved in source, but hasn't been backported to the 1.x version of the codebase that's distributed through rustup. I've updated the labels on this issue to reflect the backport status and will look into getting this backported in the next release

BellaCoola

BellaCoola commented on Dec 28, 2020

@BellaCoola

Thank you very much! Looking forward to try it :) And also thank you for all your work on rustfmt, it is a great piece of software!

added a commit that references this issue on Jan 27, 2021
f49e199
added this to the 1.4.33 milestone on Jan 27, 2021
added a commit that references this issue on Jan 28, 2021
2695846
added a commit that references this issue on Jan 30, 2021
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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @calebcartwright@topecongiro@BellaCoola@slightlyoutofphase

      Issue actions

        rustfmt silently deletes the word `const` when it appears before `impl` while using the experimental `const_trait_impl` feature · Issue #4084 · rust-lang/rustfmt