-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Problem with pub keyword in macros #18317
Comments
Any news on it ? |
Related? #14660 |
I think. |
I just encountered this. I can reproduce it with: macro_rules! defn {
($n:ident) => (
fn $n (&self) -> i32 {
println!("{}", stringify!($n));
1
}
)
}
#[derive(Copy)]
pub struct S;
impl S {
// Attempt to define a public method via a macro.
// The resulting method ends up being non-public.
pub defn!(f);
} |
Now let's just hope it'll be fixed sooner or later ! |
Hm, what should We might want to forbid The current workaround is to write item_macro!(pub thing ...); The precise syntax is up to the macro author and can accommodate multiple items. This workaround would be less annoying if we had a "0 or 1" repeat operator in macros. |
It's not clear what this means, because a macro in item position can expand to zero or more items. For now we disallow it, which is technically a [breaking-change] but is landing without an RFC. The `pub` keyword previously had no effect, which seems quite unintended. Fixes rust-lang#18317. Fixes rust-lang#14660.
It's not clear what this means, because a macro in item position can expand to zero or more items. For now we disallow it, which is technically a [breaking-change] but is landing without an RFC. The `pub` keyword previously had no effect, which seems quite unintended. Fixes rust-lang#18317. Fixes rust-lang#14660.
@kmcallister Could you be a little more explicit about the workaround - I can't seem to get it to work. |
@hugoduncan: Here's an example from the compiler itself. This approach from lazy-static looks better, with less duplicated code. |
@kmcallister Thanks for the examples. If I understand them correctly (neither of which generate macro_rules! defn {
(pub $n:ident) => (
pub fn $n (&self) -> i32 { 1 } )
}
#[derive(Copy)]
pub struct S;
impl S {
defn!(pub f);
}
#[cfg(test)]
mod tests {
use super::S;
#[test]
fn test_f() {
let s=S;
assert_eq!(1, s.f());
}
} |
Thanks for the confirmation. I'll be looking forward to a new nightly for once :) |
minor: sync from downstream
Hey,
I have an issue on my macros use. The source code of my macro is here. I can't put the pub keyword and if I call my macro like this :
The methods aren't public. Am I doing something wrong or is it a compiler's bug ?
Thanks in advance.
The text was updated successfully, but these errors were encountered: