dtolnay / paste

Macros for all your token pasting needs
Apache License 2.0
1.02k stars 56 forks source link

use of undeclared crate or module `paste` #69

Closed smmoosavi closed 2 years ago

smmoosavi commented 2 years ago

I have two crates, a lib that defined macro and an app that uses that macro. and I get this error:

use of undeclared crate or module `paste`

tree:

├── Cargo.lock
├── Cargo.toml
├── app
│   └── my-app
│       ├── Cargo.toml
│       └── src
│           └── main.rs
└── lib
    └── my-macro
        ├── Cargo.toml
        └── src
            ├── lib.rs
            └── the_macro.rs

lib

./lib/my-macro/src/the_macro.rs

#[macro_export]
macro_rules! the_macro {
    ($result:ident, $field:ident) => {
        paste::paste! {
            struct $result {
                [<$field>]: String,
            }
        }
    };
}

./lib/my-macro/src/lib.rs

mod the_macro;

./lib/my-macro/Cargo.toml

[package]
name = "my-macro"
version = "0.1.0"
edition = "2021"

[dependencies]
paste = "1.0"

app

./app/my-app/src/main.rs

use my_macro::the_macro;

the_macro!(Foo, bar);

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

./app/my-app/Cargo.toml

[package]
name = "my-app"
version = "0.1.0"
edition = "2021"

[dependencies]
my-macro = { path = "../../lib/my-macro" }

root

[workspace]
members = [
    "app/my-app",
    "lib/*"
]
default-members = ["app/my-app"]

Error

error[E0433]: failed to resolve: use of undeclared crate or module `paste`
 --> app/my-app/src/main.rs:3:1
  |
3 | the_macro!(Foo, bar);
  | ^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `paste`
  |
  = note: this error originates in the macro `the_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0433`.
error: could not compile `my-app` due to previous error

sample repo

smmoosavi commented 2 years ago

I found the solution in this post

lib.rs

mod the_macro;
+pub extern crate paste;

the_macro.rs

#[macro_export]
macro_rules! the_macro {
    ($result:ident, $field:ident) => {
-        paste::paste! {
+        $crate::paste::paste! {
            struct $result {
                [<$field>]: String,
            }
        }
    };
}

you can find the fix commit here