Skip to content

Size mismatch when const transmuting #61774

Closed
@tesuji

Description

@tesuji

Playground

Maybe this is a duplicate to #61455

I tried this code:

#![feature(const_transmute)]

use std::mem::transmute;

fn dump() -> i32 {
    42
}

fn main() {
    let arr: [i8; unsafe { transmute(dump) }];
}
stderr
thread 'rustc' panicked at 'Size mismatch when transmuting!
src: OpTy {
    op: Immediate(
        Scalar(
            <ZST>,
        ),
    ),
    layout: TyLayout {
        ty: fn() -> i32 {dump},
        details: LayoutDetails {
            variants: Single {
                index: 0,
            },
            fields: Arbitrary {
                offsets: [],
                memory_index: [],
            },
            abi: Aggregate {
                sized: true,
            },
            align: AbiAndPrefAlign {
                abi: Align {
                    pow2: 0,
                },
                pref: Align {
                    pow2: 3,
                },
            },
            size: Size {
                raw: 0,
            },
        },
    },
}
dest: PlaceTy {
    place: Ptr(
        MemPlace {
            ptr: AllocId(0).0x0,
            align: Align {
                pow2: 3,
            },
            meta: None,
        },
    ),
    layout: TyLayout {
        ty: usize,
        details: LayoutDetails {
            variants: Single {
                index: 0,
            },
            fields: Union(
                0,
            ),
            abi: Scalar(
                Scalar {
                    value: Int(
                        I64,
                        false,
                    ),
                    valid_range: 0..=18446744073709551615,
                },
            ),
            align: AbiAndPrefAlign {
                abi: Align {
                    pow2: 3,
                },
                pref: Align {
                    pow2: 3,
                },
            },
            size: Size {
                raw: 8,
            },
        },
    },
}', src/librustc_mir/interpret/place.rs:874:9
backtrace
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:212
   6: rustc::util::common::panic_hook
   7: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:479
   8: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:382
   9: std::panicking::begin_panic_fmt
             at src/libstd/panicking.rs:337
  10: rustc_mir::interpret::place::<impl rustc_mir::interpret::eval_context::InterpretCx<M>>::copy_op_transmute
  11: rustc_mir::interpret::intrinsics::<impl rustc_mir::interpret::eval_context::InterpretCx<M>>::emulate_intrinsic
  12: <rustc_mir::const_eval::CompileTimeInterpreter as rustc_mir::interpret::machine::Machine>::call_intrinsic
  13: rustc_mir::interpret::terminator::<impl rustc_mir::interpret::eval_context::InterpretCx<M>>::eval_fn_call
  14: rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpretCx<M>>::run
  15: rustc_mir::const_eval::eval_body_using_ecx
  16: rustc_mir::const_eval::const_eval_raw_provider
  17: rustc::ty::query::__query_compute::const_eval_raw
  18: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval_raw>::compute
  19: rustc::dep_graph::graph::DepGraph::with_task_impl
  20: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  21: rustc_mir::const_eval::const_eval_provider
  22: rustc::ty::query::__query_compute::const_eval
  23: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval>::compute
  24: rustc::dep_graph::graph::DepGraph::with_task_impl
  25: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  26: <rustc::traits::project::AssocTypeNormalizer as rustc::ty::fold::TypeFolder>::fold_const
  27: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable for &rustc::ty::TyS>::super_fold_with
  28: <rustc::traits::project::AssocTypeNormalizer as rustc::ty::fold::TypeFolder>::fold_ty
  29: rustc::traits::project::normalize
  30: <rustc_typeck::check::FnCtxt as rustc_typeck::astconv::AstConv>::normalize_ty
  31: <dyn rustc_typeck::astconv::AstConv>::ast_ty_to_ty
  32: <rustc_typeck::check::GatherLocalsVisitor as rustc::hir::intravisit::Visitor>::visit_local
  33: rustc::hir::intravisit::walk_expr
  34: rustc_typeck::check::check_fn
  35: rustc::ty::context::GlobalCtxt::enter_local
  36: rustc_typeck::check::typeck_tables_of
  37: rustc::ty::query::__query_compute::typeck_tables_of
  38: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::typeck_tables_of>::compute
  39: rustc::dep_graph::graph::DepGraph::with_task_impl
  40: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  41: rustc::ty::<impl rustc::ty::context::TyCtxt>::par_body_owners
  42: rustc_typeck::check::typeck_item_bodies
  43: rustc::ty::query::__query_compute::typeck_item_bodies
  44: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::typeck_item_bodies>::compute
  45: rustc::dep_graph::graph::DepGraph::with_task_impl
  46: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  47: rustc::util::common::time
  48: rustc_typeck::check_crate
  49: rustc_interface::passes::analysis
  50: rustc::ty::query::__query_compute::analysis
  51: rustc::dep_graph::graph::DepGraph::with_task_impl
  52: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  53: rustc::ty::context::tls::enter_global
  54: rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}
  55: rustc_interface::passes::create_global_ctxt::{{closure}}
  56: rustc_interface::interface::run_compiler_in_existing_thread_pool
  57: std::thread::local::LocalKey<T>::with
  58: scoped_tls::ScopedKey<T>::set
  59: syntax::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
query stack during panic:
#0 [const_eval_raw] const-evaluating `main::{{constant}}#0`
#1 [const_eval] const-evaluating + checking `main::{{constant}}#0`
#2 [typeck_tables_of] processing `main`
#3 [typeck_item_bodies] type-checking all item bodies
#4 [analysis] running analysis passes on this crate
end of query stack

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.37.0-nightly (02564de47 2019-06-10) running on x86_64-unknown-linux-gnu

note: compiler flags: -C debuginfo=2 -C incremental --crate-type lib

note: some of the compiler flags provided by cargo are hidden

error: Could not compile `check`.

To learn more, run the command again with --verbose.

Meta

% rustc -Vv
rustc 1.37.0-nightly (02564de47 2019-06-10)
binary: rustc
commit-hash: 02564de47b40e953b5144dfd37450c16a84672f1
commit-date: 2019-06-10
host: x86_64-unknown-linux-gnu
release: 1.37.0-nightly
LLVM version: 8.0

Metadata

Metadata

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.glacierICE tracked in rust-lang/glacier.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions