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

Size mismatch when const transmuting #61774

Closed
tesuji opened this issue Jun 12, 2019 · 6 comments
Closed

Size mismatch when const transmuting #61774

tesuji opened this issue Jun 12, 2019 · 6 comments
Assignees
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority 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

@tesuji
Copy link
Contributor

tesuji commented Jun 12, 2019

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
@rustbot rustbot added A-const-generics Area: const generics (parameters and arguments) 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 12, 2019
@Centril Centril added I-nominated A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) labels Jun 12, 2019
@hellow554
Copy link
Contributor

Also happens without feature gate :/

//#![feature(const_transmute)]

fn main() {
    let _: [_; unsafe { std::mem::transmute(|| {}) }];
}

@varkor
Copy link
Member

varkor commented Jun 13, 2019

We should be getting error[E0512]: cannot transmute between types of different sizes, or dependently-sized types here. I'm not sure why it's not firing yet.

@pnkfelix
Copy link
Member

triage: P-medium. Removing nomination. Assgn self and @varkor

@pnkfelix pnkfelix added P-medium Medium priority and removed I-nominated labels Jun 20, 2019
@eddyb
Copy link
Member

eddyb commented Jun 20, 2019

I think it's not firing because it's still its own pass as opposed to something like the unsafe check on MIR.
So it could run, but only after most const-evaluation has already happened.

@Centril Centril added the requires-nightly This issue requires a nightly compiler in some way. label Aug 6, 2019
@jonas-schievink jonas-schievink added the C-bug Category: This is a bug. label Aug 6, 2019
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Oct 15, 2019
@JohnTitor
Copy link
Member

It might be fixed (via glacier), could someone confirm it?

@Alexendoo
Copy link
Member

Yeah, looks like #67192 fixed it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority 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

No branches or pull requests