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

ICE: transmute: called Result::unwrap() on an Err value: ReferencesError(ErrorGuaranteed(())) #129327

Closed
matthiaskrgr opened this issue Aug 20, 2024 · 11 comments · Fixed by #129364
Labels
C-bug Category: This is a bug. F-transmutability `#![feature(transmutability)]` 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.

Comments

@matthiaskrgr
Copy link
Member

auto-reduced (treereduce-rust):

mod assert {
    use std::mem::{Assume, BikeshedIntrinsicFrom};

    pub fn is_transmutable<Src, Dst>()
    where
        Dst: BikeshedIntrinsicFrom<Src>,
    {
    }
}

fn test() {
    #[repr(C)]
    struct Src {
        a: &'a str,
        g: G,
    }

    assert::is_transmutable::<&mut Src, &mut Dst>();
}

original:

//@ check-pass

//! When safety is assumed, a transmutation over exclusive references should be
//! accepted if the source type potentially carries safety invariants.

#![crate_type = "lib"]
#![feature(transmutability)]
#![allow(dead_code)]

mod assert {
    use std::mem::{Assume, BikeshedIntrinsicFrom};

    pub fn is_transmutable<Src, Dst>()
    where
        Dst: BikeshedIntrinsicFrom<Src, { Assume::SAFETY }>
    {}
}

fn test() {
    #[repr(C)]
    struct Src {
    a: &'a str,
    g: G,
}
    type Dst = u8;
    assert::is_transmutable::<&mut Src, &mut Dst>();
}

Version information

rustc 1.82.0-nightly (4d5b3b196 2024-08-20)
binary: rustc
commit-hash: 4d5b3b196284aded6ae99d12bcf149ffdc8ef379
commit-date: 2024-08-20
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc

Program output

error[E0261]: use of undeclared lifetime name `'a`
  --> /tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs:14:13
   |
