Remove the deprecated `core::raw` and `std::raw` module.

This commit is contained in:
Charles Lew 2021-07-03 12:24:47 +08:00
parent cd48e61c5d
commit 0d1919c7ab
8 changed files with 15 additions and 146 deletions

View File

@ -255,7 +255,6 @@ pub mod option;
pub mod panic;
pub mod panicking;
pub mod pin;
pub mod raw;
pub mod result;
#[unstable(feature = "async_stream", issue = "79024")]
pub mod stream;

View File

@ -1,90 +0,0 @@
#![allow(missing_docs)]
#![unstable(feature = "raw", issue = "27751")]
#![rustc_deprecated(
since = "1.53.0",
reason = "use pointer metadata APIs instead https://github.com/rust-lang/rust/issues/81513"
)]
//! Contains struct definitions for the layout of compiler built-in types.
//!
//! They can be used as targets of transmutes in unsafe code for manipulating
//! the raw representations directly.
//!
//! Their definition should always match the ABI defined in
//! `rustc_middle::ty::layout`.
/// The representation of a trait object like `&dyn SomeTrait`.
///
/// This struct has the same layout as types like `&dyn SomeTrait` and
/// `Box<dyn AnotherTrait>`.
///
/// `TraitObject` is guaranteed to match layouts, but it is not the
/// type of trait objects (e.g., the fields are not directly accessible
/// on a `&dyn SomeTrait`) nor does it control that layout (changing the
/// definition will not change the layout of a `&dyn SomeTrait`). It is
/// only designed to be used by unsafe code that needs to manipulate
/// the low-level details.
///
/// There is no way to refer to all trait objects generically, so the only
/// way to create values of this type is with functions like
/// [`std::mem::transmute`][transmute]. Similarly, the only way to create a true
/// trait object from a `TraitObject` value is with `transmute`.
///
/// [transmute]: crate::intrinsics::transmute
///
/// Synthesizing a trait object with mismatched types—one where the
/// vtable does not correspond to the type of the value to which the
/// data pointer points—is highly likely to lead to undefined
/// behavior.
///
/// # Examples
///
/// ```
/// #![feature(raw)]
///
/// use std::{mem, raw};
///
/// // an example trait
/// trait Foo {
/// fn bar(&self) -> i32;
/// }
///
/// impl Foo for i32 {
/// fn bar(&self) -> i32 {
/// *self + 1
/// }
/// }
///
/// let value: i32 = 123;
///
/// // let the compiler make a trait object
/// let object: &dyn Foo = &value;
///
/// // look at the raw representation
/// let raw_object: raw::TraitObject = unsafe { mem::transmute(object) };
///
/// // the data pointer is the address of `value`
/// assert_eq!(raw_object.data as *const i32, &value as *const _);
///
/// let other_value: i32 = 456;
///
/// // construct a new object, pointing to a different `i32`, being
/// // careful to use the `i32` vtable from `object`
/// let synthesized: &dyn Foo = unsafe {
/// mem::transmute(raw::TraitObject {
/// data: &other_value as *const _ as *mut (),
/// vtable: raw_object.vtable,
/// })
/// };
///
/// // it should work just as if we had constructed a trait object out of
/// // `other_value` directly
/// assert_eq!(synthesized.bar(), 457);
/// ```
#[repr(C)]
#[derive(Copy, Clone)]
#[allow(missing_debug_implementations)]
pub struct TraitObject {
pub data: *mut (),
pub vtable: *mut (),
}

View File

@ -29,7 +29,6 @@
#![feature(try_find)]
#![feature(is_sorted)]
#![feature(pattern)]
#![feature(raw)]
#![feature(sort_internals)]
#![feature(slice_partition_at_index)]
#![feature(maybe_uninit_uninit_array)]

View File

