Skip to content

an empty :vis doesn't work in trait declaration #65041

Closed
@limira

Description

@limira

I want to create a macro that generate fns that use in both impl Item... and trait TraitName. In trait, pub must be omitted, but in impl SomeStruct I need pub, so the macro need a $vis:vis like this:

macro_rules! create_method {
    ($vis:vis $name:ident) => {
        $vis fn $name(&self) {
            println!("{}", stringify!($name));
        }
    }
}

trait T1 {
    // This not works
    create_method!(method_of_t1);
}

trait T2 {
    fn method_of_t2(&self);
}

struct Struct;

impl T2 for Struct {
    // This works
    create_method!(method_of_t2);
}

impl Struct {
    // I want this method to be `pub`, so the macro need a `:vis`
    // for receiving the visibility keyword like this:
    create_method!(pub another_method);
}

Expected result: create_method!(method_of_t1); in trait T1 should works.

(Edit: a better comment about the :vis in impl Struct)

Metadata

Metadata

Assignees

Labels

A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-parserArea: The lexing & parsing of Rust source code to an ASTA-visibilityArea: Visibility / privacyC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions