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

visibility modifiers ignored for method-defining macros #21412

Closed
blaenk opened this issue Jan 20, 2015 · 4 comments
Closed

visibility modifiers ignored for method-defining macros #21412

blaenk opened this issue Jan 20, 2015 · 4 comments
Assignees
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

Comments

@blaenk
Copy link
Contributor

blaenk commented Jan 20, 2015

I created a macro that intended to define a public static method in an implementation. However, the pub modifier is essentially ignored. First, it's apparently not allowed within the macro definition, and then it's ignored if I put it outside and before the macro call:

mod m {
    macro_rules! render_function {
        ($name:ident) => (
            fn $name(output: &mut i32, input: &i32) -> i32 {
                5i32
            }
        );
    }

    struct S;

    impl S {
        fn x() {}

        pub render_function!(test);
    }
}

fn main() {
    let x = m::S::test(&mut 3, &4);
}
<anon>:20:13: 20:23 error: static method `test` is private
<anon>:20     let x = m::S::test(&mut 3, &4);

playpen

@kmcallister kmcallister added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Jan 20, 2015
@kmcallister kmcallister self-assigned this Jan 20, 2015
@blaenk
Copy link
Contributor Author

blaenk commented Jan 20, 2015

For completeness I should state what is probably already obvious, that as a workaround I ended up having to contain an entire impl within the macro definition which itself contained the function definition, like this:

macro_rules! render_function {
    ($name:ident) => (
        impl S {
            pub fn $name(output: &mut i32, input: &i32) -> i32 {
                5i32
            }
        }
    );
}

@blaenk
Copy link
Contributor Author

blaenk commented Jan 20, 2015

If I change this to define a function in a module (instead of in a struct impl), it does allow me to put pub before the fn within the macro definition and it isn't ignored, whereas, as I said in the first post, if I put pub fn in the macro definition and then call the macro inside the struct impl, it would complain about a syntax error.

@shepmaster
Copy link
Member

Dupe of #17436 or #18317 or #14660?

@alexcrichton
Copy link
Member

I believe so, thanks @shepmaster!

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

No branches or pull requests

4 participants