@ -97,28 +97,6 @@ fn test_transmute_copy() {
assert_eq!(1, unsafe { transmute_copy(&1) });
}
// Remove this test when `std::raw` is removed.
// The replacement pointer metadata APIs are tested in library/core/tests/ptr.rs
#[allow(deprecated)]
#[test]
fn test_transmute() {
trait Foo {
fn dummy(&self) {}
}
impl Foo for isize {}
let a = box 100isize as Box<dyn Foo>;
unsafe {
let x: ::core::raw::TraitObject = transmute(a);
assert!(*(x.data as *const isize) == 100);
let _x: Box<dyn Foo> = transmute(x);
}
unsafe {
assert_eq!(transmute::<_, Vec<u8>>("L".to_string()), [76]);
}
}
#[test]
#[allow(dead_code)]
fn test_discriminant_send_sync() {

View File

@ -23,7 +23,6 @@
#![feature(unwind_attributes)]
#![feature(abi_thiscall)]
#![feature(rustc_attrs)]
#![feature(raw)]
#![panic_runtime]
#![feature(panic_runtime)]
// `real_imp` is unused with Miri, so silence warnings.

View File

@ -303,7 +303,6 @@
#![feature(pin_static_ref)]
#![feature(prelude_import)]
#![feature(ptr_internals)]
#![feature(raw)]
#![feature(ready_macro)]
#![feature(rustc_attrs)]
#![feature(rustc_private)]
@ -455,9 +454,6 @@ pub use core::pin;
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::ptr;
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated, deprecated_in_future)]
pub use core::raw;
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::result;
#[unstable(feature = "async_stream", issue = "79024")]
pub use core::stream;

View File

@ -1,12 +1,6 @@
// run-pass
// Remove this file when `std::raw` is removed.
// The replacement pointer metadata APIs are tested in library/core/tests/ptr.rs
#![allow(deprecated)]
#![feature(raw)]
use std::mem;
use std::raw;
#![feature(ptr_metadata)]
trait Foo {
fn foo(&self) {}
@ -31,13 +25,10 @@ fn main() {
// And conversion to a void pointer/address for trait objects too.
let a: *mut dyn Foo = &mut Bar;
let b = a as *mut ();
let b = a as *mut () as usize;
let c = a as *const () as usize;
let d = unsafe {
let r: raw::TraitObject = mem::transmute(a);
r.data
};
let d = a.to_raw_parts().0 as usize;
assert_eq!(b, d);
assert_eq!(c, d as usize);
assert_eq!(c, d);
}

View File

@ -1,12 +1,11 @@
// run-pass
// Test structs with always-unsized fields.
#![allow(warnings)]
#![feature(box_syntax, unsize, raw)]
#![feature(box_syntax, unsize, ptr_metadata)]
use std::mem;
use std::raw;
use std::ptr;
use std::slice;
struct Foo<T> {
@ -28,7 +27,7 @@ trait Tr {
}
struct St {
f: usize
f: usize,
}
impl Tr for St {
@ -38,7 +37,7 @@ impl Tr for St {
}
struct Qux<'a> {
f: Tr+'a
f: Tr + 'a,
}
pub fn main() {
@ -56,10 +55,10 @@ pub fn main() {
unsafe {
struct Foo_<T> {
f: [T; 3]
f: [T; 3],
}
let data: Box<Foo_<i32>> = box Foo_{f: [1, 2, 3] };
let data: Box<Foo_<i32>> = box Foo_ { f: [1, 2, 3] };
let x: &Foo<i32> = mem::transmute(slice::from_raw_parts(&*data, 3));
assert_eq!(x.f.len(), 3);
assert_eq!(x.f[0], 1);
@ -69,8 +68,8 @@ pub fn main() {
f2: [u8; 5],
}
let data: Box<_> = box Baz_ {
f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
let data: Box<_> =
box Baz_ { f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
assert_eq!(x.f1, 42);
let chs: Vec<char> = x.f2.chars().collect();
@ -82,15 +81,13 @@ pub fn main() {
assert_eq!(chs[4], 'e');
struct Qux_ {
f: St
f: St,
}
let obj: Box<St> = box St { f: 42 };
let obj: &Tr = &*obj;
let obj: raw::TraitObject = mem::transmute(&*obj);
let data: Box<_> = box Qux_{ f: St { f: 234 } };
let x: &Qux = mem::transmute(raw::TraitObject { vtable: obj.vtable,
data: mem::transmute(&*data) });
let data: Box<_> = box Qux_ { f: St { f: 234 } };
let x: &Qux = &*ptr::from_raw_parts::<Qux>((&*data as *const _).cast(), ptr::metadata(obj));
assert_eq!(x.f.foo(), 234);
}
}