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 visibilities when casting struct into trait object. #121342

Closed
Sol-Ell opened this issue Feb 20, 2024 · 1 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.

Comments

@Sol-Ell
Copy link

Sol-Ell commented Feb 20, 2024

Code

struct A;

trait Trait {
    async fn f();
}

impl Trait for A {
    async fn f() {
        &A as &dyn Trait;
    }
}

Current output

error[E0391]: cycle detected when checking effective visibilities
   |
note: ...which requires computing type of `<impl at src\lib.rs:7:1: 7:17>::f::{opaque#0}`...
  --> src\lib.rs:8:5
   |
8  |     async fn f() {
   |     ^^^^^^^^^^^^
note: ...which requires computing type of opaque `<impl at src\lib.rs:7:1: 7:17>::f::{opaque#0}`...
  --> src\lib.rs:8:5
   |
8  |     async fn f() {
   |     ^^^^^^^^^^^^
note: ...which requires type-checking `<impl at src\lib.rs:7:1: 7:17>::f`...
  --> src\lib.rs:8:5
   |
8  |     async fn f() {
   |     ^^^^^^^^^^^^
   = note: ...which again requires checking effective visibilities, completing the cycle
note: cycle used when checking item types in top-level module
  --> src\lib.rs:1:1
   |
1  | / struct A;
2  | |
3  | | trait Trait {
4  | |     async fn f();
...  |
10 | |     }
11 | | }
   | |_^
   = 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

Desired output

No response

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-pc-windows-msvc
release: 1.76.0
LLVM version: 17.0.6

Anything else?

Possibly related to:

@Sol-Ell Sol-Ell 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 Feb 20, 2024
@Sol-Ell Sol-Ell changed the title cycle detected when checking effective visibilities Cycle detected when checking effective visibilities when casting struct into trait object. Feb 20, 2024
@compiler-errors
Copy link
Member

This is fixed on nightly:

error[E0038]: the trait `Trait` cannot be made into an object
 --> src/lib.rs:9:15
  |
9 |         &A as &dyn Trait;
  |               ^^^^^^^^^^ `Trait` 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/lib.rs:4:14
  |
3 | trait Trait {
  |       ----- this trait cannot be made into an object...
4 |     async fn f();
  |              ^ ...because associated function `f` has no `self` parameter
  = help: only type `A` implements the trait, consider using it directly instead
help: consider turning `f` into a method by giving it a `&self` argument
  |
4 |     async fn f(&self);
  |                +++++
help: alternatively, consider constraining `f` so it does not apply to trait objects
  |
4 |     async fn f() where Self: Sized;
  |                  +++++++++++++++++

error[E0038]: the trait `Trait` cannot be made into an object
 --> src/lib.rs:9:9
  |
9 |         &A as &dyn Trait;
  |         ^^ `Trait` 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/lib.rs:4:14
  |
3 | trait Trait {
  |       ----- this trait cannot be made into an object...
4 |     async fn f();
  |              ^ ...because associated function `f` has no `self` parameter
  = help: only type `A` implements the trait, consider using it directly instead
  = note: required for the cast from `&A` to `&dyn Trait`
help: consider turning `f` into a method by giving it a `&self` argument
  |
4 |     async fn f(&self);
  |                +++++
help: alternatively, consider constraining `f` so it does not apply to trait objects
  |
4 |     async fn f() where Self: Sized;
  |                  +++++++++++++++++

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

Kind of verbose, but at least it mentions the reasons and fixes.

@compiler-errors compiler-errors closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2024
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

No branches or pull requests

2 participants