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

Generated code is not public with macro generated impl fn #22176

Closed
hugoduncan opened this issue Feb 11, 2015 · 1 comment
Closed

Generated code is not public with macro generated impl fn #22176

hugoduncan opened this issue Feb 11, 2015 · 1 comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

Comments

@hugoduncan
Copy link

When generating a public fn with impl code, the pub keyword is ignored, and the resulting function is private (and raises a dead_code warning).

It is also not possible to move the pub keyword into the macro, as it then causes an error:

 expected one of `extern`, `fn`, or `unsafe`, found `pub`

Related issues: #4621, #11403

The workaround suggested in #4621 (generating multiple impl blocks) is unsatisfactory for public impl functions, as it does not allow for documenting the function, and produces doc output with repeated impl Type headers for each function.

The use case for this is generating code for library bindings.

The following code reproduces the issue:

macro_rules! defn {
    ($n:ident) => (
        fn $n (&self) -> i32 { 1 } )
}

#[derive(Copy)]
pub struct S;

impl S {
    pub defn!(f);
}


#[cfg(test)]
mod tests {
    use super::S;

    #[test]
    fn test_f() {
        let s=S;
        assert_eq!(1, s.f());
    }
}
@jdm jdm added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Feb 11, 2015
@hugoduncan
Copy link
Author

Duplicate of #18317

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

2 participants