rust-lang / rust-lang/rust

Diagnostic talks about and prints entire function *type* on private function in expansion of macro 2.0

Open
#143,455 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-decl-macros-2-0 A-diagnostics A-visibility D-confusing D-verbose F-decl_macro P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

In a project of mine I just got confronted with the following mouthful diagnostic:

error: type `for<'a, 'b> fn(&'a Query<EngineKind, std::result::Result<std::string::String, ()>>, for<'a> fn(EngineKind, context::Context<'a>) -> std::result::Result<std::string::String, ()>, EngineKind, context::Context<'b>) -> std::result::Result<std::string::String, ()> {invoke::<EngineKind, std::result::Result<std::string::String, ()>>}` is private
   --> src/context.rs:73:5
    |
73  |     invoke(&$cx.store().$query, $query, $input, $cx)
    |     ^^^^^^ private type
    |
   ::: src/build.rs:746:9
    |
746 |         crate::context::invoke!(cx.query_engine_path(self))
    |         --------------------------------------------------- in this macro invocation
    |
    = note: this error originates in the macro `crate::context::invoke` (in Nightly builds, run with -Z macro-backtrace for more info)

The underlying user error is the fact that the expansion of macro invoke contains a function (invoke) that isn't public enough at the usage site.

While rustc nicely highlights the private function, instead of talking about a private function, it talks about the visibility of the corresponding function type. That's technically correct but slightly confusing. Moreover, it blasts out the entire function type into the primary message (that I find most problematic)!

Primary messages should generally not contain things whose textual representation can easily grow arbitrarily large like types (I thought I once read about this in the diagnostic output style guide but apparently I'm misremembering).

In my case, I encountered this diagnostic as an "end-of-line diagnostic" in my editor (shown alongside the source code) and thus the only thing I basically saw was type `for<'a, 'b> fn(XYZXYZXZY....


Here's an MCVE:

#![feature(decl_macro)]

mod chamber {
    pub(crate) macro invoke { () => { invoke() } }
    fn invoke() {}
}

fn main() { chamber::invoke!(); }
error: type `fn() {invoke}` is private
 --> src/main.rs:4:39
  |
4 |     pub(crate) macro invoke { () => { invoke() } }
  |                                       ^^^^^^ private type
...
8 | fn main() { chamber::invoke!(); }
  |             ------------------ in this macro invocation
  |
  = note: this error originates in the macro `chamber::invoke` (in Nightly builds, run with -Z macro-backtrace for more info)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Rust MCVE and compare its diagnostic with the reported output from src/context.rs and src/build.rs. Trace the privacy error produced by the macro expansion; done means the primary message identifies the private function without printing its arbitrarily large function type.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.