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

False positive unused_braces when calling a macro #106545

Closed
peter-lyons-kehl opened this issue Jan 6, 2023 · 0 comments · Fixed by #106563
Closed

False positive unused_braces when calling a macro #106545

peter-lyons-kehl opened this issue Jan 6, 2023 · 0 comments · Fixed by #106563
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@peter-lyons-kehl
Copy link
Contributor

peter-lyons-kehl commented Jan 6, 2023

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

macro_rules! co_vec_default {
    () => {
        1
    }
}

struct V<const U: usize>
where
    [(); U]:;

trait Tr {}

impl Tr for V<{co_vec_default!()}> {}

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=058b224b06eefb23132dda5fad6f2950

The above generates a warning:

Compiling playground v0.0.1 (/playground)
warning: unnecessary braces around const expression
  --> src/lib.rs:16:15
   |
16 | impl Tr for V<{co_vec_default!()}> {}
   |               ^                 ^
   |
   = note: `#[warn(unused_braces)]` on by default
help: remove these braces

That above warning is either

  • false positive, or
  • directing a developer to an unimplemented rustc functionality or error, because without the braces it produces a (confusing) error. The same example code adjusted:
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

macro_rules! co_vec_default {
    () => {
        1
    }
}

struct V<const U: usize>
where
    [(); U]:;

trait Tr {}

impl Tr for V<co_vec_default!()> {}

Limited playground (I don't see a way to pass -Z macro-backtrace to the playground):
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=8b334f459ac48f56639de94f116a7705

The current output (with nightly invoked with RUSTFLAGS="-Z macro-backtrace" cargo build) is:

Compiling co_vec_macro v0.1.0 (/tmp/co_vec_macro)
error: expected type, found `1`
  --> src/lib.rs:6:9
   |
4  | / macro_rules! co_vec_default {
5  | |     () => {
6  | |         1
   | |         ^ expected type
7  | |     }
8  | | }
   | |_- in this expansion of `co_vec_default!`
...
16 |   impl Tr for V<co_vec_default!()> {}
   |                 -----------------
   |                 |
   |                 this macro call doesn't expand to a type
   |                 in this macro invocation

error[E0747]: type provided when a constant was expected
  --> src/lib.rs:16:15
   |
16 | impl Tr for V<co_vec_default!()> {}
   |               ^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0747`.

Thank you in advance for any progress.

@peter-lyons-kehl peter-lyons-kehl added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 6, 2023
@peter-lyons-kehl peter-lyons-kehl changed the title False negative unused_braces when calling a macro False positive unused_braces when calling a macro Jan 6, 2023
@bors bors closed this as completed in d7bc758 Jan 15, 2023
@dtolnay dtolnay added A-const-generics Area: const generics (parameters and arguments) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Jan 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants