From f9bbb5f31df8232fb1e17a3408b62590c30112c7 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 19 Aug 2018 17:36:04 +0200 Subject: [PATCH 01/11] panic when instantiating an uninhabited type via mem::{uninitialized,zeroed} --- src/librustc_codegen_llvm/mir/block.rs | 49 +++++++++++++++++++ src/librustc_target/abi/mod.rs | 8 +++ .../run-pass/panic-uninitialized-zeroed.rs | 31 ++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/test/run-pass/panic-uninitialized-zeroed.rs diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index a534b4e478fb7..72fb9df6f81bd 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -463,6 +463,55 @@ impl FunctionCx<'a, 'll, 'tcx> { return; } + if (intrinsic == Some("init") || intrinsic == Some("uninit")) && + bx.cx.layout_of(sig.output()).abi.is_uninhabited() + { + let loc = bx.sess().codemap().lookup_char_pos(span.lo()); + let filename = Symbol::intern(&loc.file.name.to_string()).as_str(); + let filename = C_str_slice(bx.cx, filename); + let line = C_u32(bx.cx, loc.line as u32); + let col = C_u32(bx.cx, loc.col.to_usize() as u32 + 1); + let align = tcx.data_layout.aggregate_align + .max(tcx.data_layout.i32_align) + .max(tcx.data_layout.pointer_align); + + let str = if intrinsic == Some("init") { + "Attempted to instantiate an uninhabited type (e.g. `!`) \ + using mem::zeroed()" + } else { + "Attempted to instantiate an uninhabited type (e.g. `!`) \ + using mem::uninitialized()" + }; + let msg_str = Symbol::intern(str).as_str(); + let msg_str = C_str_slice(bx.cx, msg_str); + let msg_file_line_col = C_struct(bx.cx, + &[msg_str, filename, line, col], + false); + let msg_file_line_col = consts::addr_of(bx.cx, + msg_file_line_col, + align, + Some("panic_loc")); + + // Obtain the panic entry point. + let def_id = + common::langcall(bx.tcx(), Some(span), "", lang_items::PanicFnLangItem); + let instance = ty::Instance::mono(bx.tcx(), def_id); + let fn_ty = FnType::of_instance(bx.cx, &instance); + let llfn = callee::get_fn(bx.cx, instance); + + // Codegen the actual panic invoke/call. + do_call( + self, + bx, + fn_ty, + llfn, + &[msg_file_line_col], + destination.as_ref().map(|(_, bb)| (ReturnDest::Nothing, *bb)), + cleanup, + ); + return; + } + let extra_args = &args[sig.inputs().len()..]; let extra_args = extra_args.iter().map(|op_arg| { let op_ty = op_arg.ty(self.mir, bx.tcx()); diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 5c4cd849f89bc..96eb69163220e 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -802,6 +802,14 @@ impl Abi { _ => false, } } + + /// Returns true if this is an uninhabited type + pub fn is_uninhabited(&self) -> bool { + match *self { + Abi::Uninhabited => true, + _ => false, + } + } } #[derive(PartialEq, Eq, Hash, Debug)] diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs new file mode 100644 index 0000000000000..a4115f8fa1d65 --- /dev/null +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -0,0 +1,31 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This test checks that instantiating an uninhabited type via `mem::{uninitialized,zeroed}` results +// in a runtime panic. + +#![feature(never_type)] + +use std::{mem, panic}; + +struct Foo { + x: u8, + y: !, +} + +fn main() { + unsafe { + panic::catch_unwind(|| mem::uninitialized::()).is_err(); + panic::catch_unwind(|| mem::zeroed::()).is_err(); + + panic::catch_unwind(|| mem::uninitialized::()).is_err(); + panic::catch_unwind(|| mem::zeroed::()).is_err(); + } +} From bd3c7812cbadf875ebcc79d65a1f65c29dea0c8c Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:34:38 +0200 Subject: [PATCH 02/11] use is_uninhabited in more places --- src/librustc/ty/layout.rs | 10 +++++----- src/librustc_codegen_llvm/debuginfo/mod.rs | 2 +- src/librustc_codegen_llvm/declare.rs | 4 ++-- src/librustc_codegen_llvm/mir/place.rs | 4 ++-- src/librustc_codegen_llvm/mir/rvalue.rs | 2 +- src/librustc_mir/interpret/operand.rs | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index e756313a1253d..8bfe46568eb19 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -449,7 +449,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { } } - if sized && fields.iter().any(|f| f.abi == Abi::Uninhabited) { + if sized && fields.iter().any(|f| f.abi.is_uninhabited()) { abi = Abi::Uninhabited; } @@ -724,7 +724,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { // See issue #49298 for more details on the need to leave space // for non-ZST uninhabited data (mostly partial initialization). let absent = |fields: &[TyLayout<'_>]| { - let uninhabited = fields.iter().any(|f| f.abi == Abi::Uninhabited); + let uninhabited = fields.iter().any(|f| f.abi.is_uninhabited()); let is_zst = fields.iter().all(|f| f.is_zst()); uninhabited && is_zst }; @@ -872,7 +872,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { _ => Abi::Aggregate { sized: true }, }; - if st.iter().all(|v| v.abi == Abi::Uninhabited) { + if st.iter().all(|v| v.abi.is_uninhabited()) { abi = Abi::Uninhabited; } @@ -900,7 +900,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { let discr_type = def.repr.discr_type(); let bits = Integer::from_attr(tcx, discr_type).size().bits(); for (i, discr) in def.discriminants(tcx).enumerate() { - if variants[i].iter().any(|f| f.abi == Abi::Uninhabited) { + if variants[i].iter().any(|f| f.abi.is_uninhabited()) { continue; } let mut x = discr.val as i128; @@ -1096,7 +1096,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { } } - if layout_variants.iter().all(|v| v.abi == Abi::Uninhabited) { + if layout_variants.iter().all(|v| v.abi.is_uninhabited()) { abi = Abi::Uninhabited; } diff --git a/src/librustc_codegen_llvm/debuginfo/mod.rs b/src/librustc_codegen_llvm/debuginfo/mod.rs index 99919a940b405..7b0c413e85761 100644 --- a/src/librustc_codegen_llvm/debuginfo/mod.rs +++ b/src/librustc_codegen_llvm/debuginfo/mod.rs @@ -279,7 +279,7 @@ pub fn create_function_debug_context( } None => {} }; - if cx.layout_of(sig.output()).abi == ty::layout::Abi::Uninhabited { + if cx.layout_of(sig.output()).abi.is_uninhabited() { flags = flags | DIFlags::FlagNoReturn; } diff --git a/src/librustc_codegen_llvm/declare.rs b/src/librustc_codegen_llvm/declare.rs index 5e743ac51bc61..7141c9ece89d7 100644 --- a/src/librustc_codegen_llvm/declare.rs +++ b/src/librustc_codegen_llvm/declare.rs @@ -23,7 +23,7 @@ use llvm; use llvm::AttributePlace::Function; use rustc::ty::{self, Ty}; -use rustc::ty::layout::{self, LayoutOf}; +use rustc::ty::layout::LayoutOf; use rustc::session::config::Sanitizer; use rustc_data_structures::small_c_str::SmallCStr; use rustc_target::spec::PanicStrategy; @@ -137,7 +137,7 @@ pub fn declare_fn( let fty = FnType::new(cx, sig, &[]); let llfn = declare_raw_fn(cx, name, fty.llvm_cconv(), fty.llvm_type(cx)); - if cx.layout_of(sig.output()).abi == layout::Abi::Uninhabited { + if cx.layout_of(sig.output()).abi.is_uninhabited() { llvm::Attribute::NoReturn.apply_llfn(Function, llfn); } diff --git a/src/librustc_codegen_llvm/mir/place.rs b/src/librustc_codegen_llvm/mir/place.rs index c781b456af6b2..e7b6f5908a4d1 100644 --- a/src/librustc_codegen_llvm/mir/place.rs +++ b/src/librustc_codegen_llvm/mir/place.rs @@ -275,7 +275,7 @@ impl PlaceRef<'ll, 'tcx> { /// Obtain the actual discriminant of a value. pub fn codegen_get_discr(self, bx: &Builder<'a, 'll, 'tcx>, cast_to: Ty<'tcx>) -> &'ll Value { let cast_to = bx.cx.layout_of(cast_to).immediate_llvm_type(bx.cx); - if self.layout.abi == layout::Abi::Uninhabited { + if self.layout.abi.is_uninhabited() { return C_undef(cast_to); } match self.layout.variants { @@ -338,7 +338,7 @@ impl PlaceRef<'ll, 'tcx> { /// Set the discriminant for a new value of the given case of the given /// representation. pub fn codegen_set_discr(&self, bx: &Builder<'a, 'll, 'tcx>, variant_index: usize) { - if self.layout.for_variant(bx.cx, variant_index).abi == layout::Abi::Uninhabited { + if self.layout.for_variant(bx.cx, variant_index).abi.is_uninhabited() { return; } match self.layout.variants { diff --git a/src/librustc_codegen_llvm/mir/rvalue.rs b/src/librustc_codegen_llvm/mir/rvalue.rs index c3ec347f60876..fa22bdff94ddd 100644 --- a/src/librustc_codegen_llvm/mir/rvalue.rs +++ b/src/librustc_codegen_llvm/mir/rvalue.rs @@ -290,7 +290,7 @@ impl FunctionCx<'a, 'll, 'tcx> { mir::CastKind::Misc => { assert!(cast.is_llvm_immediate()); let ll_t_out = cast.immediate_llvm_type(bx.cx); - if operand.layout.abi == layout::Abi::Uninhabited { + if operand.layout.abi.is_uninhabited() { return (bx, OperandRef { val: OperandValue::Immediate(C_undef(ll_t_out)), layout: cast, diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index fef2f916b4156..a11150c47836e 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -514,7 +514,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> rval: OpTy<'tcx>, ) -> EvalResult<'tcx, (u128, usize)> { trace!("read_discriminant_value {:#?}", rval.layout); - if rval.layout.abi == layout::Abi::Uninhabited { + if rval.layout.abi.is_uninhabited() { return err!(Unreachable); } From 7294fcdc410051777f124a80d63e7a7a629fcec5 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:36:51 +0200 Subject: [PATCH 03/11] improve panic message --- src/librustc_codegen_llvm/mir/block.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index 72fb9df6f81bd..c8515a4b6fffa 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -475,14 +475,12 @@ impl FunctionCx<'a, 'll, 'tcx> { .max(tcx.data_layout.i32_align) .max(tcx.data_layout.pointer_align); - let str = if intrinsic == Some("init") { - "Attempted to instantiate an uninhabited type (e.g. `!`) \ - using mem::zeroed()" - } else { - "Attempted to instantiate an uninhabited type (e.g. `!`) \ - using mem::uninitialized()" - }; - let msg_str = Symbol::intern(str).as_str(); + let str = format!( + "Attempted to instantiate uninhabited type {} using mem::{}", + sig.output(), + if intrinsic == Some("init") { "zeroed" } else { "uninitialized" } + ); + let msg_str = Symbol::intern(&str).as_str(); let msg_str = C_str_slice(bx.cx, msg_str); let msg_file_line_col = C_struct(bx.cx, &[msg_str, filename, line, col], From 00ba5cb00b52e69a5564c4f8112d3b9f44d0e455 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:39:03 +0200 Subject: [PATCH 04/11] move our check to reuse a previous computation --- src/librustc_codegen_llvm/mir/block.rs | 41 +++++++++++++------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index c8515a4b6fffa..8a0ec731f45a6 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -463,8 +463,28 @@ impl FunctionCx<'a, 'll, 'tcx> { return; } + let extra_args = &args[sig.inputs().len()..]; + let extra_args = extra_args.iter().map(|op_arg| { + let op_ty = op_arg.ty(self.mir, bx.tcx()); + self.monomorphize(&op_ty) + }).collect::>(); + + let fn_ty = match def { + Some(ty::InstanceDef::Virtual(..)) => { + FnType::new_vtable(bx.cx, sig, &extra_args) + } + Some(ty::InstanceDef::DropGlue(_, None)) => { + // empty drop glue - a nop. + let &(_, target) = destination.as_ref().unwrap(); + funclet_br(self, bx, target); + return; + } + _ => FnType::new(bx.cx, sig, &extra_args) + }; + + // emit a panic instead of instantiating an uninhabited type if (intrinsic == Some("init") || intrinsic == Some("uninit")) && - bx.cx.layout_of(sig.output()).abi.is_uninhabited() + fn_ty.ret.layout.abi.is_uninhabited() { let loc = bx.sess().codemap().lookup_char_pos(span.lo()); let filename = Symbol::intern(&loc.file.name.to_string()).as_str(); @@ -510,25 +530,6 @@ impl FunctionCx<'a, 'll, 'tcx> { return; } - let extra_args = &args[sig.inputs().len()..]; - let extra_args = extra_args.iter().map(|op_arg| { - let op_ty = op_arg.ty(self.mir, bx.tcx()); - self.monomorphize(&op_ty) - }).collect::>(); - - let fn_ty = match def { - Some(ty::InstanceDef::Virtual(..)) => { - FnType::new_vtable(bx.cx, sig, &extra_args) - } - Some(ty::InstanceDef::DropGlue(_, None)) => { - // empty drop glue - a nop. - let &(_, target) = destination.as_ref().unwrap(); - funclet_br(self, bx, target); - return; - } - _ => FnType::new(bx.cx, sig, &extra_args) - }; - // The arguments we'll be passing. Plus one to account for outptr, if used. let arg_count = fn_ty.args.len() + fn_ty.ret.is_indirect() as usize; let mut llargs = Vec::with_capacity(arg_count); From 22cde0efa5c95d740ab04d7d47e3f1be55725cd8 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:49:00 +0200 Subject: [PATCH 05/11] improve the run-pass test --- .../run-pass/panic-uninitialized-zeroed.rs | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs index a4115f8fa1d65..fd88bba49c484 100644 --- a/src/test/run-pass/panic-uninitialized-zeroed.rs +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -22,10 +22,40 @@ struct Foo { fn main() { unsafe { - panic::catch_unwind(|| mem::uninitialized::()).is_err(); - panic::catch_unwind(|| mem::zeroed::()).is_err(); + assert_eq!( + panic::catch_unwind(|| { + mem::uninitialized::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type ! using mem::uninitialized" + })), + Some(true) + ); - panic::catch_unwind(|| mem::uninitialized::()).is_err(); - panic::catch_unwind(|| mem::zeroed::()).is_err(); + assert_eq!( + panic::catch_unwind(|| { + mem::zeroed::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type ! using mem::zeroed" + })), + Some(true) + ); + + assert_eq!( + panic::catch_unwind(|| { + mem::uninitialized::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type Foo using mem::uninitialized" + })), + Some(true) + ); + + assert_eq!( + panic::catch_unwind(|| { + mem::zeroed::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type Foo using mem::zeroed" + })), + Some(true) + ); } } From 0bf40d86d8e93cc6d7a11419d5fb2d6d7e5fa2b0 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:49:35 +0200 Subject: [PATCH 06/11] add empty enum to the test cases --- .../run-pass/panic-uninitialized-zeroed.rs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs index fd88bba49c484..f40ea7cce078a 100644 --- a/src/test/run-pass/panic-uninitialized-zeroed.rs +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -20,6 +20,8 @@ struct Foo { y: !, } +enum Bar {} + fn main() { unsafe { assert_eq!( @@ -57,5 +59,23 @@ fn main() { })), Some(true) ); + + assert_eq!( + panic::catch_unwind(|| { + mem::uninitialized::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type Bar using mem::uninitialized" + })), + Some(true) + ); + + assert_eq!( + panic::catch_unwind(|| { + mem::zeroed::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type Bar using mem::zeroed" + })), + Some(true) + ); } } From cef3aa0261fb8d343b11a9d266c065deee140007 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 23 Sep 2018 00:37:08 +0200 Subject: [PATCH 07/11] allow dead_code --- src/test/run-pass/panic-uninitialized-zeroed.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs index f40ea7cce078a..db2d7bdf4657f 100644 --- a/src/test/run-pass/panic-uninitialized-zeroed.rs +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -15,6 +15,7 @@ use std::{mem, panic}; +#[allow(dead_code)] struct Foo { x: u8, y: !, From fba4e291ce0143eecd268ad195df49689016b3ca Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:18:28 +0200 Subject: [PATCH 08/11] adapt to change in Session API --- src/librustc_codegen_llvm/mir/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index 8a0ec731f45a6..709fceb492509 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -486,7 +486,7 @@ impl FunctionCx<'a, 'll, 'tcx> { if (intrinsic == Some("init") || intrinsic == Some("uninit")) && fn_ty.ret.layout.abi.is_uninhabited() { - let loc = bx.sess().codemap().lookup_char_pos(span.lo()); + let loc = bx.sess().source_map().lookup_char_pos(span.lo()); let filename = Symbol::intern(&loc.file.name.to_string()).as_str(); let filename = C_str_slice(bx.cx, filename); let line = C_u32(bx.cx, loc.line as u32); From a5dfbdba7a8fe4d7b76e31b5692ab28be874df83 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 1 Sep 2018 20:13:13 +0200 Subject: [PATCH 09/11] make the nil-enum test work again --- src/test/debuginfo/nil-enum.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/test/debuginfo/nil-enum.rs b/src/test/debuginfo/nil-enum.rs index 94377421c0b0c..ab9c7e2dd2758 100644 --- a/src/test/debuginfo/nil-enum.rs +++ b/src/test/debuginfo/nil-enum.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// NOTE Instantiating an empty enum is UB. This test may break in the future. + // LLDB can't handle zero-sized values // ignore-lldb @@ -25,8 +27,11 @@ #![allow(unused_variables)] #![feature(omit_gdb_pretty_printer_section)] +#![feature(maybe_uninit)] #![omit_gdb_pretty_printer_section] +use std::mem::MaybeUninit; + enum ANilEnum {} enum AnotherNilEnum {} @@ -35,8 +40,8 @@ enum AnotherNilEnum {} // The error from gdbr is expected since nil enums are not supposed to exist. fn main() { unsafe { - let first: ANilEnum = ::std::mem::zeroed(); - let second: AnotherNilEnum = ::std::mem::zeroed(); + let first: ANilEnum = MaybeUninit::uninitialized().into_inner(); + let second: AnotherNilEnum = MaybeUninit::uninitialized().into_inner(); zzz(); // #break } From 0703af2a7753feba61ad3b6b08468a50043b27b4 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 20 Sep 2018 20:57:25 +0200 Subject: [PATCH 10/11] add codegen test --- src/test/codegen/box-maybe-uninit.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/codegen/box-maybe-uninit.rs diff --git a/src/test/codegen/box-maybe-uninit.rs b/src/test/codegen/box-maybe-uninit.rs new file mode 100644 index 0000000000000..168e1a3eba0c5 --- /dev/null +++ b/src/test/codegen/box-maybe-uninit.rs @@ -0,0 +1,23 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -O +#![crate_type="lib"] +#![feature(maybe_uninit)] + +use std::mem::MaybeUninit; + +// Boxing a `MaybeUninit` value should not copy junk from the stack +#[no_mangle] +pub fn box_uninitialized() -> Box> { + // CHECK-LABEL: @box_uninitialized + // CHECK-NOT: store + Box::new(MaybeUninit::uninitialized()) +} From dd65d732ed702302ac0943179cb2feec835975ee Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 21 Sep 2018 23:40:00 +0200 Subject: [PATCH 11/11] the test requires unwinding so we don't run it on the wasm32-bare target --- src/test/run-pass/panic-uninitialized-zeroed.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs index db2d7bdf4657f..2972f6efa32e0 100644 --- a/src/test/run-pass/panic-uninitialized-zeroed.rs +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// ignore-wasm32-bare always compiled as panic=abort right now and this requires unwinding // This test checks that instantiating an uninhabited type via `mem::{uninitialized,zeroed}` results // in a runtime panic.