Skip to content

cycle detected when checking effective visibility hides relevant diagnostics message #119502

Description

@Lymkwi

Code

trait Useless {}
impl Useless for () {}
trait Source {
    fn a_function(self) -> impl Useless;
}
struct ABot;

impl Source for ABot {
    fn a_function(self) -> impl Useless {}
}

trait Sink {
    fn run(self) -> impl Useless;
}

struct Bot {
    pub sources: Vec<Box<dyn Source>>,
}

impl Sink for Bot {
    fn run(self) -> impl Useless {
        let _ = self.sources.len();
    }
}

fn main() {}

Current output

error[E0391]: cycle detected when checking effective visibilities
   |
note: ...which requires computing type of `<impl at src/main.rs:20:1: 20:18>::run::{opaque#0}`...
  --> src/main.rs:21:21
   |
21 |     fn run(self) -> impl Useless {
   |                     ^^^^^^^^^^^^
note: ...which requires computing type of opaque `<impl at src/main.rs:20:1: 20:18>::run::{opaque#0}`...
  --> src/main.rs:21:21
   |
21 |     fn run(self) -> impl Useless {
   |                     ^^^^^^^^^^^^
note: ...which requires type-checking `<impl at src/main.rs:20:1: 20:18>::run`...
  --> src/main.rs:21:5
   |
21 |     fn run(self) -> impl Useless {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: ...which again requires checking effective visibilities, completing the cycle
note: cycle used when checking that `Bot` is well-formed
  --> src/main.rs:16:1
   |
16 | struct Bot {
   | ^^^^^^^^^^
   = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

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

Desired output

error[E0038]: the trait `Source` cannot be made into an object
  --> src/main.rs:17:26
   |
17 |     pub sources: Vec<Box<dyn Source>>,
   |                          ^^^^^^^^^^ `Source` cannot be made into an object
   |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> src/main.rs:4:29
   |
3  | trait Source {
   |       ------ this trait cannot be made into an object...
4  |     fn a_function(&self) -> impl Useless;
   |                             ^^^^^^^^^^^^ ...because method `a_function` references an `impl Trait` type in its return type
   = help: consider moving `a_function` to another trait
   = help: only type `ABot` implements the trait, consider using it directly instead

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

Rationale and extra context

No response

Other cases

No response

Anything else?

This code is originally extracted from a reduction of a problem found hit on a friend's project. I reduced the code down to something that, confusingly, provides an incorrect diagnostics message.
Originally, the -> impl Useless dynamic dispatch were async fn, I just substituted them. The nightly toolchain as of today behaves the same.

As a note, the correct output is provided if the statement let _ = self.sources.len(); is commented out, which is why I think this is related to #119346 , but maybe not the same.

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

    C-bugCategory: This is a bug.I-cycleIssue: A query cycle occurred while none was expectedT-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