Skip to content
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

Closed
GuillaumeGomez opened this issue Oct 25, 2014 · 11 comments · Fixed by #22285
Closed

Problem with pub keyword in macros #18317

GuillaumeGomez opened this issue Oct 25, 2014 · 11 comments · Fixed by #22285
Assignees
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

Comments

@GuillaumeGomez
Copy link
Member

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 :

pub impl_GObjectFunctions!(t1, t2)

The methods aren't public. Am I doing something wrong or is it a compiler's bug ?

Thanks in advance.

@GuillaumeGomez
Copy link
Member Author

Any news on it ?

@kmcallister
Copy link
Contributor

Related? #14660

@GuillaumeGomez
Copy link
Member Author

I think.

@steveklabnik steveklabnik added O-macos Operating system: macOS A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) and removed O-macos Operating system: macOS labels Jan 27, 2015
@hugoduncan
Copy link

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);
}

@GuillaumeGomez
Copy link
Member Author

Now let's just hope it'll be fixed sooner or later !

@kmcallister kmcallister self-assigned this Feb 2, 2015
@kmcallister kmcallister changed the title Problem with pub keywork in macros Problem with pub keyword in macros Feb 3, 2015
@kmcallister
Copy link
Contributor

Hm, what should pub item_macro!(); actually mean? A macro in item position can expand to zero or more items. Should the pub qualifier apply to all of them, or should it be an error to use it with a multi-item macro?

We might want to forbid pub in this position for 1.0, and come back to the issue later. I had a design for item macro sugar that I think would be rather nice.

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.

kmcallister added a commit to kmcallister/rust that referenced this issue Feb 13, 2015
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.
Manishearth added a commit to Manishearth/rust that referenced this issue Feb 15, 2015
 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.
@hugoduncan
Copy link

@kmcallister Could you be a little more explicit about the workaround - I can't seem to get it to work.

@kmcallister
Copy link
Contributor

@hugoduncan: Here's an example from the compiler itself.

This approach from lazy-static looks better, with less duplicated code.

@hugoduncan
Copy link

@kmcallister Thanks for the examples. If I understand them correctly (neither of which generate fn forms, iiuc), I should be matching on pub and inserting a literal pub in the expansion, but this does not seem to work :

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());
    }
}

@kmcallister
Copy link
Contributor

It works for me on rustc 1.0.0-dev (e4e7aa2 2015-02-16). The playpen is just too old to have my fix for #17436.

@hugoduncan
Copy link

Thanks for the confirmation. I'll be looking forward to a new nightly for once :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants