Skip to content

decl_macro can call pub fn methods of private types. #155631

Description

@theemathas

I tried this code:

#![feature(decl_macro)]
macro my_macro($val:expr) {
    $val.method()
}

pub mod module {
    struct Thing;
    impl Thing {
        pub fn method(&self) -> bool {
            true
        }
    }
    pub fn weird() -> bool {
        super::my_macro!(Thing)
    }
}

The method is a pub fn. However, it is implemented on a private type. Therefore, it should not be callable from outside the module. So, this code shouldn't compile. Instead, this code compiles without errors.

Changing pub fn method to fn method causes the following error, as expected:

error[E0624]: method `method` is private
  --> src/lib.rs:3:10
   |
 3 |     $val.method()
   |          ^^^^^^ private method
...
 9 |         fn method(&self) -> bool {
   |         ------------------------ private method defined here
...
14 |         super::my_macro!(Thing)
   |         ----------------------- in this macro invocation
   |
   = note: this error originates in the macro `super::my_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0624`.

This behavior also seems to apply to built-in macros. See #155598 (comment)

Meta

Reproducible on the playground with 1.97.0-nightly (2026-04-20 66da6cae1a6f12e95854)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)A-hygieneArea: Macro hygieneA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-method-lookupArea: Method lookup (typeck, post `rustc_resolve`, not to be confused with `A-resolve`)A-visibilityArea: Visibility / privacyC-bugCategory: This is a bug.F-decl_macro`#![feature(decl_macro)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions