From 307ee94a8a535019feadf69ce4258cdfb67a3a1c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 7 Mar 2022 23:06:59 -0800 Subject: [PATCH] only emit pointer-like metadata for BZST-allocator Box --- .../src/debuginfo/metadata.rs | 4 +++- .../debuginfo-box-with-large-allocator.rs | 23 +++++++++++++++++++ ...fo_with_uninhabitable_field_and_unsized.rs | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/debuginfo/debuginfo-box-with-large-allocator.rs diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 89fc898003737..a6e4878e5b361 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -617,7 +617,9 @@ pub fn type_metadata<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll ty::RawPtr(ty::TypeAndMut { ty: pointee_type, .. }) | ty::Ref(_, pointee_type, _) => { pointer_or_reference_metadata(cx, t, pointee_type, unique_type_id) } - ty::Adt(def, _) if def.is_box() => { + // Box may have a non-ZST allocator A. In that case, we + // cannot treat Box as just an owned alias of `*mut T`. + ty::Adt(def, substs) if def.is_box() && cx.layout_of(substs.type_at(1)).is_zst() => { pointer_or_reference_metadata(cx, t, t.boxed_ty(), unique_type_id) } ty::FnDef(..) | ty::FnPtr(_) => subroutine_type_metadata(cx, unique_type_id), diff --git a/src/test/ui/debuginfo/debuginfo-box-with-large-allocator.rs b/src/test/ui/debuginfo/debuginfo-box-with-large-allocator.rs new file mode 100644 index 0000000000000..761539227a79c --- /dev/null +++ b/src/test/ui/debuginfo/debuginfo-box-with-large-allocator.rs @@ -0,0 +1,23 @@ +// build-pass +// compile-flags: -Cdebuginfo=2 +// fixes issue #94725 + +#![feature(allocator_api)] + +use std::alloc::{AllocError, Allocator, Layout}; +use std::ptr::NonNull; + +struct ZST; + +unsafe impl Allocator for &ZST { + fn allocate(&self, layout: Layout) -> Result, AllocError> { + todo!() + } + unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { + todo!() + } +} + +fn main() { + let _ = Box::::new_in(43, &ZST); +} diff --git a/src/test/ui/debuginfo/debuginfo_with_uninhabitable_field_and_unsized.rs b/src/test/ui/debuginfo/debuginfo_with_uninhabitable_field_and_unsized.rs index 833a4726acb0f..b3f22ecf5115e 100644 --- a/src/test/ui/debuginfo/debuginfo_with_uninhabitable_field_and_unsized.rs +++ b/src/test/ui/debuginfo/debuginfo_with_uninhabitable_field_and_unsized.rs @@ -1,4 +1,4 @@ -// check-pass +// build-pass // compile-flags: -Cdebuginfo=2 // fixes issue #94149