From 646f16d7e4f451b0a6ee95fd61c1ae12a567bbe0 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Thu, 9 May 2013 11:44:52 -0700 Subject: [PATCH 1/3] core: Make intrinsics::init unsafe as per #3920 --- src/libcore/unstable/intrinsics.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libcore/unstable/intrinsics.rs b/src/libcore/unstable/intrinsics.rs index cfd305f4b70c1..3ab7c3368d888 100644 --- a/src/libcore/unstable/intrinsics.rs +++ b/src/libcore/unstable/intrinsics.rs @@ -42,7 +42,10 @@ pub extern "rust-intrinsic" { pub fn get_tydesc() -> *(); - pub fn init() -> T; + /// init is unsafe because it returns a zeroed-out datum, + /// which is unsafe unless T is POD. We don't have a POD + /// kind yet. (See #4074) + pub unsafe fn init() -> T; #[cfg(not(stage0))] pub unsafe fn uninit() -> T; From cd235d423008ce9a35d0d5f3a7e35a91b8c3033b Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Thu, 9 May 2013 12:15:37 -0700 Subject: [PATCH 2/3] core: Make intrinsics::forget unsafe, too --- src/libcore/unstable/intrinsics.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libcore/unstable/intrinsics.rs b/src/libcore/unstable/intrinsics.rs index 3ab7c3368d888..a820c5d15a8db 100644 --- a/src/libcore/unstable/intrinsics.rs +++ b/src/libcore/unstable/intrinsics.rs @@ -50,7 +50,9 @@ pub extern "rust-intrinsic" { #[cfg(not(stage0))] pub unsafe fn uninit() -> T; - pub fn forget(_: T) -> (); + /// forget is unsafe because the caller is responsible for + /// ensuring the argument is deallocated already + pub unsafe fn forget(_: T) -> (); pub fn needs_drop() -> bool; From ca18ed8f64daa12138352541f7d8ccfc593b2970 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Thu, 9 May 2013 12:15:44 -0700 Subject: [PATCH 3/3] testsuite: Test that init and forget are both unsafe --- src/test/compile-fail/forget-init-unsafe.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/compile-fail/forget-init-unsafe.rs diff --git a/src/test/compile-fail/forget-init-unsafe.rs b/src/test/compile-fail/forget-init-unsafe.rs new file mode 100644 index 0000000000000..2361b5ad6a9cf --- /dev/null +++ b/src/test/compile-fail/forget-init-unsafe.rs @@ -0,0 +1,17 @@ +// Copyright 2013 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. + +use core::unstable::intrinsics::{init, forget}; + +// Test that the `forget` and `init` intrinsics are really unsafe +pub fn main() { + let stuff = init::(); //~ ERROR access to unsafe function requires unsafe + forget(stuff); //~ ERROR access to unsafe function requires unsafe +} \ No newline at end of file