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 commentedon Mar 14, 2020
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 commentedon Mar 14, 2020
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.Include constness in impl blocks
Include constness in impl blocks (#4215)
BellaCoola commentedon Dec 25, 2020
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 theimpl
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 commentedon Dec 26, 2020
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 commentedon Dec 28, 2020
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!
Include constness in impl blocks (rust-lang#4215)
Include constness in impl blocks (#4215)
Rollup merge of rust-lang#81501 - calebcartwright:update-rustfmt, r=s…