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

tait: "cycle detected when computing type" when using transmute. #98993

Closed
Dirbaio opened this issue Jul 6, 2022 · 2 comments
Closed

tait: "cycle detected when computing type" when using transmute. #98993

Dirbaio opened this issue Jul 6, 2022 · 2 comments
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` 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

@Dirbaio
Copy link
Contributor

Dirbaio commented Jul 6, 2022

playground

#![feature(type_alias_impl_trait)]

type Tait = impl Sized;

fn main() {
    let foo1: Tait = 0u32;
    let foo2: Tait = unsafe { core::mem::transmute(foo1) };
}

gives

error[[E0391]](https://doc.rust-lang.org/nightly/error-index.html#E0391): cycle detected when computing type of `Tait::{opaque#0}`
 --> src/main.rs:3:13
  |
3 | type Tait = impl Sized;
  |             ^^^^^^^^^^
  |
note: ...which requires type-checking `main`...
 --> src/main.rs:5:1
  |
5 | fn main() {
  | ^^^^^^^^^
  = note: ...which requires computing layout of `Tait`...
  = note: ...which requires normalizing `Tait`...
  = note: ...which again requires computing type of `Tait::{opaque#0}`, completing the cycle
note: cycle used when checking item types in top-level module
 --> src/main.rs:1:1
  |
1 | / #![feature(type_alias_impl_trait)]
2 | |
3 | | type Tait = impl Sized;
4 | |
... |
7 | |     let foo2: Tait = unsafe { core::mem::transmute(foo1) };
8 | | }
  | |_^

error[[E0512]](https://doc.rust-lang.org/nightly/error-index.html#E0512): cannot transmute between types of different sizes, or dependently-sized types
 --> src/main.rs:7:31
  |
7 |     let foo2: Tait = unsafe { core::mem::transmute(foo1) };
  |                               ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `Tait` does not have a fixed size

I find it quite confusing that the transmute is not allowed. They're the same type after all.

@rustbot label A-impl-trait F-type_alias_impl_trait

@rustbot rustbot added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` labels Jul 6, 2022
@oli-obk oli-obk moved this from Todo to Can do after stabilization in type alias impl trait stabilization Sep 9, 2022
@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. I-cycle Issue: A query cycle occurred while none was expected labels Jan 25, 2024
@RodBurman
Copy link

Using nightly toolchain:

% cargo -v -V
cargo 1.87.0-nightly (2622e844b 2025-02-28)
release: 1.87.0-nightly
commit-hash: 2622e844bc1e2e6123e54e94e4706f7b6195ce3d
commit-date: 2025-02-28
host: aarch64-apple-darwin
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.7.1 (sys:0.4.79+curl-8.12.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: Mac OS 15.3.1 [64-bit]

Gives a simpler error:

% cargo build
   Compiling taiter v0.1.0 (/Users/rod/code/rust/triage/taiter)
error[E0308]: mismatched types
 --> src/main.rs:6:22
  |
3 | type Tait = impl Sized;
  |             ---------- the expected opaque type
...
6 |     let foo1: Tait = 0u32;
  |               ----   ^^^^ expected opaque type, found `u32`
  |               |
  |               expected due to this
  |
  = note: expected opaque type `Tait`
                    found type `u32`
note: this item must have the opaque type in its signature in order to be able to register hidden types
 --> src/main.rs:5:4
  |
5 | fn main() {
  |    ^^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `taiter` (bin "taiter") due to 1 previous error

Note the current stable build 1.85 gives the same error along with the addition of:

error[E0658]: `impl Trait` in type aliases is unstable

which error is to be expected.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 3, 2025

Considering that

#![feature(type_alias_impl_trait)]

type Tait = impl Sized;

fn bar(_: Tait) {
    let foo1: Tait = 0u32;
    let foo2: Tait = unsafe { core::mem::transmute(foo1) };
}

Compiles fine now, I consider this fixed.

I don't think it needs a dedicated test either. The new system is much more robust

@oli-obk oli-obk closed this as completed Mar 3, 2025
@github-project-automation github-project-automation bot moved this from Can do after stabilization to Done in type alias impl trait stabilization Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` 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.
Development

No branches or pull requests

5 participants