13 |     struct Src {
   |               - help: consider introducing lifetime `'a` here: `<'a>`
14 |         a: &'a str,
   |             ^^ undeclared lifetime

error[E0412]: cannot find type `G` in this scope
  --> /tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs:15:12
   |
15 |         g: G,
   |            ^ not found in this scope
   |
help: you might be missing a type parameter
   |
13 |     struct Src<G> {
   |               +++

error[E0412]: cannot find type `Dst` in this scope
  --> /tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs:18:46
   |
18 |     assert::is_transmutable::<&mut Src, &mut Dst>();
   |                                              ^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
11 | fn test<Dst>() {
   |        +++++

warning: unused import: `Assume`
 --> /tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs:2:20
  |
2 |     use std::mem::{Assume, BikeshedIntrinsicFrom};
  |                    ^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0601]: `main` function not found in crate `mvce`
  --> /tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs:19:2
   |
19 | }
   |  ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs`

error[E0658]: use of unstable library feature 'transmutability'
 --> /tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs:2:20
  |
2 |     use std::mem::{Assume, BikeshedIntrinsicFrom};
  |                    ^^^^^^
  |
  = note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
  = help: add `#![feature(transmutability)]` to the crate attributes to enable
  = note: this compiler was built on 2024-08-20; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'transmutability'
 --> /tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs:2:28
  |
2 |     use std::mem::{Assume, BikeshedIntrinsicFrom};
  |                            ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
  = help: add `#![feature(transmutability)]` to the crate attributes to enable
  = note: this compiler was built on 2024-08-20; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'transmutability'
 --> /tmp/icemaker_global_tempdir.11e6Po1ssj9b/rustc_testrunner_tmpdir_reporting.TfSZCUR9yS0l/mvce.rs:6:14
  |
6 |         Dst: BikeshedIntrinsicFrom<Src>,
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
  = help: add `#![feature(transmutability)]` to the crate attributes to enable
  = note: this compiler was built on 2024-08-20; consider upgrading it if it is out of date

thread 'rustc' panicked at compiler/rustc_transmute/src/layout/tree.rs:488:49:
called `Result::unwrap()` on an `Err` value: ReferencesError(ErrorGuaranteed(()))
stack backtrace:
   0:     0x75f78cfbc8bd - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hf9983800a10aa2c6
   1:     0x75f78d805457 - core::fmt::write::h878015f659953925
   2:     0x75f78e799351 - std::io::Write::write_fmt::h3c30dbfe90a8ec80
   3:     0x75f78cfbef9b - std::panicking::default_hook::{{closure}}::h4de9d0974edce2a2
   4:     0x75f78cfbec0e - std::panicking::default_hook::h37c112ea75e0ed52
   5:     0x75f78c140db9 - std[ad0fe81d36b825dd]::panicking::update_hook::<alloc[80838699efa87670]::boxed::Box<rustc_driver_impl[e626c935ad353586]::install_ice_hook::{closure#0}>>::{closure#0}
   6:     0x75f78cfbf8b7 - std::panicking::rust_panic_with_hook::he404808eae2fa6a3
   7:     0x75f78cfbf577 - std::panicking::begin_panic_handler::{{closure}}::h2e0f2172b8396a90
   8:     0x75f78cfbcd79 - std::sys::backtrace::__rust_end_short_backtrace::h8e04dc1a1de87bcf
   9:     0x75f78cfbf244 - rust_begin_unwind
  10:     0x75f789e9bd73 - core::panicking::panic_fmt::h7c850fd3e66ca6f3
  11:     0x75f789f5aad6 - core::result::unwrap_failed::h8a305cb559451044
  12:     0x75f78cefad67 - <rustc_transmute[a3340d9efd954e09]::layout::tree::Tree<rustc_transmute[a3340d9efd954e09]::layout::rustc::Def, rustc_transmute[a3340d9efd954e09]::layout::rustc::Ref>>::from_ty
  13:     0x75f78ceff8b7 - <rustc_transmute[a3340d9efd954e09]::rustc::TransmuteTypeEnv>::is_transmutable
  14:     0x75f78e58180e - <rustc_trait_selection[c090ce818561ca7c]::traits::select::SelectionContext>::poly_select::{closure#0}
  15:     0x75f789e60970 - <rustc_trait_selection[c090ce818561ca7c]::traits::fulfill::FulfillProcessor as rustc_data_structures[c066231c7fa32b84]::obligation_forest::ObligationProcessor>::process_obligation
  16:     0x75f78d8087f7 - <rustc_data_structures[c066231c7fa32b84]::obligation_forest::ObligationForest<rustc_trait_selection[c090ce818561ca7c]::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection[c090ce818561ca7c]::traits::fulfill::FulfillProcessor>
  17:     0x75f78a4aa3f3 - <rustc_hir_typeck[35f980454f904813]::fn_ctxt::FnCtxt>::confirm_builtin_call
  18:     0x75f78e2d8e65 - <rustc_hir_typeck[35f980454f904813]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  19:     0x75f78e2d21fd - <rustc_hir_typeck[35f980454f904813]::fn_ctxt::FnCtxt>::check_block_with_expected
  20:     0x75f78e2d96c8 - <rustc_hir_typeck[35f980454f904813]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  21:     0x75f78d8c01b7 - rustc_hir_typeck[35f980454f904813]::check::check_fn
  22:     0x75f78deda8f2 - rustc_hir_typeck[35f980454f904813]::typeck
  23:     0x75f78deda335 - rustc_query_impl[c25b9b67542a1b7e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[c25b9b67542a1b7e]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[b9d4f7314f7035ba]::query::erase::Erased<[u8; 8usize]>>
  24:     0x75f78db84e7d - rustc_query_system[33396c7944b94add]::query::plumbing::try_execute_query::<rustc_query_impl[c25b9b67542a1b7e]::DynamicConfig<rustc_query_system[33396c7944b94add]::query::caches::VecCache<rustc_span[bd867921c9292620]::def_id::LocalDefId, rustc_middle[b9d4f7314f7035ba]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[c25b9b67542a1b7e]::plumbing::QueryCtxt, false>
  25:     0x75f78db84095 - rustc_query_impl[c25b9b67542a1b7e]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  26:     0x75f78db83d1b - <rustc_middle[b9d4f7314f7035ba]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[9db2b2b48bb4004b]::check_crate::{closure#4}>::{closure#0}
  27:     0x75f78db81a64 - rustc_hir_analysis[9db2b2b48bb4004b]::check_crate
  28:     0x75f78db7a27f - rustc_interface[91cc5f479e6877b9]::passes::run_required_analyses
  29:     0x75f78e351bde - rustc_interface[91cc5f479e6877b9]::passes::analysis
  30:     0x75f78e351bb1 - rustc_query_impl[c25b9b67542a1b7e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[c25b9b67542a1b7e]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[b9d4f7314f7035ba]::query::erase::Erased<[u8; 1usize]>>
  31:     0x75f78e76b46e - rustc_query_system[33396c7944b94add]::query::plumbing::try_execute_query::<rustc_query_impl[c25b9b67542a1b7e]::DynamicConfig<rustc_query_system[33396c7944b94add]::query::caches::SingleCache<rustc_middle[b9d4f7314f7035ba]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[c25b9b67542a1b7e]::plumbing::QueryCtxt, false>
  32:     0x75f78e76b1cf - rustc_query_impl[c25b9b67542a1b7e]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  33:     0x75f78e5e57aa - rustc_interface[91cc5f479e6877b9]::interface::run_compiler::<core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>, rustc_driver_impl[e626c935ad353586]::run_compiler::{closure#0}>::{closure#1}
  34:     0x75f78e672a90 - std[ad0fe81d36b825dd]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[91cc5f479e6877b9]::util::run_in_thread_with_globals<rustc_interface[91cc5f479e6877b9]::util::run_in_thread_pool_with_globals<rustc_interface[91cc5f479e6877b9]::interface::run_compiler<core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>, rustc_driver_impl[e626c935ad353586]::run_compiler::{closure#0}>::{closure#1}, core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>>::{closure#0}, core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>>
  35:     0x75f78e6730fa - <<std[ad0fe81d36b825dd]::thread::Builder>::spawn_unchecked_<rustc_interface[91cc5f479e6877b9]::util::run_in_thread_with_globals<rustc_interface[91cc5f479e6877b9]::util::run_in_thread_pool_with_globals<rustc_interface[91cc5f479e6877b9]::interface::run_compiler<core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>, rustc_driver_impl[e626c935ad353586]::run_compiler::{closure#0}>::{closure#1}, core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>>::{closure#0}, core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[4a74943f2cbe9718]::result::Result<(), rustc_span[bd867921c9292620]::ErrorGuaranteed>>::{closure#1} as core[4a74943f2cbe9718]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  36:     0x75f78e67346b - std::sys::pal::unix::thread::Thread::new::thread_start::hf4aaee2b23b0d10a
  37:     0x75f788aa339d - <unknown>
  38:     0x75f788b2849c - <unknown>
  39:                0x0 - <unknown>

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: rustc 1.82.0-nightly (4d5b3b196 2024-08-20) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [typeck] type-checking `test`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 7 previous errors; 1 warning emitted

Some errors have detailed explanations: E0261, E0412, E0601, E0658.
For more information about an error, try `rustc --explain E0261`.

@rustbot label +F-transmutability

@matthiaskrgr matthiaskrgr added 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. C-bug Category: This is a bug. labels Aug 20, 2024
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. F-transmutability `#![feature(transmutability)]` labels Aug 20, 2024
@matthiaskrgr
Copy link
Member Author

#129217 cc @jswrenn ^^

@matthiaskrgr
Copy link
Member Author

reduced a bit

use std::mem::BikeshedIntrinsicFrom;

pub fn is_transmutable<Src, Dst>()
where
    Dst: BikeshedIntrinsicFrom<Src>,
{
}

struct Src {
    g: G,
}

fn main() {
    is_transmutable::<&mut Src, &mut Dst>();
}

@jswrenn
Copy link
Member

jswrenn commented Aug 20, 2024

Hm, the problem seems to stem from G not being defined. I think there's been a few instances where we ICE on malformed types/parameters — hopefully there's some simple thing we can do at a top-level to catch these cases and fail gracefully.

@compiler-errors
Copy link
Member

hopefully there's some simple thing we can do at a top-level

@jswrenn: I don't think so, since you're only encountering these error types on field projection. I recommend reworking the layout computation code to stop unwrapping and handle the layout errors gracefully.

@jswrenn
Copy link
Member

jswrenn commented Aug 20, 2024

The downside of doing so is that any misconceptions I have about layout_of will no longer be helpfully reported to me as ICEs.

Is there a way to query whether a Ty is well-formed?

@compiler-errors
Copy link
Member

No, and also I think the particular unwrap in this case is very weird. I have no idea why it's unwrapping when it's only being called in functions that also only return Results.

@jswrenn
Copy link
Member

jswrenn commented Aug 20, 2024

Because it's important to get this right. For this scenario, the ideal thing to do might be to accept the transmute, because doing so will avoid emitting a distracting 'trait not implemented' error. But, in scenarios that don't involved malformed types where layout_of fails, this approach might mask a bug.

My ideal solution would be to detect at a top-level whether the involved types are well-formed, unconditionally accept the transmute if they are not, and retain the unwraps in tree.rs. Since that's not possible, the conservative fix will be to bubble up NotYetSupported errors. Emitting diagnostic might be distracting, but at least it won't ever be unsound.

@compiler-errors
Copy link
Member

compiler-errors commented Aug 20, 2024

I don't believe the correct fix is to bubble up NotYetSupported errors. There literally is already a conversion from LayoutError to rustc_transmute::tree::rustc::Error: https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_transmute/layout/tree.rs.html#195-204

That error struct already has variants to represent the layout errors this API should be handling. I don't see why you'd be turning them into NotYetSupported errors instead, and I'm skeptical of the claim that this will mask any bugs. I would consider any other code in rustc that's unwrapping a layout call (except for in post-monomorphization codegen) to be a bug, on the contrary.

@jswrenn
Copy link
Member

jswrenn commented Aug 20, 2024

Yeah, that sounds right — forgot we had that impl. For some reason, NotYetSupported is how we currently handle layout errors: https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_transmute/maybe_transmutable/mod.rs.html#47-51

There's probably a UI test or issue somewhere that prompted that, but I agree that it smells.

@compiler-errors
Copy link
Member

I recommend you change that one to handle layout errors gracefully, too.

@jswrenn
Copy link
Member

jswrenn commented Aug 20, 2024

Of course; will do!

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 21, 2024
jswrenn added a commit to jswrenn/rust that referenced this issue Aug 21, 2024
Changes `.unwrap()`s to `?` to avoid ICEs. Adds ui tests.

Fixes rust-lang#129327
jswrenn added a commit to jswrenn/rust that referenced this issue Aug 21, 2024
Changes `.unwrap()`s to `?` to avoid ICEs. Adds ui tests.

Fixes rust-lang#129327
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 21, 2024
…ompiler-errors

safe transmute: gracefully bubble-up layout errors

Changes `.unwrap()`s to `?` to avoid ICEs. Adds ui tests.

Fixes rust-lang#129327

Tracking Issue: rust-lang#99571

r​? `@compiler-errors`
@bors bors closed this as completed in e2328eb Aug 21, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 21, 2024
Rollup merge of rust-lang#129364 - jswrenn:transmute-layout-errs, r=compiler-errors

safe transmute: gracefully bubble-up layout errors

Changes `.unwrap()`s to `?` to avoid ICEs. Adds ui tests.

Fixes rust-lang#129327

Tracking Issue: rust-lang#99571

r​? `@compiler-errors`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-transmutability `#![feature(transmutability)]` 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants