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

broken MIR when transmuting integer to &'static () in const generic #72293

Closed
iwikal opened this issue May 17, 2020 · 6 comments · Fixed by #86205
Closed

broken MIR when transmuting integer to &'static () in const generic #72293

iwikal opened this issue May 17, 2020 · 6 comments · Fixed by #86205
Labels
A-const-generics Area: const generics (parameters and arguments) A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@iwikal
Copy link

iwikal commented May 17, 2020

Code

#![feature(const_generics)]

struct Const<const P: &'static ()>;

fn main() {
    const A: &'static () = unsafe {
        std::mem::transmute(10 as *const ())
    };

    let _ = Const::<{A}>;
}

Meta

rustc --version --verbose:

rustc 1.45.0-nightly (a74d1862d 2020-05-14)
binary: rustc
commit-hash: a74d1862d4d87a56244958416fd05976c58ca1a8
commit-date: 2020-05-14
host: x86_64-unknown-linux-gnu
release: 1.45.0-nightly
LLVM version: 9.0

Error output

warning: the feature `const_generics` is incomplete and may cause the compiler to crash
 --> src/main.rs:1:12
  |
1 | #![feature(const_generics, const_transmute)]
  |            ^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

error: internal compiler error: broken MIR in DefId(0:6 ~ main[317d]::main[0]) (CanonicalUserTypeAnnotation { user_ty: Canonical { max_universe: U0, variables: [], value: TypeOf(DefId(0:4 ~ main[317d]::Const[0]::{{constructor}}[0]), UserSubsts { substs: [Const { ty: &'static (), val: Unevaluated(DefId(0:8 ~ main[317d]::main[0]::{{constant}}[0]), [], None) }], user_self_ty: None }) }, span: src/main.rs:10:13: 10:25, inferred_ty: Const<{transmute(0x000000000000000a): &()}> }): bad user type AscribeUserType(Const<{transmute(0x000000000000000a): &()}>, DefId(0:4 ~ main[317d]::Const[0]::{{constructor}}[0]) UserSubsts { substs: [Const { ty: &'static (), val: Unevaluated(DefId(0:8 ~ main[317d]::main[0]::{{constant}}[0]), [], None) }], user_self_ty: None }): NoSolution

error: internal compiler error: broken MIR in DefId(0:6 ~ main[317d]::main[0]) (_1 = Const::<{transmute(0x000000000000000a): &()}>): bad assignment (Const<{transmute(0x000000000000000a): &()}> = Const<{transmute(0x000000000000000a): &()}>): NoSolution
  --> src/main.rs:10:13
   |
10 |     let _ = Const::<{A}>;
   |             ^^^^^^^^^^^^

error: internal compiler error: broken MIR in DefId(0:6 ~ main[317d]::main[0]) (_1 = Const::<{transmute(0x000000000000000a): &()}>): bad user type on rvalue (CanonicalUserTypeAnnotation { user_ty: Canonical { max_universe: U0, variables: [], value: TypeOf(DefId(0:4 ~ main[317d]::Const[0]::{{constructor}}[0]), UserSubsts { substs: [Const { ty: &'static (), val: Unevaluated(DefId(0:8 ~ main[317d]::main[0]::{{constant}}[0]), [], None) }], user_self_ty: None }) }, span: src/main.rs:10:13: 10:25, inferred_ty: Const<{transmute(0x000000000000000a): &()}> } = Const<{transmute(0x000000000000000a): &()}>): NoSolution
  --> src/main.rs:10:13
   |
10 |     let _ = Const::<{A}>;
   |             ^^^^^^^^^^^^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:366:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.45.0-nightly (a74d1862d 2020-05-14) running on x86_64-unknown-linux-gnu

Backtrace

stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1069
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1537
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:218
  10: rustc_driver::report_ice
  11: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:481
  12: std::panicking::begin_panic
  13: <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop
  14: core::ptr::drop_in_place
  15: <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop
  16: core::ptr::drop_in_place
  17: rustc_interface::interface::run_compiler_in_existing_thread_pool
  18: scoped_tls::ScopedKey<T>::set
  19: rustc_ast::attr::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@iwikal iwikal added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 17, 2020
@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. labels May 17, 2020
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label May 18, 2020
@Alexendoo
Copy link
Member

Still ICEs but needs const_transmute removed from features after #72920

#![feature(const_generics)]

struct Const<const P: &'static ()>;

fn main() {
    const A: &'static () = unsafe {
        std::mem::transmute(10 as *const ())
    };

    let _ = Const::<{A}>;
}

Alexendoo added a commit to rust-lang/glacier that referenced this issue Jul 13, 2020
@varkor varkor added fixed-by-const-generics Enabling feature `const_generics` fixes the issue. const-generics-fixed-by-min_const_generics E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed const-generics-needs-test-for-min_const_generics fixed-by-const-generics Enabling feature `const_generics` fixes the issue. labels Oct 1, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Oct 5, 2020
…lcnr,estebank

Fix missing diagnostic span for `impl Trait` with const generics, and add various tests for `min_const_generics` and `const_generics`

Closes rust-lang#61410.

Adds `min_const_generics` tests for:
- rust-lang#73727
- rust-lang#72293
- rust-lang#67375
- rust-lang#75153
- rust-lang#71922
- rust-lang#69913
- rust-lang#67945
- rust-lang#69239

Adds `const_generics` tests for:
- rust-lang#67375
- rust-lang#75153
- rust-lang#71922
- rust-lang#69913
- rust-lang#67945
- rust-lang#69239

(I only added separate `min_const_generics` and `const_generics` tests if they were handled differently by the two features.)

We need to figure out how to deduplicate when `const_generics` is stabilised, but we can discuss that later. For now, we should be checking neither feature breaks, so require regression tests for both. I've given them identical names when I've added both, which should make it easier to spot them later.

r? @lcnr
@JohnTitor JohnTitor removed the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Oct 9, 2020
@Alexendoo
Copy link
Member

Alexendoo commented Mar 18, 2021

No longer ICEs, bisect shows since #82936

Could do with a test for full const_generics I believe

@Alexendoo Alexendoo added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Mar 18, 2021
@RalfJung
Copy link
Member

@oli-obk is that an expected side-effect of your PR?

@oli-obk
Copy link
Contributor

oli-obk commented Mar 18, 2021

Not expected before that PR, but the ICE in this issue "obviously" (in hindsight) occurred because before 019dba0 the problem was that types were compared exactly (including lifetimes), so a constant of type &'static () is not the same as a constant of type &'empty (), even though all lifetimes in constants' final values are 'static by definition.

@lcnr
Copy link
Contributor

lcnr commented Mar 18, 2021

even though all lifetimes in constants' final values are 'static by definition.

You can have constants with non-static lifetimes, even if their target actually does live forever.

use std::marker::PhantomData;

struct Foo<'a>(&'a (), PhantomData<fn(&'a ()) -> &'a ()>);

fn covariant<'a>(v: Foo<'static>) -> Foo<'a> {
    v // ERROR mismatched types
}

fn covariant_xd<'a>(_: Foo<'static>) -> Foo<'a> {
    Foo::<'a>::ASSOC
}

impl<'a> Foo<'a> {
    const ASSOC: Foo<'a> = Foo(&(), PhantomData);
}

(https://twitter.com/lcnr7/status/1360218268703817733)

Or just

struct Foo<'a>(&'a ());
impl<'a> Foo<'a> {
    const N: &'a () = &();
}

@oli-obk
Copy link
Contributor

oli-obk commented Mar 19, 2021

Right. Even so, this doesn't affect Relate, as the constants are still equal to each other irrespective of actual lifetimes. The only way I can see this breaking is if we could specialize on lifetimes.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 11, 2021
…i-obk

Run full const-generics test for issue-72293

Closes rust-lang#72293
r? `@oli-obk`
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 11, 2021
…i-obk

Run full const-generics test for issue-72293

Closes rust-lang#72293
r? ``@oli-obk``
@bors bors closed this as completed in 2b3884f Jun 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. 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.

9 participants