Skip to content

ICE when using Self as uninitialized return type in trait method #57796

Description

@paulocsanz
trait FromUnchecked {
    unsafe fn from_unchecked(s: &str) -> Self;
}

impl FromUnchecked for [u8; 30] {
    unsafe fn from_unchecked(s: &str) -> Self {
        let mut array: Self = std::mem::uninitialized();
        let ptr = &mut array as *mut [u8] as *mut u8;
        std::ptr::copy_nonoverlapping(s.as_bytes().as_ptr(), ptr, s.len());
        std::ptr::write_bytes(ptr.add(s.len()), 0, 30 - s.len());
        array
    }
}

fn main() {
    let _: [u8; 30] = unsafe { FromUnchecked::from_unchecked("asdasd") };
}

Playground

To make it compile you can simply replace Self for [u8; 30] in the uninitialized call (Playground)

 Compiling playground v0.0.1 (/playground)
error: internal compiler error: src/librustc/ty/sty.rs:2107: expected constant usize, got Const {
    ty: usize,
    val: Unevaluated(
        DefId(0/1:9 ~ playground[ec94]::{{impl}}[0]::{{constant}}[0]),
        []
    )
}

thread 'main' panicked at 'Box<Any>', src/librustc_errors/lib.rs:600:9
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:59
             at src/libstd/panicking.rs:211
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:227
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:495
   6: std::panicking::begin_panic
   7: rustc_errors::Handler::bug
   8: rustc::util::bug::opt_span_bug_fmt::{{closure}}
   9: rustc::ty::context::tls::with_opt::{{closure}}
  10: rustc::ty::context::tls::with_context_opt
  11: rustc::ty::context::tls::with_opt
  12: rustc::util::bug::opt_span_bug_fmt
  13: rustc::util::bug::bug_fmt
  14: rustc::ty::sty::Const::unwrap_usize::{{closure}}
  15: <rustc_mir::transform::qualify_consts::Qualifier<'a, 'tcx, 'tcx> as rustc::mir::visit::Visitor<'tcx>>::visit_rvalue
  16: <rustc_mir::transform::qualify_consts::Qualifier<'a, 'tcx, 'tcx> as rustc::mir::visit::Visitor<'tcx>>::visit_assign
  17: <rustc_mir::transform::qualify_consts::QualifyAndPromoteConstants as rustc_mir::transform::MirPass>::run_pass
  18: rustc_mir::transform::run_passes::{{closure}}
  19: rustc_mir::transform::run_passes
  20: rustc_mir::transform::mir_validated
  21: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::mir_validated<'tcx>>::compute
  22: rustc::dep_graph::graph::DepGraph::with_task_impl
  23: <rustc::ty::query::plumbing::JobOwner<'a, 'tcx, Q>>::start
  24: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  25: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  26: rustc_mir::borrow_check::mir_borrowck
  27: rustc::ty::query::__query_compute::mir_borrowck
  28: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::mir_borrowck<'tcx>>::compute
  29: rustc::dep_graph::graph::DepGraph::with_task_impl
  30: <rustc::ty::query::plumbing::JobOwner<'a, 'tcx, Q>>::start
  31: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  32: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  33: rustc::ty::query::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::mir_borrowck
  34: rustc::ty::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::par_body_owners
  35: rustc::util::common::time
  36: rustc::ty::context::tls::enter_context
  37: <std::thread::local::LocalKey<T>>::with
  38: rustc::ty::context::TyCtxt::create_and_enter
  39: rustc_driver::driver::compile_input
  40: rustc_driver::run_compiler_with_pool
  41: <scoped_tls::ScopedKey<T>>::set
  42: rustc_driver::run_compiler
  43: rustc_driver::monitor::{{closure}}
  44: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:102
  45: rustc_driver::run
  46: rustc_driver::main
  47: std::rt::lang_start::{{closure}}
  48: std::panicking::try::do_call
             at src/libstd/rt.rs:59
             at src/libstd/panicking.rs:310
  49: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:102
  50: std::rt::lang_start_internal
             at src/libstd/panicking.rs:289
             at src/libstd/panic.rs:398
             at src/libstd/rt.rs:58
  51: main
  52: __libc_start_main
  53: <unknown>
query stack during panic:
#0 [mir_validated] processing `<[u8; _] as FromUnchecked>::from_unchecked`
#1 [mir_borrowck] processing `<[u8; _] as FromUnchecked>::from_unchecked`
end of query stack
error: aborting due to previous error


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.32.0 (9fda7c223 2019-01-16) running on x86_64-unknown-linux-gnu

note: compiler flags: -C codegen-units=1 -C debuginfo=2 --crate-type bin

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

error: Could not compile `playground`.

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

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions