Closed
Description
Code
#![feature(allocator_api)]
use std::alloc::{AllocError, Allocator, GlobalAlloc, Layout};
use std::ptr::NonNull;
struct MyAllocator {}
impl MyAllocator {
fn new() -> Self {
Self {}
}
}
unsafe impl GlobalAlloc for MyAllocator {
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
std::ptr::null_mut()
}
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}
#[derive(Clone, Debug)]
struct RefAlloc<'a, T: GlobalAlloc>(&'a T);
impl<'a, T: GlobalAlloc> RefAlloc<'a, T> {
fn new(inner: &'a T) -> Self {
Self(inner)
}
}
unsafe impl<'a, T: GlobalAlloc> Allocator for RefAlloc<'a, T> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let ptr = unsafe { self.0.alloc(layout) };
if ptr.is_null() {
return Err(AllocError);
}
unsafe {
Ok(NonNull::new_unchecked(std::slice::from_raw_parts_mut(
ptr,
layout.size(),
)))
}
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
self.0.dealloc(ptr.as_ptr(), layout)
}
}
fn main() {
let my_alloc: MyAllocator = MyAllocator::new();
// Constructing a box using the RefAlloc causes the compiler to crash
let a_box = Box::new_in(0, RefAlloc::new(&my_alloc));
println!("Hello, world! {:?}", a_box);
}
Meta
rustc --version --verbose
:
rustc 1.59.0-nightly (5531927e8 2021-12-16)
binary: rustc
commit-hash: 5531927e8af9b99ad923af4c827c91038bca51ee
commit-date: 2021-12-16
host: aarch64-apple-darwin
release: 1.59.0-nightly
LLVM version: 13.0.0
Error output
thread 'rustc' panicked at 'assertion failed: i < this.fields.count()', /rustc/5531927e8af9b99ad923af4c827c91038bca51ee/compiler/rustc_middle/src/ty/layout.rs:2311:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
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/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
note: rustc 1.59.0-nightly (5531927e8 2021-12-16) running on aarch64-apple-darwin
note: compiler flags: -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C incremental --crate-type bin
note: some of the compiler flags provided by cargo are hidden
query stack during panic:
end of query stack
Backtrace
stack backtrace:
0: _rust_begin_unwind
1: core::panicking::panic_fmt
2: core::panicking::panic
3: <&rustc_middle::ty::TyS as rustc_target::abi::TyAbiInterface<rustc_codegen_llvm::context::CodegenCx>>::ty_and_layout_field
4: <rustc_target::abi::TyAndLayout<&rustc_middle::ty::TyS> as rustc_codegen_llvm::type_of::LayoutLlvmExt>::scalar_pair_element_llvm_type
5: <rustc_target::abi::TyAndLayout<&rustc_middle::ty::TyS> as rustc_codegen_llvm::type_of::LayoutLlvmExt>::scalar_pair_element_llvm_type
6: <rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS> as rustc_codegen_llvm::abi::FnAbiLlvmExt>::llvm_type
7: <rustc_codegen_llvm::context::CodegenCx as rustc_codegen_ssa::traits::declare::PreDefineMethods>::predefine_fn
8: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
9: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
10: rustc_codegen_llvm::base::compile_codegen_unit
11: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
12: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
13: <rustc_interface::queries::Queries>::ongoing_codegen
14: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorReported>>
15: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
16: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Notes
- This issue is also reproducible in the Rust Playground.