dtolnay / paste

Macros for all your token pasting needs
Apache License 2.0
983 stars 53 forks source link

Can't paste together `use` import #67

Closed thomwiggers closed 2 years ago

thomwiggers commented 3 years ago

I'm trying to construct an identifier of a namespace from parts, but that does not seem to be supported.

use paste::paste;

mod test {
    const X: u32 = 2;
}

use paste!{ [< te st >] }::X;

fn main() {
    println!("Hello, world!, {}", X);
}

results in

   Compiling playground v0.0.1 (/playground)
error: expected one of `::`, `;`, or `as`, found `!`
 --> src/main.rs:7:10
  |
7 | use paste!{ [< te st >] }::X;
  |          ^ expected one of `::`, `;`, or `as`

error: aborting due to previous error

error: could not compile `playground`

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5cb2fb8f9b67684f1a271dd51fedffc7

marmidr commented 2 years ago

I think this is not related to the use keyword, but rather attempt to glue tokens with special characters, like :. In my case, I can't glue Enum class name and it's enumerator

enum States{
  Unknowm,
  On, 
  Off
}

let x = paste!{ [<States::  Off>] ; }

error: expected identifier after :

Surprised BTW - the ## operator in C/C++ is so simple and obvious :/

dtolnay commented 2 years ago

In @thomwiggers snippet use macroname!{...} is not valid Rust syntax. You'd want to write:

paste! {
    use [<te st>]::X;
}