Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Inconsistent expression precedence when using camel #94

Closed
9uapaw opened this issue Mar 22, 2023 · 1 comment
Closed

Inconsistent expression precedence when using camel #94

9uapaw opened this issue Mar 22, 2023 · 1 comment

Comments

@9uapaw
Copy link

9uapaw commented Mar 22, 2023

Hey! I have encountered an edge case with a surprising behavior. When using an expression in a macro along with camel and using a unary operator on the expression, the parentheses are eliminated resulting in a faulty short circuit.
Reproducible with:
paste = 1.0.12
rustc = 1.68.0

macro_rules! test {
    ($($name:ident $pred:expr),*) => {
        paste::paste! {
            $(
                if !$pred {
                    let camel = [<$name:camel>]::new();
                    println!("{}", $pred);
                }
            )*
        }
    }
}

fn main() {
    test!(String false || true);
}

Expanded to the following via cargo expand:

fn main() {
    if !false || true {
        let camel = String::new();
        {
            ::std::io::_print(format_args!("{0}\n", false || true));
        };
    }
}

Whereas without using camel it expands to:

fn main() {
    if !(false || true) {
        {
            ::std::io::_print(format_args!("{0}\n", false || true));
        };
    }
} 
@dtolnay
Copy link
Owner

dtolnay commented Mar 22, 2023

This is a rustc bug — paste! expands this input correctly (preserving invisible grouping of interpolated macro_rules metavariables) but rustc's parser that is used for parsing the tokens returned by a proc macro just totally ignores None-delimited groups as if they were not grouped at all. See rust-lang/rust#67062.

@dtolnay dtolnay closed this as completed Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants