Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Sep 21, 2020
1 parent 073127a commit 2855b92
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/test/ui/const-generics/issues/issue-69654-run-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// run-pass
#![feature(const_generics)]
#![allow(incomplete_features, unused_braces)]

trait Bar<T> {}
impl<T> Bar<T> for [u8; {7}] {}

struct Foo<const N: usize> {}
impl<const N: usize> Foo<N>
where
[u8; N]: Bar<[(); N]>,
{
fn foo() {}
}

fn main() {
Foo::foo();
}
18 changes: 18 additions & 0 deletions src/test/ui/const-generics/issues/issue-69654.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(const_generics)]
#![allow(incomplete_features)]

trait Bar<T> {}
impl<T> Bar<T> for [u8; T] {}
//~^ ERROR expected value, found type parameter `T`

struct Foo<const N: usize> {}
impl<const N: usize> Foo<N>
where
[u8; N]: Bar<[(); N]>,
{
fn foo() {}
}

fn main() {
Foo::foo();
}
9 changes: 9 additions & 0 deletions src/test/ui/const-generics/issues/issue-69654.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0423]: expected value, found type parameter `T`
--> $DIR/issue-69654.rs:5:25
|
LL | impl<T> Bar<T> for [u8; T] {}
| ^ not a value

error: aborting due to previous error

For more information about this error, try `rustc --explain E0423`.
17 changes: 17 additions & 0 deletions src/test/ui/const-generics/occurs-check/bind-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// build-pass
#![feature(const_generics)]
#![allow(incomplete_features)]

// This test does not use any "unevaluated" consts, so it should compile just fine.

fn bind<const N: usize>(value: [u8; N]) -> [u8; N] {
todo!()
}

fn sink(_: [u8; 5]) {}

fn main() {
let mut arr = Default::default();
arr = bind(arr);
sink(arr);
}
18 changes: 18 additions & 0 deletions src/test/ui/const-generics/occurs-check/unify-fixpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(const_generics)] //~ WARN the feature `const_generics` is incomplete

// It depends on how we normalize constants and how const equate works if this
// compiles.
//
// Please ping @lcnr if the output if this test changes.


fn bind<const N: usize>(value: [u8; N + 2]) -> [u8; N * 2] {
//~^ ERROR constant expression depends on a generic parameter
//~| ERROR constant expression depends on a generic parameter
todo!()
}

fn main() {
let mut arr = Default::default();
arr = bind::<2>(arr);
}
27 changes: 27 additions & 0 deletions src/test/ui/const-generics/occurs-check/unify-fixpoint.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/unify-fixpoint.rs:1:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information

error: constant expression depends on a generic parameter
--> $DIR/unify-fixpoint.rs:9:32
|
LL | fn bind<const N: usize>(value: [u8; N + 2]) -> [u8; N * 2] {
| ^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes

error: constant expression depends on a generic parameter
--> $DIR/unify-fixpoint.rs:9:48
|
LL | fn bind<const N: usize>(value: [u8; N + 2]) -> [u8; N * 2] {
| ^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes

error: aborting due to 2 previous errors; 1 warning emitted

17 changes: 17 additions & 0 deletions src/test/ui/const-generics/occurs-check/unify-n-nplusone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(const_generics)]
#![allow(incomplete_features)]

// This test would try to unify `N` with `N + 1` which must fail the occurs check.

fn bind<const N: usize>(value: [u8; N]) -> [u8; N + 1] {
//~^ ERROR constant expression depends on a generic parameter
todo!()
}

fn sink(_: [u8; 5]) {}

fn main() {
let mut arr = Default::default();
arr = bind(arr);
sink(arr);
}
10 changes: 10 additions & 0 deletions src/test/ui/const-generics/occurs-check/unify-n-nplusone.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: constant expression depends on a generic parameter
--> $DIR/unify-n-nplusone.rs:6:44
|
LL | fn bind<const N: usize>(value: [u8; N]) -> [u8; N + 1] {
| ^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes

error: aborting due to previous error

14 changes: 14 additions & 0 deletions src/test/ui/const-generics/occurs-check/unused-substs-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// build-pass
#![feature(const_generics)]
#![allow(incomplete_features)]

trait Bar<const M: usize> {}
impl<const N: usize> Bar<N> for A<{ 6 + 1 }> {}

struct A<const N: usize>
where
A<N>: Bar<N>;

fn main() {
let _ = A;
}
27 changes: 27 additions & 0 deletions src/test/ui/const-generics/occurs-check/unused-substs-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// check-pass
#![feature(const_generics)]
#![allow(incomplete_features)]

// The goal is is to get an unevaluated const `ct` with a `Ty::Infer(TyVar(_#1t)` subst.
//
// If we are then able to infer `ty::Infer(TyVar(_#1t) := Ty<ct>` we introduced an
// artificial inference cycle.
struct Foo<const N: usize>;

trait Bind<T> {
fn bind() -> (T, Self);
}

// `N` has to be `ConstKind::Unevaluated`.
impl<T> Bind<T> for Foo<{ 6 + 1 }> {
fn bind() -> (T, Self) {
(panic!(), Foo)
}
}

fn main() {
let (mut t, foo) = Foo::bind();
// `t` is `ty::Infer(TyVar(_#1t))`
// `foo` contains `ty::Infer(TyVar(_#1t))` in its substs
t = foo;
}
18 changes: 18 additions & 0 deletions src/test/ui/const-generics/occurs-check/unused-substs-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// check-pass
#![feature(const_generics)]
#![allow(incomplete_features)]

// The goal is is to get an unevaluated const `ct` with a `Ty::Infer(TyVar(_#1t)` subst.
//
// If we are then able to infer `ty::Infer(TyVar(_#1t) := Ty<ct>` we introduced an
// artificial inference cycle.
fn bind<T>() -> (T, [u8; 6 + 1]) {
todo!()
}

fn main() {
let (mut t, foo) = bind();
// `t` is `ty::Infer(TyVar(_#1t))`
// `foo` contains `ty::Infer(TyVar(_#1t))` in its substs
t = foo;
}
12 changes: 12 additions & 0 deletions src/test/ui/const-generics/occurs-check/unused-substs-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// build-pass
#![feature(const_generics)]
#![allow(incomplete_features)]

fn bind<const N: usize>(value: [u8; N]) -> [u8; 3 + 4] {
todo!()
}

fn main() {
let mut arr = Default::default();
arr = bind(arr);
}

0 comments on commit 2855b92

Please sign in to comment.