diff options
| author | bors <bors@rust-lang.org> | 2019-08-18 13:22:38 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-08-18 13:22:38 +0000 |
| commit | ea52be482ab4945fda63cb65b6a198309a041e3c (patch) | |
| tree | 7c1bd1c098b3d0bced875d58a3953717fa15e177 | |
| parent | 71e2882973e63b9ddc837a61ac8631e6451d31a9 (diff) | |
| parent | 1ea88a8689a461638fef31e01e62fffc63ac5b79 (diff) | |
| download | rust-ea52be482ab4945fda63cb65b6a198309a041e3c.tar.gz rust-ea52be482ab4945fda63cb65b6a198309a041e3c.zip | |
Auto merge of #63635 - oli-obk:default-slice-dangles, r=eddyb
Do not generate allocations for zero sized allocations Alternative to https://github.com/rust-lang/rust/issues/62487 r? @eddyb There are other places where we could do this, too, but that would cause `static FOO: () = ();` to not have a unique address
| -rw-r--r-- | src/librustc_codegen_llvm/common.rs | 24 | ||||
| -rw-r--r-- | src/test/ui/consts/zst_no_llvm_alloc.rs | 19 |
2 files changed, 34 insertions, 9 deletions
diff --git a/src/librustc_codegen_llvm/common.rs b/src/librustc_codegen_llvm/common.rs index b0c94a139be..19f18088579 100644 --- a/src/librustc_codegen_llvm/common.rs +++ b/src/librustc_codegen_llvm/common.rs @@ -333,15 +333,21 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> { offset: Size, ) -> PlaceRef<'tcx, &'ll Value> { assert_eq!(alloc.align, layout.align.abi); - let init = const_alloc_to_llvm(self, alloc); - let base_addr = self.static_addr_of(init, alloc.align, None); - - let llval = unsafe { llvm::LLVMConstInBoundsGEP( - self.const_bitcast(base_addr, self.type_i8p()), - &self.const_usize(offset.bytes()), - 1, - )}; - let llval = self.const_bitcast(llval, self.type_ptr_to(layout.llvm_type(self))); + let llty = self.type_ptr_to(layout.llvm_type(self)); + let llval = if layout.size == Size::ZERO { + let llval = self.const_usize(alloc.align.bytes()); + unsafe { llvm::LLVMConstIntToPtr(llval, llty) } + } else { + let init = const_alloc_to_llvm(self, alloc); + let base_addr = self.static_addr_of(init, alloc.align, None); + + let llval = unsafe { llvm::LLVMConstInBoundsGEP( + self.const_bitcast(base_addr, self.type_i8p()), + &self.const_usize(offset.bytes()), + 1, + )}; + self.const_bitcast(llval, llty) + }; PlaceRef::new_sized(llval, layout, alloc.align) } diff --git a/src/test/ui/consts/zst_no_llvm_alloc.rs b/src/test/ui/consts/zst_no_llvm_alloc.rs new file mode 100644 index 00000000000..5d779355400 --- /dev/null +++ b/src/test/ui/consts/zst_no_llvm_alloc.rs @@ -0,0 +1,19 @@ +// run-pass + +#[repr(align(4))] +struct Foo; + +static FOO: Foo = Foo; + +fn main() { + let x: &'static () = &(); + assert_eq!(x as *const () as usize, 1); + let x: &'static Foo = &Foo; + assert_eq!(x as *const Foo as usize, 4); + + // statics must have a unique address + assert_ne!(&FOO as *const Foo as usize, 4); + + assert_eq!(<Vec<i32>>::new().as_ptr(), <&[i32]>::default().as_ptr()); + assert_eq!(<Box<[i32]>>::default().as_ptr(), (&[]).as_ptr()); +} |
