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: overflow computing relative variant idx in rustc_const_eval/src/interpret/discriminant.rs #126267

Closed
cushionbadak opened this issue Jun 11, 2024 · 1 comment · Fixed by #133681
Labels
C-bug Category: This is a bug. F-transmutability `#![feature(transmutability)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cushionbadak
Copy link

Code

(hand-reduced)

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

pub enum ApiError {}
pub struct TokioError {
    b: bool,
}
pub enum Error {
    Api { source: ApiError },
    Ethereum,
    Tokio { source: TokioError },
}

mod assert {
    use std::mem::BikeshedIntrinsicFrom;

    pub fn is_transmutable<Src, Dst>()
    where
        Dst: BikeshedIntrinsicFrom<Src>, // safety is NOT assumed
    {
    }
}

fn test() {
    struct Src;
    type Dst = Error;
    assert::is_transmutable::<Src, Dst>();
}
(original)

//@ compile-flags: -C opt-level=0
#![crate_type = "lib"]

pub enum ApiError {}
#[allow(dead_code)]
pub struct TokioError {
    b: bool,
}
pub enum Error {
    Api { source: ApiError },
    Ethereum,
    Tokio { source: TokioError },
}
struct Api;
impl IntoError<Error> for Api {
    type Source = ApiError;
    // CHECK-LABEL: @into_error
    // CHECK: llvm.trap()
    // Also check the next two instructions to make sure we do not match against `trap`
    // elsewhere in the code.
    // CHECK-NEXT: load
    // CHECK-NEXT: ret
    #[no_mangle]
    fn into_error(self, error: Self::) -> Error {
        Error::Api { source: error }
    }
}

pub trait IntoError<E> {
    /// The underlying error
    type Source;

    /// Combine the information to produce the error
    fn into_error(self, source: Self::Source) -> E;
}


//@ check-pass

//! The presence of safety invariants in the source type does not affect
//! transmutability.

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

mod assert {
    use std::mem::BikeshedIntrinsicFrom;

    pub fn is_transmutable<Src, Dst>()
    where
        Dst: BikeshedIntrinsicFrom<Src> // safety is NOT assumed
    {}
}

fn test() {
    #[repr(C)]
    struct Src;
    type Dst = Error;
    assert::is_transmutable::<Src, Dst>();
}

Meta

rustc --version --verbose:

rustc 1.81.0-nightly (b5b13568f 2024-06-10)
binary: rustc
commit-hash: b5b13568fb5da4ac988bde370008d6134d3dfe6c
commit-date: 2024-06-10
host: x86_64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7

Error output

Command: rustc

<output>
Backtrace

thread 'rustc' panicked at compiler/rustc_const_eval/src/interpret/discriminant.rs:288:22:
overflow computing relative variant idx
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::option::expect_failed
   3: rustc_const_eval::const_eval::tag_for_variant_provider
      [... omitted 2 frames ...]
   4: <rustc_transmute::layout::tree::Tree<rustc_transmute::layout::rustc::Def, rustc_transmute::layout::rustc::Ref>>::from_enum
   5: <rustc_transmute::rustc::TransmuteTypeEnv>::is_transmutable
   6: <rustc_trait_selection::traits::select::SelectionContext>::confirm_candidate
   7: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_candidate::{closure#0}::{closure#0}
   8: <rustc_infer::infer::InferCtxt>::probe::<core::result::Result<rustc_middle::traits::select::EvaluationResult, rustc_middle::traits::select::OverflowError>, <rustc_trait_selection::traits::select::SelectionContext>::evaluation_probe<<rustc_trait_selection::traits::select::SelectionContext>::evaluate_candidate::{closure#0}::{closure#0}>::{closure#0}>
   9: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_trait_predicate_recursively::{closure#0}::{closure#1}
  10: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_trait_predicate_recursively::{closure#0}
  11: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_predicate_recursively::{closure#0}::{closure#0}
  12: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_root_obligation::{closure#0}
  13: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_root_obligation
  14: rustc_traits::evaluate_obligation::evaluate_obligation
      [... omitted 2 frames ...]
  15: <rustc_infer::infer::InferCtxt as rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt>::evaluate_obligation
  16: <rustc_infer::infer::InferCtxt as rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt>::evaluate_obligation_no_overflow
  17: <rustc_trait_selection::traits::fulfill::FulfillProcessor>::process_trait_obligation
  18: <rustc_trait_selection::traits::fulfill::FulfillProcessor as rustc_data_structures::obligation_forest::ObligationProcessor>::process_obligation
  19: <rustc_data_structures::obligation_forest::ObligationForest<rustc_trait_selection::traits::fulfill::PendingPredicateObligation>>::process_obligations::<rustc_trait_selection::traits::fulfill::FulfillProcessor>
  20: <rustc_trait_selection::traits::fulfill::FulfillmentContext<rustc_trait_selection::traits::FulfillmentError> as rustc_infer::traits::engine::TraitEngine<rustc_trait_selection::traits::FulfillmentError>>::select_where_possible
  21: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_argument_types
  22: <rustc_hir_typeck::fn_ctxt::FnCtxt>::confirm_builtin_call
  23: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_kind
  24: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  25: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_block_with_expected
  26: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  27: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_return_expr
  28: rustc_hir_typeck::check::check_fn
  29: rustc_hir_typeck::typeck
      [... omitted 1 frame ...]
  30: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_hir_analysis::check_crate::{closure#4}>::{closure#0}
  31: rustc_hir_analysis::check_crate
  32: rustc_interface::passes::run_required_analyses
  33: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  34: <rustc_interface::queries::QueryResult<&rustc_middle::ty::context::GlobalCtxt>>::enter::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}::{closure#1}::{closure#3}>
  35: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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: please attach the file at `/Volumes/T7/workspace/240607_100chaos_tree_combine_typ/icefiles/rustc-ice-2024-06-11T10_43_12-38775.txt` to your bug report

query stack during panic:
#0 [tag_for_variant] computing variant tag for enum
#1 [evaluate_obligation] evaluating trait selection obligation `Error: core::mem::transmutability::BikeshedIntrinsicFrom<test::Src, core::mem::transmutability::Assume { alignment: false, lifetimes: false, safety: false, validity: false }>`
#2 [typeck] type-checking `test`
#3 [analysis] running analysis passes on this crate
end of query stack

Notes

@rustbot label +F-transmutability

@cushionbadak cushionbadak 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 Jun 11, 2024
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. F-transmutability `#![feature(transmutability)]` labels Jun 11, 2024
@cushionbadak
Copy link
Author

