Skip to content

Commit

Permalink
Test all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 12, 2024
1 parent e673a9e commit 5592c40
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/ui/impl-trait/in-ctfe/array-len-size-of.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Check that const eval can use the size of opaque types.
// check-pass
use std::mem;
fn returns_opaque() -> impl Sized {
0u8
}

struct NamedOpaqueType {
data: [mem::MaybeUninit<u8>; size_of_fut(returns_opaque)],
}

const fn size_of_fut<FUT>(x: fn() -> FUT) -> usize {
mem::size_of::<FUT>()
}

fn main() {}
22 changes: 22 additions & 0 deletions tests/ui/impl-trait/in-ctfe/array-len.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! Check that array lengths can observe associated types of opaque types
// check-pass
trait MyTrait: Copy {
const ASSOC: usize;
}

impl MyTrait for u8 {
const ASSOC: usize = 32;
}

const fn yeet() -> impl MyTrait {
0u8
}

const fn output<T: MyTrait>(_: T) -> usize {
<T as MyTrait>::ASSOC
}

fn main() {
let x = [0u8; output(yeet())];
println!("{:?}", x);
}
26 changes: 26 additions & 0 deletions tests/ui/impl-trait/in-ctfe/enum-discr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! check that const eval can observe associated types of opaque types.
// check-pass
trait MyTrait: Copy {
const ASSOC: usize;
}

impl MyTrait for u8 {
const ASSOC: usize = 32;
}

const fn yeet() -> impl MyTrait {
0u8
}

const fn output<T: MyTrait>(_: T) -> usize {
<T as MyTrait>::ASSOC
}

#[repr(usize)]
enum Foo {
Bar = output(yeet()),
}

fn main() {
println!("{}", Foo::Bar as usize);
}
24 changes: 24 additions & 0 deletions tests/ui/impl-trait/in-ctfe/match-arm-exhaustive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Check that pattern matching can observe the hidden type of opaque types.
// check-pass
trait MyTrait: Copy {
const ASSOC: u8;
}

impl MyTrait for () {
const ASSOC: u8 = 0;
}

const fn yeet() -> impl MyTrait {}

const fn output<T: MyTrait>(_: T) -> u8 {
<T as MyTrait>::ASSOC
}

const CT: u8 = output(yeet());

fn main() {
match 0 {
CT => (),
1.. => (),
}
}
14 changes: 14 additions & 0 deletions tests/ui/impl-trait/transmute/in-defining-scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This causes a query cycle due to using `Reveal::All`,
// in #119821 const eval was changed to always use `Reveal::All`
//
// See that PR for more details.
use std::mem::transmute;
fn foo() -> impl Sized {
//~^ ERROR cycle detected when computing type of
unsafe {
transmute::<_, u8>(foo());
}
0u8
}

