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

rustfmt removes inner attributes from inline const blocks #6158

Open
matthieu-m opened this issue May 4, 2024 · 2 comments · May be fixed by #6173
Open

rustfmt removes inner attributes from inline const blocks #6158

matthieu-m opened this issue May 4, 2024 · 2 comments · May be fixed by #6173
Assignees
Labels
bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors

Comments

@matthieu-m
Copy link

matthieu-m commented May 4, 2024

Clippy unfortunately warns about asserts whose conditions can be evaluated at compile-time even in inline const blocks. It's a known issue, it'll hopefully be solved at some point.

In the meantime, if one wishes to perform such compile-time asserts without disabling the lint globally, one can simply allow the offending lint:

fn main() {
    const {
        #![allow(clippy::assertions_on_constants)]

        assert!(1 < 2);
    }
}

Unfortunately, running rustfmt on the above will simply remove the attribute (try it on the playground), resulting in:

fn main() {
    const {
        assert!(1 < 2);
    }
}

And then Clippy nags at us again :'(

With inline const blocks due to being stabilized for 1.79 (in 6 weeks), it would be nice to fix rustfmt to not eat their code away.

Meta

rustfmt version:

1.7.0-nightly (2024-05-03 d2d24e3)
@ytmimi ytmimi added the bug Panic, non-idempotency, invalid code, etc. label May 6, 2024
@ytmimi
Copy link
Contributor

ytmimi commented May 6, 2024

I think the issue here is that the inner attributes are stored on the outer ast::Expr node with the kind of ast::ExprKind::ConstBlock(AnonConst). Internally the ast::AnonConst holds a reference to an ast::ExprKind::Block expression that we want to rewrite, but it doesn't hold a reference to the attributes.

We can probably solve this by matching on the AnonConst's inner expression kind and calling rewrite_block if it's an ast::ExprKind::Block:

rustfmt/src/expr.rs

Lines 141 to 143 in d5f1200

ast::ExprKind::ConstBlock(ref anon_const) => {
Some(format!("const {}", anon_const.rewrite(context, shape)?))
}

@ytmimi ytmimi added the good first issue Issues up for grabs, also good candidates for new rustfmt contributors label May 6, 2024
@WeiTheShinobi
Copy link

@rustbot claim

@ytmimi ytmimi changed the title rustfmt eats attributes in inline const blogs rustfmt removes inner attributes from inline const blocks May 23, 2024
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. good first issue Issues up for grabs, also good candidates for new rustfmt contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants