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

False-positive "cycle detected" in const fn with RPIT #99793

Closed
yvt opened this issue Jul 27, 2022 · 1 comment · Fixed by #122192
Closed

False-positive "cycle detected" in const fn with RPIT #99793

yvt opened this issue Jul 27, 2022 · 1 comment · Fixed by #122192
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. 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

@yvt
Copy link
Contributor

yvt commented Jul 27, 2022

I tried this code:

const fn f() -> impl Eq { g() }
const fn g() {}
// OK: const fn f() -> impl Eq { g(); }
// OK: const fn f() -> impl Eq {}
// OK: const fn f() -> impl Eq { (g(),).0 }

(Playground)

I expected to see this happen: Successful compilation

Instead, this happened:

error[E0391]: cycle detected when computing type of `f::{opaque#0}`
 --> src/lib.rs:1:17
  |
1 | const fn f() -> impl Eq { g() }
  |                 ^^^^^^^
  |
note: ...which requires borrow-checking `f`...
 --> src/lib.rs:1:1
  |
1 | const fn f() -> impl Eq { g() }
  | ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires processing `f`...
 --> src/lib.rs:1:1
  |
1 | const fn f() -> impl Eq { g() }
  | ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires const checking `f`...
 --> src/lib.rs:1:1
  |
1 | const fn f() -> impl Eq { g() }
  | ^^^^^^^^^^^^^^^^^^^^^^^
  = note: ...which requires computing whether `impl core::cmp::Eq` is freeze...
  = note: ...which requires evaluating trait selection obligation `impl core::cmp::Eq: core::marker::Freeze`...
  = note: ...which again requires computing type of `f::{opaque#0}`, completing the cycle
note: cycle used when checking item types in top-level module
 --> src/lib.rs:1:1
  |
1 | const fn f() -> impl Eq { g() }
  | ^^^^^^^^^^^^^^^^^^^^^^^

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

Meta

rustc --version --verbose:

rustc 1.62.1 (e092d0b6b 2022-07-16)
binary: rustc
commit-hash: e092d0b6b43f2de967af0887873151bb1c0b18d3
commit-date: 2022-07-16
host: x86_64-unknown-linux-gnu
release: 1.62.1
LLVM version: 14.0.5
rustc 1.64.0-nightly (6dbae3ad1 2022-07-25)
binary: rustc
commit-hash: 6dbae3ad19309bb541d9e76638e6aa4b5449f29a
commit-date: 2022-07-25
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6
@yvt yvt added the C-bug Category: This is a bug. label Jul 27, 2022
@yvt yvt changed the title False "cycle detected" error in const fn with RPIT False-positive "cycle detected" in const fn with RPIT Jul 28, 2022
yvt added a commit to r3-os/r3 that referenced this issue Aug 9, 2022
yvt added a commit to r3-os/r3 that referenced this issue Aug 9, 2022
…cted" errors

Work-around for [rust-lang/rust#99793][1]. For some reason this one
didn't cause an error in nightly-2022-06-26.

[1]: rust-lang/rust#99793
@y86-dev
Copy link
Contributor

y86-dev commented Sep 14, 2022

I also encountered this issue on

rustc 1.65.0-nightly (17cbdfd07 2022-09-13)
binary: rustc
commit-hash: 17cbdfd07178349d0a3cecb8e7dde8f915666ced
commit-date: 2022-09-13
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 15.0.0

The following code also works (seems like a general workaround):

const fn f() -> impl Eq { let g = g(); g }
const fn g() {}

@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. I-cycle Issue: A query cycle occurred while none was expected and removed needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. labels Jan 25, 2024
fmease added a commit to fmease/rust that referenced this issue Jun 4, 2024
…hecks, r=lcnr

Do not try to reveal hidden types when trying to prove Freeze in the defining scope

fixes rust-lang#99793

this avoids the cycle error by just causing a selection error, which is not fatal. We pessimistically assume that freeze does not hold, which is always a safe assumption.
@bors bors closed this as completed in 6bf5fd5 Jul 25, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2024
Rollup merge of rust-lang#122192 - oli-obk:type_of_opaque_for_const_checks, r=lcnr

Do not try to reveal hidden types when trying to prove auto-traits in the defining scope

fixes rust-lang#99793

this avoids the cycle error by just causing a selection error, which is not fatal. We pessimistically assume that freeze does not hold, which is always a safe assumption.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. 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
4 participants