Skip to content

Impossible to have a macro expand to pub method #17635

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

Closed
mitsuhiko opened this issue Sep 29, 2014 · 3 comments
Closed

Impossible to have a macro expand to pub method #17635

mitsuhiko opened this issue Sep 29, 2014 · 3 comments
Labels
A-syntaxext Area: Syntax extensions

Comments

@mitsuhiko
Copy link
Contributor

Currently a macro cannot create a pub fn. It can however create a regular fn.

#![feature(macro_rules)]

macro_rules! foo (
    () => (
        pub fn foo() -> int {
            42
        }
    )
)

struct X;

impl X {
    foo!()
}

fn main() {
    println!("{}", X::foo());
}

Fails with:

$ rustc test.rs
test.rs:5:9: 5:12 error: unexpected token: `pub`
test.rs:5         pub fn foo() -> int {
                  ^~~
@huonw huonw changed the title Impossbile to have a macro expand to pub fn Impossbile to have a macro expand to pub method Sep 29, 2014
@huonw huonw changed the title Impossbile to have a macro expand to pub method Impossible to have a macro expand to pub method Sep 29, 2014
@huonw
Copy link
Member

huonw commented Sep 29, 2014

This is a problem with pub methods specifically, it works fine if foo! is used to define a normal top-level function (i.e. not inside an impl).

@huonw huonw added the A-syntaxext Area: Syntax extensions label Sep 29, 2014
@shepmaster
Copy link
Member

Dupe of #17436 ?

@huonw
Copy link
Member

huonw commented Nov 9, 2014

@shepmaster, thanks.

@huonw huonw closed this as completed Nov 9, 2014
lnicola pushed a commit to lnicola/rust that referenced this issue Jul 28, 2024
RalfJung pushed a commit to RalfJung/rust that referenced this issue Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

3 participants