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

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

Closed
Lymkwi opened this issue Jan 1, 2024 · 1 comment · Fixed by #119506
Closed
Labels
C-bug Category: This is a bug. I-cycle Issue: A query cycle occurred while none was expected T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Lymkwi
Copy link

Lymkwi commented Jan 1, 2024

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.

@Lymkwi Lymkwi 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 Jan 1, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 1, 2024
@compiler-errors
Copy link
Member

This is a duplicate of #119346, so I'll close this in favor of that. I do agree that it's a diagnostic issue, so I will fix it.

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 2, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 5, 2024
…bject-safety-error, r=Nilstrieb

Use `resolutions(()).effective_visiblities` to avoid cycle errors in `report_object_error`

Inside of `report_object_error`, using the `effective_visibilities` query causes cycles since it calls `type_of`, which itself may call `typeck`, which may end up reporting its own object-safety errors.

Fixes rust-lang#119346
Fixes rust-lang#119502
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 5, 2024
…bject-safety-error, r=Nilstrieb

Use `resolutions(()).effective_visiblities` to avoid cycle errors in `report_object_error`

Inside of `report_object_error`, using the `effective_visibilities` query causes cycles since it calls `type_of`, which itself may call `typeck`, which may end up reporting its own object-safety errors.

Fixes rust-lang#119346
Fixes rust-lang#119502
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 5, 2024
…bject-safety-error, r=Nilstrieb

Use `resolutions(()).effective_visiblities` to avoid cycle errors in `report_object_error`

Inside of `report_object_error`, using the `effective_visibilities` query causes cycles since it calls `type_of`, which itself may call `typeck`, which may end up reporting its own object-safety errors.

Fixes rust-lang#119346
Fixes rust-lang#119502
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 5, 2024
…bject-safety-error, r=Nilstrieb

Use `resolutions(()).effective_visiblities` to avoid cycle errors in `report_object_error`

Inside of `report_object_error`, using the `effective_visibilities` query causes cycles since it calls `type_of`, which itself may call `typeck`, which may end up reporting its own object-safety errors.

Fixes rust-lang#119346
Fixes rust-lang#119502
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 5, 2024
…bject-safety-error, r=Nilstrieb

Use `resolutions(()).effective_visiblities` to avoid cycle errors in `report_object_error`

Inside of `report_object_error`, using the `effective_visibilities` query causes cycles since it calls `type_of`, which itself may call `typeck`, which may end up reporting its own object-safety errors.

Fixes rust-lang#119346
Fixes rust-lang#119502
@bors bors closed this as completed in 8309063 Jan 5, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 5, 2024
Rollup merge of rust-lang#119506 - compiler-errors:visibilities-for-object-safety-error, r=Nilstrieb

Use `resolutions(()).effective_visiblities` to avoid cycle errors in `report_object_error`

Inside of `report_object_error`, using the `effective_visibilities` query causes cycles since it calls `type_of`, which itself may call `typeck`, which may end up reporting its own object-safety errors.

Fixes rust-lang#119346
Fixes rust-lang#119502
@fmease fmease added I-cycle Issue: A query cycle occurred while none was expected C-bug Category: This is a bug. and removed A-diagnostics Area: Messages for errors, warnings, and lints labels Jan 27, 2024
hduoc2003 added a commit to hduoc2003/axum-diesel-todoapp that referenced this issue Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-cycle Issue: A query cycle occurred while none was expected T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants