diff options
| author | bors <bors@rust-lang.org> | 2017-12-19 07:05:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-12-19 07:05:05 +0000 |
| commit | a9f047c048eac4f135dd6df5223b7195d651539d (patch) | |
| tree | 6aafdf5c1ae3526fa5cd6ff1e072b4dfe355a443 /src/test | |
| parent | c8b94c6aa1bdee2fb6371e3f445f0abbba7c5093 (diff) | |
| parent | 5c656f0c72d4a1aeff9a761fe455e522e975eee2 (diff) | |
Auto merge of #46664 - mikeyhew:raw_pointer_self, r=arielb1
arbitrary_self_types: add support for raw pointer `self` types This adds support for raw pointer `self` types, under the `arbitrary_self_types` feature flag. Types like `self: *const Self`, `self: *const Rc<Self>`, `self: Rc<*const Self` are all supported. Object safety checks are updated to allow`self: *const Self` and `self: *mut Self`. This PR does not add support for `*const self` and `*mut self` syntax. That can be added in a later PR once this code is reviewed and merged. #44874 r? @arielb1
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/run-pass/arbitrary_self_types_raw_pointer_struct.rs | 37 | ||||
| -rw-r--r-- | src/test/run-pass/arbitrary_self_types_raw_pointer_trait.rs | 70 | ||||
| -rw-r--r-- | src/test/ui/arbitrary-self-types-not-object-safe.stderr | 4 | ||||
| -rw-r--r-- | src/test/ui/feature-gate-arbitrary_self_types-raw-pointer.rs | 28 | ||||
| -rw-r--r-- | src/test/ui/feature-gate-arbitrary_self_types-raw-pointer.stderr | 29 | ||||
| -rw-r--r-- | src/test/ui/inference-variable-behind-raw-pointer.rs (renamed from src/test/compile-fail/issue-26194.rs) | 15 | ||||
| -rw-r--r-- | src/test/ui/inference-variable-behind-raw-pointer.stderr | 8 |
7 files changed, 182 insertions, 9 deletions
diff --git a/src/test/run-pass/arbitrary_self_types_raw_pointer_struct.rs b/src/test/run-pass/arbitrary_self_types_raw_pointer_struct.rs new file mode 100644 index 00000000000..8b6941e1c63 --- /dev/null +++ b/src/test/run-pass/arbitrary_self_types_raw_pointer_struct.rs @@ -0,0 +1,37 @@ +// Copyright 2015 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 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(arbitrary_self_types)] + +use std::rc::Rc; + +struct Foo(String); + +impl Foo { + unsafe fn foo(self: *const Self) -> *const str { + (*self).0.as_ref() + } + + fn complicated_1(self: *const Rc<Self>) -> &'static str { + "Foo::complicated_1" + } + + unsafe fn complicated_2(self: Rc<*const Self>) -> *const str { + (**self).0.as_ref() + } +} + +fn main() { + let foo = Foo("abc123".into()); + assert_eq!("abc123", unsafe { &*(&foo as *const Foo).foo() }); + assert_eq!("Foo::complicated_1", std::ptr::null::<Rc<Foo>>().complicated_1()); + let rc = Rc::new(&foo as *const Foo); + assert_eq!("abc123", unsafe { &*rc.complicated_2()}); +} diff --git a/src/test/run-pass/arbitrary_self_types_raw_pointer_trait.rs b/src/test/run-pass/arbitrary_self_types_raw_pointer_trait.rs new file mode 100644 index 00000000000..f70e38a14e4 --- /dev/null +++ b/src/test/run-pass/arbitrary_self_types_raw_pointer_trait.rs @@ -0,0 +1,70 @@ +// Copyright 2015 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 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(arbitrary_self_types)] + +use std::ptr; + +trait Foo { + fn foo(self: *const Self) -> &'static str; + + unsafe fn bar(self: *const Self) -> i64; + + unsafe fn complicated(self: *const *const Self) -> i64 where Self: Sized { + (*self).bar() + } +} + +impl Foo for i32 { + fn foo(self: *const Self) -> &'static str { + "I'm an i32!" + } + + unsafe fn bar(self: *const Self) -> i64 { + *self as i64 + } +} + +impl Foo for u32 { + fn foo(self: *const Self) -> &'static str { + "I'm a u32!" + } + + unsafe fn bar(self: *const Self) -> i64 { + *self as i64 + } +} + +fn main() { + let null_i32 = ptr::null::<i32>() as *const Foo; + let null_u32 = ptr::null::<u32>() as *const Foo; + + assert_eq!("I'm an i32!", null_i32.foo()); + assert_eq!("I'm a u32!", null_u32.foo()); + + let valid_i32 = 5i32; + let valid_i32_thin = &valid_i32 as *const i32; + assert_eq!("I'm an i32!", valid_i32_thin.foo()); + assert_eq!(5, unsafe { valid_i32_thin.bar() }); + assert_eq!(5, unsafe { (&valid_i32_thin as *const *const i32).complicated() }); + let valid_i32_fat = valid_i32_thin as *const Foo; + assert_eq!("I'm an i32!", valid_i32_fat.foo()); + assert_eq!(5, unsafe { valid_i32_fat.bar() }); + + let valid_u32 = 18u32; + let valid_u32_thin = &valid_u32 as *const u32; + assert_eq!("I'm a u32!", valid_u32_thin.foo()); + assert_eq!(18, unsafe { valid_u32_thin.bar() }); + assert_eq!(18, unsafe { (&valid_u32_thin as *const *const u32).complicated() }); + let valid_u32_fat = valid_u32_thin as *const Foo; + assert_eq!("I'm a u32!", valid_u32_fat.foo()); + assert_eq!(18, unsafe { valid_u32_fat.bar() }); + +} diff --git a/src/test/ui/arbitrary-self-types-not-object-safe.stderr b/src/test/ui/arbitrary-self-types-not-object-safe.stderr index a1090fe031e..f258488ee2f 100644 --- a/src/test/ui/arbitrary-self-types-not-object-safe.stderr +++ b/src/test/ui/arbitrary-self-types-not-object-safe.stderr @@ -4,7 +4,7 @@ error[E0038]: the trait `Foo` cannot be made into an object 40 | let x = Box::new(5usize) as Box<Foo>; | ^^^^^^^^ the trait `Foo` cannot be made into an object | - = note: method `foo` has a non-standard `self` type. Only `&self`, `&mut self`, and `Box<Self>` are currently supported for trait objects + = note: method `foo` has a non-standard `self` type error[E0038]: the trait `Foo` cannot be made into an object --> $DIR/arbitrary-self-types-not-object-safe.rs:40:13 @@ -12,7 +12,7 @@ error[E0038]: the trait `Foo` cannot be made into an object 40 | let x = Box::new(5usize) as Box<Foo>; | ^^^^^^^^^^^^^^^^ the trait `Foo` cannot be made into an object | - = note: method `foo` has a non-standard `self` type. Only `&self`, `&mut self`, and `Box<Self>` are currently supported for trait objects + = note: method `foo` has a non-standard `self` type = note: required because of the requirements on the impl of `std::ops::CoerceUnsized<std::boxed::Box<Foo>>` for `std::boxed::Box<usize>` error: aborting due to 2 previous errors diff --git a/src/test/ui/feature-gate-arbitrary_self_types-raw-pointer.rs b/src/test/ui/feature-gate-arbitrary_self_types-raw-pointer.rs new file mode 100644 index 00000000000..29e51727edc --- /dev/null +++ b/src/test/ui/feature-gate-arbitrary_self_types-raw-pointer.rs @@ -0,0 +1,28 @@ +// 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 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Foo; + +impl Foo { + fn foo(self: *const Self) {} + //~^ ERROR raw pointer `self` is unstable +} + +trait Bar { + fn bar(self: *const Self); + //~^ ERROR raw pointer `self` is unstable +} + +impl Bar for () { + fn bar(self: *const Self) {} + //~^ ERROR raw pointer `self` is unstable +} + +fn main() {} diff --git a/src/test/ui/feature-gate-arbitrary_self_types-raw-pointer.stderr b/src/test/ui/feature-gate-arbitrary_self_types-raw-pointer.stderr new file mode 100644 index 00000000000..d629ac4c60f --- /dev/null +++ b/src/test/ui/feature-gate-arbitrary_self_types-raw-pointer.stderr @@ -0,0 +1,29 @@ +error: raw pointer `self` is unstable (see issue #44874) + --> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:19:18 + | +19 | fn bar(self: *const Self); + | ^^^^^^^^^^^ + | + = help: add #![feature(arbitrary_self_types)] to the crate attributes to enable + = help: consider changing to `self`, `&self`, `&mut self`, or `self: Box<Self>` + +error: raw pointer `self` is unstable (see issue #44874) + --> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:14:18 + | +14 | fn foo(self: *const Self) {} + | ^^^^^^^^^^^ + | + = help: add #![feature(arbitrary_self_types)] to the crate attributes to enable + = help: consider changing to `self`, `&self`, `&mut self`, or `self: Box<Self>` + +error: raw pointer `self` is unstable (see issue #44874) + --> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:24:18 + | +24 | fn bar(self: *const Self) {} + | ^^^^^^^^^^^ + | + = help: add #![feature(arbitrary_self_types)] to the crate attributes to enable + = help: consider changing to `self`, `&self`, `&mut self`, or `self: Box<Self>` + +error: aborting due to 3 previous errors + diff --git a/src/test/compile-fail/issue-26194.rs b/src/test/ui/inference-variable-behind-raw-pointer.rs index 7ddd56229ce..0ae027679fe 100644 --- a/src/test/compile-fail/issue-26194.rs +++ b/src/test/ui/inference-variable-behind-raw-pointer.rs @@ -1,4 +1,4 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// 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. // @@ -8,11 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -struct S(String); +// must-compile-successfully -impl S { - fn f(self: *mut S) -> String { self.0 } - //~^ ERROR invalid `self` type -} +// tests that the following code compiles, but produces a future-compatibility warning -fn main() { S("".to_owned()).f(); } +fn main() { + let data = std::ptr::null(); + let _ = &data as *const *const (); + if data.is_null() {} +} diff --git a/src/test/ui/inference-variable-behind-raw-pointer.stderr b/src/test/ui/inference-variable-behind-raw-pointer.stderr new file mode 100644 index 00000000000..a8874c9bf5a --- /dev/null +++ b/src/test/ui/inference-variable-behind-raw-pointer.stderr @@ -0,0 +1,8 @@ +warning[E0619]: the type of this value must be known in this context + --> $DIR/inference-variable-behind-raw-pointer.rs:18:13 + | +18 | if data.is_null() {} + | ^^^^^^^ + | + = note: this will be made into a hard error in a future version of the compiler + |
