-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #78365 - lcnr:const-eval-obj-safety, r=oli-obk
check object safety of generic constants As `Self` can only be effectively used in constants with `const_evaluatable_checked` this should not matter outside of it. Implements the first item of #72219 > Object safety interactions with constants r? @oli-obk for now cc @nikomatsakis
- Loading branch information
Showing
10 changed files
with
244 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/test/ui/const-generics/const_evaluatable_checked/object-safety-err-ret.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![feature(const_generics, const_evaluatable_checked)] | ||
#![allow(incomplete_features)] | ||
|
||
|
||
const fn bar<T: ?Sized>() -> usize { 7 } | ||
|
||
trait Foo { | ||
fn test(&self) -> [u8; bar::<Self>()]; | ||
} | ||
|
||
impl Foo for () { | ||
fn test(&self) -> [u8; bar::<Self>()] { | ||
[0; bar::<Self>()] | ||
} | ||
} | ||
|
||
fn use_dyn(v: &dyn Foo) { //~ERROR the trait `Foo` cannot be made into an object | ||
v.test(); | ||
} | ||
|
||
fn main() {} |
18 changes: 18 additions & 0 deletions
18
src/test/ui/const-generics/const_evaluatable_checked/object-safety-err-ret.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error[E0038]: the trait `Foo` cannot be made into an object | ||
--> $DIR/object-safety-err-ret.rs:17:15 | ||
| | ||
LL | fn use_dyn(v: &dyn Foo) { | ||
| ^^^^^^^^ `Foo` cannot be made into an object | ||
| | ||
= help: consider moving `test` to another trait | ||
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> | ||
--> $DIR/object-safety-err-ret.rs:8:23 | ||
| | ||
LL | trait Foo { | ||
| --- this trait cannot be made into an object... | ||
LL | fn test(&self) -> [u8; bar::<Self>()]; | ||
| ^^^^^^^^^^^^^^^^^^^ ...because method `test` references the `Self` type in its return type | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0038`. |
22 changes: 22 additions & 0 deletions
22
src/test/ui/const-generics/const_evaluatable_checked/object-safety-err-where-bounds.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![feature(const_generics, const_evaluatable_checked)] | ||
#![allow(incomplete_features)] | ||
#![deny(where_clauses_object_safety)] | ||
|
||
|
||
const fn bar<T: ?Sized>() -> usize { 7 } | ||
|
||
trait Foo { | ||
fn test(&self) where [u8; bar::<Self>()]: Sized; | ||
//~^ ERROR the trait `Foo` cannot be made into an object | ||
//~| WARN this was previously accepted by the compiler but is being phased out | ||
} | ||
|
||
impl Foo for () { | ||
fn test(&self) where [u8; bar::<Self>()]: Sized {} | ||
} | ||
|
||
fn use_dyn(v: &dyn Foo) { | ||
v.test(); | ||
} | ||
|
||
fn main() {} |
24 changes: 24 additions & 0 deletions
24
src/test/ui/const-generics/const_evaluatable_checked/object-safety-err-where-bounds.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error: the trait `Foo` cannot be made into an object | ||
--> $DIR/object-safety-err-where-bounds.rs:9:8 | ||
| | ||
LL | fn test(&self) where [u8; bar::<Self>()]: Sized; | ||
| ^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/object-safety-err-where-bounds.rs:3:9 | ||
| | ||
LL | #![deny(where_clauses_object_safety)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #51443 <https://github.com/rust-lang/rust/issues/51443> | ||
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> | ||
--> $DIR/object-safety-err-where-bounds.rs:9:8 | ||
| | ||
LL | trait Foo { | ||
| --- this trait cannot be made into an object... | ||
LL | fn test(&self) where [u8; bar::<Self>()]: Sized; | ||
| ^^^^ ...because method `test` references the `Self` type in its `where` clause | ||
= help: consider moving `test` to another trait | ||
|
||
error: aborting due to previous error | ||
|
22 changes: 22 additions & 0 deletions
22
src/test/ui/const-generics/const_evaluatable_checked/object-safety-ok-infer-err.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![feature(const_generics, const_evaluatable_checked)] | ||
#![allow(incomplete_features)] | ||
|
||
trait Foo<const N: usize> { | ||
fn test(&self) -> [u8; N + 1]; | ||
} | ||
|
||
impl<const N: usize> Foo<N> for () { | ||
fn test(&self) -> [u8; N + 1] { | ||
[0; N + 1] | ||
} | ||
} | ||
|
||
fn use_dyn<const N: usize>(v: &dyn Foo<N>) where [u8; N + 1]: Sized { | ||
assert_eq!(v.test(), [0; N + 1]); | ||
} | ||
|
||
fn main() { | ||
// FIXME(const_evaluatable_checked): Improve the error message here. | ||
use_dyn(&()); | ||
//~^ ERROR type annotations needed | ||
} |
Oops, something went wrong.