Regression in rust-lang-ci@d48eec5
The PR introducing the regression in this rollup is #123367: Safe Transmute: Compute transmutability from `rustc_target:…

searched nightlies: from nightly-2023-01-01 to nightly-2024-05-30
regressed nightly: nightly-2024-04-09
searched commit range: 9d5cdf7...ab5bda1
regressed commit: ab5bda1

bisected with cargo-bisect-rustc v0.6.8

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --start=2023-01-01 --end=2024-05-30 --regress=ice --script=rustc --preserve -- 126267.rs

@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Jun 17, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 23, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 3, 2024
improve TagEncoding::Niche docs, sanity check, and UB checks

Turns out the `niche_variants` range can actually contain the `untagged_variant`. We should report this as UB in Miri, so this PR implements that.

Also rename `partially_check_layout` to `layout_sanity_check` for better consistency with how similar functions are called in other parts of the compiler.

Turns out my adjustments to the transmutation logic also fix rust-lang#126267.
@bors bors closed this as completed in 6e87eb5 Dec 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 4, 2024
Rollup merge of rust-lang#133681 - RalfJung:niches, r=wesleywiser

improve TagEncoding::Niche docs, sanity check, and UB checks

Turns out the `niche_variants` range can actually contain the `untagged_variant`. We should report this as UB in Miri, so this PR implements that.

Also rename `partially_check_layout` to `layout_sanity_check` for better consistency with how similar functions are called in other parts of the compiler.

Turns out my adjustments to the transmutation logic also fix rust-lang#126267.
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Dec 4, 2024
improve TagEncoding::Niche docs, sanity check, and UB checks

Turns out the `niche_variants` range can actually contain the `untagged_variant`. We should report this as UB in Miri, so this PR implements that.

Also rename `partially_check_layout` to `layout_sanity_check` for better consistency with how similar functions are called in other parts of the compiler.

Turns out my adjustments to the transmutation logic also fix rust-lang/rust#126267.
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) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. 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.

4 participants