fn main() {}
29 changes: 29 additions & 0 deletions tests/ui/impl-trait/transmute/in-defining-scope.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0391]: cycle detected when computing type of `foo::{opaque#0}`
--> $DIR/in-defining-scope.rs:6:13
|
LL | fn foo() -> impl Sized {
| ^^^^^^^^^^
|
note: ...which requires computing type of opaque `foo::{opaque#0}`...
--> $DIR/in-defining-scope.rs:6:13
|
LL | fn foo() -> impl Sized {
| ^^^^^^^^^^
note: ...which requires type-checking `foo`...
--> $DIR/in-defining-scope.rs:6:1
|
LL | fn foo() -> impl Sized {
| ^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `foo::{opaque#0}`...
= note: ...which requires normalizing `foo::{opaque#0}`...
= note: ...which again requires computing type of `foo::{opaque#0}`, completing the cycle
note: cycle used when checking that `foo::{opaque#0}` is well-formed
--> $DIR/in-defining-scope.rs:6:13
|
LL | fn foo() -> impl Sized {
| ^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0391`.
12 changes: 12 additions & 0 deletions tests/ui/impl-trait/transmute/outside-of-defining-scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Check that typeck can observe the size of an opaque type.
// check-pass
use std::mem::transmute;
fn foo() -> impl Sized {
0u8
}

fn main() {
unsafe {
transmute::<_, u8>(foo());
}
}
18 changes: 18 additions & 0 deletions tests/ui/specialization/ctfe/default-assoc-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Regression test for revealing associated types through specialization during const eval.
// check-pass
#![feature(specialization)]
//~^ WARNING the feature `specialization` is incomplete and may not be safe to use

trait Foo {
const ASSOC: usize;
}

impl Foo for u32 {
default const ASSOC: usize = 0;
}

fn foo() -> [u8; 0] {
[0; <u32 as Foo>::ASSOC]
}

fn main() {}
12 changes: 12 additions & 0 deletions tests/ui/specialization/ctfe/default-assoc-const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/default-assoc-const.rs:6:12
|
LL | #![feature(specialization)]
| ^^^^^^^^^^^^^^
|
= note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
= help: consider using `min_specialization` instead, which is more stable and complete
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

27 changes: 27 additions & 0 deletions tests/ui/specialization/ctfe/default-assoc-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Regression test showing that we can access assoicated types during const eval,
//! even if they rely on specialization.
// check-pass
#![feature(specialization)]
//~^ WARNING the feature `specialization` is incomplete and may not be safe to use

trait Foo {
type Assoc: Trait;
}

impl<T> Foo for Vec<T> {
default type Assoc = u32;
}

trait Trait {
const ASSOC: usize;
}

impl Trait for u32 {
const ASSOC: usize = 0;
}

fn foo() -> [u8; 0] {
[0; <<Vec<u32> as Foo>::Assoc as Trait>::ASSOC]
}

fn main() {}
12 changes: 12 additions & 0 deletions tests/ui/specialization/ctfe/default-assoc-type.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/default-assoc-type.rs:6:12
|
LL | #![feature(specialization)]
| ^^^^^^^^^^^^^^
|
= note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
= help: consider using `min_specialization` instead, which is more stable and complete
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

16 changes: 16 additions & 0 deletions tests/ui/type-alias-impl-trait/in-where-clause.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! We evaluate `1 + 2` with `Reveal::All` during typeck, causing
//! us to to get the concrete type of `Bar` while computing it.
//! This again requires type checking `foo`.
#![feature(type_alias_impl_trait)]
type Bar = impl Sized;
//~^ ERROR: cycle
//~| ERROR: cycle

fn foo() -> Bar
where
Bar: Send,
{
[0; 1 + 2]
}

fn main() {}
50 changes: 50 additions & 0 deletions tests/ui/type-alias-impl-trait/in-where-clause.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
error[E0391]: cycle detected when computing type of `Bar::{opaque#0}`
--> $DIR/in-where-clause.rs:5:12
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
|
note: ...which requires computing type of opaque `Bar::{opaque#0}`...
--> $DIR/in-where-clause.rs:5:12
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
note: ...which requires type-checking `foo`...
--> $DIR/in-where-clause.rs:9:1
|
LL | / fn foo() -> Bar
LL | | where
LL | | Bar: Send,
| |______________^
= note: ...which requires revealing opaque types in `[Binder { value: TraitPredicate(<Bar as core::marker::Send>, polarity:Positive), bound_vars: [] }]`...
= note: ...which again requires computing type of `Bar::{opaque#0}`, completing the cycle
note: cycle used when checking that `Bar::{opaque#0}` is well-formed
--> $DIR/in-where-clause.rs:5:12
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error[E0391]: cycle detected when computing type of opaque `Bar::{opaque#0}`
--> $DIR/in-where-clause.rs:5:12
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
|
note: ...which requires type-checking `foo`...
--> $DIR/in-where-clause.rs:13:9
|
LL | [0; 1 + 2]
| ^^^^^
= note: ...which requires evaluating trait selection obligation `Bar: core::marker::Send`...
= note: ...which again requires computing type of opaque `Bar::{opaque#0}`, completing the cycle
note: cycle used when computing type of `Bar::{opaque#0}`
--> $DIR/in-where-clause.rs:5:12
|
LL | type Bar = impl Sized;
| ^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0391`.

0 comments on commit 5592c40

Please sign in to comment.