Skip to content

Cycle when computing type of recursive opaque type #87850

Closed
@LeSeulArtichaut

Description

I tried this code:

#![feature(rustc_attrs)]

#[rustc_layout_scalar_valid_range_start(1)]
struct NonZero<T>(T);

fn closure_ref_capture() -> NonZero<impl Sized> {
    let x = closure_ref_capture();
    unsafe { NonZero(move || { &x.0; }) }
}

rustc gives the following output:

error[E0391]: cycle detected when computing type of `closure_ref_capture::{opaque#0}`
 --> test.rs:6:37
  |
6 | fn closure_ref_capture() -> NonZero<impl Sized> {
  |                                     ^^^^^^^^^^
  |
note: ...which requires borrow-checking `closure_ref_capture`...
 --> test.rs:6:1
  |
6 | fn closure_ref_capture() -> NonZero<impl Sized> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires processing `closure_ref_capture`...
 --> test.rs:6:1
  |
6 | fn closure_ref_capture() -> NonZero<impl Sized> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires processing MIR for `closure_ref_capture`...
 --> test.rs:6:1
  |
6 | fn closure_ref_capture() -> NonZero<impl Sized> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires building MIR for `closure_ref_capture`...
 --> test.rs:6:1
  |
6 | fn closure_ref_capture() -> NonZero<impl Sized> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires unsafety-checking `closure_ref_capture`...
 --> test.rs:8:33
  |
8 |     unsafe { NonZero(move || { &x.0; }) }
  |                                 ^^^
  = note: ...which requires computing whether `impl Sized` is freeze...
  = note: ...which requires evaluating trait selection obligation `impl Sized: std::marker::Freeze`...
  = note: ...which again requires computing type of `closure_ref_capture::{opaque#0}`, completing the cycle
note: cycle used when checking item types in top-level module
 --> test.rs:1:1
  |
1 | / #![feature(rustc_attrs)]
2 | |
3 | | #[rustc_layout_scalar_valid_range_start(1)]
4 | | struct NonZero<T>(T);
... |
8 | |     unsafe { NonZero(move || { &x.0; }) }
9 | | }
  | |_^

warning: function cannot return without recursing
 --> test.rs:6:1
  |
6 | fn closure_ref_capture() -> NonZero<impl Sized> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
7 |     let x = closure_ref_capture();
  |             --------------------- recursive call site
  |
  = note: `#[warn(unconditional_recursion)]` on by default
  = help: a `loop` may express intention better if this is on purpose

error[E0720]: cannot resolve opaque type
 --> test.rs:6:37
  |
6 | fn closure_ref_capture() -> NonZero<impl Sized> {
  |                                     ^^^^^^^^^^ recursive opaque type
7 |     let x = closure_ref_capture();
8 |     unsafe { NonZero(move || { &x.0; }) }
  |              -------------------------- returning here with type `NonZero<[closure@test.rs:8:22: 8:39]>`

error: aborting due to 3 previous errors; 1 warning emitted

This affects both the MIR and the THIR unsafety checkers. However, since the code is obviously wrong and the relevant errors are still emitted, I'd expect this to be closed as wontfix.

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (5ad7389bd 2021-08-06)
binary: rustc
commit-hash: 5ad7389bdd1abe7d2c6f73a233af1a7a69e96285
commit-date: 2021-08-06
host: x86_64-unknown-linux-gnu
release: 1.56.0-nightly
LLVM version: 12.0.1

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

    -Zthir-unsafeckUnstable option: THIR unsafeckA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-type-systemArea: Type systemC-bugCategory: This is a bug.F-rustc_attrsInternal rustc attributes gated on the `#[rustc_attrs]` feature gate.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