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 error instead of invalid function signature on trait #98260

Closed
AngelOnFira opened this issue Jun 19, 2022 · 7 comments · Fixed by #98642 or #98680
Closed

Cycle detected error instead of invalid function signature on trait #98260

AngelOnFira opened this issue Jun 19, 2022 · 7 comments · Fixed by #98642 or #98680
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

@AngelOnFira
Copy link
Member

AngelOnFira commented Jun 19, 2022

Given the following code: Link to playground

fn main() {}

trait A {
    fn a(aa: B) -> Result<_, B> {
        Ok(())
    }
}

enum B {}

The current output is:

   Compiling s1 v0.1.0 (/Users/forest/Documents/test/s1)
error[E0391]: cycle detected when computing function signature of `A::a`
 --> src/main.rs:3:5
  |
3 |     fn a(aa: B) -> Result<_, B> {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: ...which requires type-checking `A::a`...
 --> src/main.rs:3:5
  |
3 |     fn a(aa: B) -> Result<_, B> {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires computing the variances of `B`...
 --> src/main.rs:8:1
  |
8 | enum B {}
  | ^^^^^^
  = note: ...which requires computing the variances for items in this crate...
  = note: ...which again requires computing function signature of `A::a`, completing the cycle
note: cycle used when collecting item types in top-level module
 --> src/main.rs:1:1
  |
1 | fn main() {}
  | ^^^^^^^^^

For more information about this error, try `rustc --explain E0391`.
error: could not compile `s1` due to previous error

I expected the compiler to let me know that function signature was wrong with the _ in Result<_, B>. Once I changed this, it compiled fine.

@AngelOnFira AngelOnFira 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 Jun 19, 2022
@TaKO8Ki TaKO8Ki assigned TaKO8Ki and unassigned TaKO8Ki Jun 20, 2022
@yanchen4791
Copy link
Contributor

@rustbot claim

@yanchen4791
Copy link
Contributor

I've come up a solution with which the output of this test case will be like the following:

error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
 --> main.rs:3:27
  |
3 |     fn a(aa: B) -> Result<_, B> {
  |                           ^ not allowed in type signatures

error[E0391]: cycle detected when computing function signature of `A::a`
 --> main.rs:3:5
  |
3 |     fn a(aa: B) -> Result<_, B> {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: ...which requires type-checking `A::a`...
 --> main.rs:3:5
  |
3 |     fn a(aa: B) -> Result<_, B> {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires computing the variances of `B`...
 --> main.rs:8:1
  |
8 | enum B {}
  | ^^^^^^
  = note: ...which requires computing the variances for items in this crate...
  = note: ...which again requires computing function signature of `A::a`, completing the cycle
note: cycle used when collecting item types in top-level module
 --> main.rs:1:1
  |
1 | fn main() {}
  | ^^^^^^^^^

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0121, E0391.
For more information about an error, try `rustc --explain E0121`.

It is cumbersome for still having E0391 printed, but it seems not easy to remove this without a significant code changes. I think the first message E0121 "the placeholder _ is not allowed within types on item signatures for return types" should have told the user enough information how to fix the problem. Any comments/suggestions?

@spastorino
Copy link
Member

spastorino commented Jun 27, 2022

@yanchen4791 having an early return for when there's no generics on compiler/rustc_typeck/src/variance/mod.rs will solve this problem.

@yanchen4791
Copy link
Contributor

@spastorino Thanks a lot for the advise!

After the following code change, the faulty cycle detected message has gone way, and correct output generated.

--- a/compiler/rustc_typeck/src/variance/mod.rs
+++ b/compiler/rustc_typeck/src/variance/mod.rs
@@ -37,6 +37,12 @@ fn crate_variances(tcx: TyCtxt<'_>, (): ()) -> CrateVariancesMap<'_> {
 }

 fn variances_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[ty::Variance] {
+
+    // Skip items with no generics - there's nothing to infer in them.
+    if tcx.generics_of(item_def_id).count() == 0 {
+        return &[];
+    }
+
     match tcx.def_kind(item_def_id) {
         DefKind::Fn
         | DefKind::AssocFn

@yanchen4791
Copy link
Contributor

The output of the test case after the fix is correct:

error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
 --> main.rs:3:27
  |
3 |     fn a(aa: B) -> Result<_, B> {
  |                    -------^----
  |                    |      |
  |                    |      not allowed in type signatures
  |                    help: replace with the correct return type: `Result<(), B>`

error: aborting due to previous error

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

@spastorino
Copy link
Member

@yanchen4791 great, now you can send a pull request :).

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 29, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 29, 2022
…askrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#98434 (Ensure that `static_crt` is set in the bootstrapper whenever using `cc-rs` to get a compiler command line.)
 - rust-lang#98636 (Triagebot: Fix mentions word wrapping.)
 - rust-lang#98642 (Fix rust-lang#98260)
 - rust-lang#98643 (Improve pretty printing of valtrees for references)
 - rust-lang#98646 (rustdoc: fix bugs in main.js popover help and settings)
 - rust-lang#98647 (Update cargo)
 - rust-lang#98652 (`alloc`: clean and ensure `no_global_oom_handling`  builds are warning-free)
 - rust-lang#98660 (Unbreak stage1 tests via ignore-stage1 in `proc-macro/invalid-punct-ident-1.rs`.)
 - rust-lang#98665 (Use verbose help for deprecation suggestion)
 - rust-lang#98668 (Avoid some `&str` to `String` conversions with `MultiSpan::push_span_label`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in f97326d Jun 29, 2022
@AngelOnFira
Copy link
Member Author

Thanks for your work @yanchen4791!

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.

4 participants