Skip to content

"The following types implement trait..." suggets crate private items, if inside macro. #99080

Closed
@5225225

Description

@5225225

There are two crates here, library and consumer (which depends on library).

library:

#![feature(decl_macro)]

pub macro global_meow($item:expr) {
    const _: () = { 
        fn foo() {
            Meow::meow(&$item);
        }
    };
}

#[macro_export]
macro_rules! global_meow_rules {
    ($item:expr) => {
        const _: () = { 
            fn foo() {
                $crate::Meow::meow(&$item);
            }
        };
    }
}

pub trait Meow {
    fn meow(&self) {}
}

pub struct GlobalMeow;

impl Meow for GlobalMeow { }

pub(crate) struct PrivateMeow;

impl Meow for PrivateMeow { }

consumer:

static NOT_MEOW: usize = 0;

library::global_meow!(NOT_MEOW);

library::global_meow_rules!(NOT_MEOW);

fn main() {}

The current output is (when running cargo check on consumer):

# cargo check
    Checking consumer v0.1.0 (/tmp/scratcho2FpPeJDq/consumer)
error[E0277]: the trait bound `usize: Meow` is not satisfied
 --> src/main.rs:2:1
  |
2 | library::global_meow!(NOT_MEOW);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Meow` is not implemented for `usize`
  |
  = help: the following other types implement trait `Meow`:
            GlobalMeow
            library::PrivateMeow
  = note: this error originates in the macro `library::global_meow` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `usize: Meow` is not satisfied
 --> src/main.rs:4:1
  |
4 | library::global_meow_rules!(NOT_MEOW);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Meow` is not implemented for `usize`
  |
  = help: the following other types implement trait `Meow`:
            GlobalMeow
            library::PrivateMeow
  = note: this error originates in the macro `library::global_meow_rules` (in Nightly builds, run with -Z macro-backtrace for more info)

Ideally the output should look like:

# cargo check
    Checking consumer v0.1.0 (/tmp/scratcho2FpPeJDq/consumer)
error[E0277]: the trait bound `usize: Meow` is not satisfied
 --> src/main.rs:2:1
  |
2 | library::global_meow!(NOT_MEOW);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Meow` is not implemented for `usize`
  |
  = help: the following other types implement trait `Meow`:
            GlobalMeow
  = note: this error originates in the macro `library::global_meow` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `usize: Meow` is not satisfied
 --> src/main.rs:4:1
  |
4 | library::global_meow_rules!(NOT_MEOW);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Meow` is not implemented for `usize`
  |
  = help: the following other types implement trait `Meow`:
            GlobalMeow
  = note: this error originates in the macro `library::global_meow_rules` (in Nightly builds, run with -Z macro-backtrace for more info)

We should not be suggesting private types to users, since macro hygiene does mean they can't write library::PrivateMeow

This is on the latest nightly, rustc 1.64.0-nightly (06754d885 2022-07-08)

This was ran into in #99074

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler 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