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

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

Closed
5225225 opened this issue Jul 9, 2022 · 4 comments · Fixed by #99091
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@5225225
Copy link
Contributor

5225225 commented Jul 9, 2022

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

@5225225 5225225 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 9, 2022
@compiler-errors
Copy link
Member

@5225225, this only happens with macro items and not macro_rules! global_meow?

@5225225
Copy link
Contributor Author

5225225 commented Jul 9, 2022

I included examples of macro and macro_rules!, and it seems to happen for both (see: library::global_meow_rules!)

@compiler-errors
Copy link
Member

Oh, my bad. I didn't read closely, my eyes just went directly to the macro item. Sorry. 😅

@compiler-errors
Copy link
Member

compiler-errors commented Jul 9, 2022

For the record, this is not a macro problem. See this example that does not use macros:

/// ```
/// fn needs_meow<T: playground::Meow>(t: T) {}
/// fn main() { needs_meow(1usize); }
/// ```
pub trait Meow {
    fn meow(&self) {}
}

pub struct GlobalMeow;

impl Meow for GlobalMeow { }

pub(crate) struct PrivateMeow;

impl Meow for PrivateMeow { }

(uses a doc comment to emulate two crates on playground)

The reason is that InferCtxt::find_similar_impl_candidates does not do any filtering for type privacy.

@compiler-errors compiler-errors self-assigned this Jul 9, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 11, 2022
…d-stay-private, r=lcnr

Do not mention private types from other crates as impl candidates

Fixes rust-lang#99080
@bors bors closed this as completed in 93f71d4 Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants