rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.8k stars 12.66k forks source link

Seems like quantificator symbol do not works correctly in macros expression #103256

Open sergio-ivanuzzo opened 2 years ago

sergio-ivanuzzo commented 2 years ago

I tried this code:

#[macro_export]
macro_rules! vec2 {
    ( $( $x:expr ),* ) => {
        {
            let mut temp_vec = Vec::new();
            $(
                temp_vec.push($x);
            )? // this one works the same as below
            temp_vec
        }
    };
}

#[macro_export]
macro_rules! vec3 {
    ( $( $x:expr ),* ) => {
        {
            let mut temp_vec = Vec::new();
            $(
                temp_vec.push($x);
            )* // this one works the same as above
            temp_vec
        }
    };
}

I expected to see this happen: macros expression that use "?" should return error

Instead, this happened: both macros works in same way

Meta

rustc --version --verbose:

rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-pc-windows-msvc
release: 1.60.0
LLVM version: 14.0.0
PatchMixolydic commented 2 years ago

This is caught by the allow-by-default meta_variable_misuse lint (playground):

warning: meta-variable repeats with different Kleene operator
  --> src/lib.rs:9:31
   |
5  |     ( $( $x:expr ),* ) => {
   |                    - expected repetition
...
9  |                 temp_vec.push($x);
   |                               ^^
10 |             )? // this one works the same as below
   |              - conflicting repetition
   |
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(meta_variable_misuse)]
   |         ^^^^^^^^^^^^^^^^^^^^

See also #61053, the original tracking issue for the lint, and #95943, which contains a discussion on why this lint is allow-by-default.

sergio-ivanuzzo commented 2 years ago

This is caught by the allow-by-default meta_variable_misuse lint (playground):

warning: meta-variable repeats with different Kleene operator
  --> src/lib.rs:9:31
   |
5  |     ( $( $x:expr ),* ) => {
   |                    - expected repetition
...
9  |                 temp_vec.push($x);
   |                               ^^
10 |             )? // this one works the same as below
   |              - conflicting repetition
   |
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(meta_variable_misuse)]
   |         ^^^^^^^^^^^^^^^^^^^^

See also #61053, the original tracking issue for the lint, and #95943, which contains a discussion on why this lint is allow-by-default.

So this lint supposed to be used only in debug purposes right ?

PatchMixolydic commented 2 years ago

As far as I'm aware, you can leave it turned on as long as you're willing to accept a few false positives. For what its worth, I don't remember encountering any false positives, but this lint has saved me a bit of trouble several times.

fmease commented 8 months ago

Some discussion happened here: Zulip | #wg-macros > Repetition operator mismatch between transcriber and matcher.