Skip to content

Commit 2df072c

Browse files
committed
Tests
1 parent 7df7c3c commit 2df072c

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![feature(generic_arg_infer)]
2+
3+
// Test that would start passing if we defer repeat expr copy checks to end of
4+
// typechecking and they're checked after integer fallback occurs. We accomplish
5+
// this by contriving a situation where integer fallback allows progress to be
6+
// made on a trait goal that infers the length of a repeat expr.
7+
8+
use std::marker::PhantomData;
9+
10+
struct NotCopy;
11+
12+
trait Trait<const N: usize> {}
13+
14+
impl Trait<2> for u32 {}
15+
impl Trait<1> for i32 {}
16+
17+
fn make_goal<T: Trait<N>, const N: usize>(_: &T, _: [NotCopy; N]) {}
18+
19+
fn main() {
20+
let a = 1;
21+
let b = [NotCopy; _];
22+
//~^ ERROR: type annotations needed
23+
24+
// a is of type `?y`
25+
// b is of type `[NotCopy; ?x]`
26+
// there is a goal ?y: Trait<?x>` with two candidates:
27+
// - `i32: Trait<1>`, ?y=i32 ?x=1 which doesnt require `NotCopy: Copy`
28+
// - `u32: Trait<2>` ?y=u32 ?x=2 which requires `NotCopy: Copy`
29+
make_goal(&a, b);
30+
31+
// final repeat expr checks:
32+
//
33+
// `NotCopy; ?x`
34+
// - succeeds if fallback happens before repeat exprs as `i32: Trait<?x>` infers `?x=1`
35+
// - fails if repeat expr checks happen first as `?x` is unconstrained so cannot be structurally resolved
36+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed for `[NotCopy; _]`
2+
--> $DIR/copy-check-deferred-after-fallback.rs:21:9
3+
|
4+
LL | let b = [NotCopy; _];
5+
| ^ ------- type must be known at this point
6+
|
7+
help: consider giving `b` an explicit type, where the value of const parameter `N` is specified
8+
|
9+
LL | let b: [_; N] = [NotCopy; _];
10+
| ++++++++
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//@ check-pass
2+
3+
#![feature(generic_arg_infer)]
4+
5+
// Test that if we defer repeat expr copy checks to end of typechecking they're
6+
// checked before integer fallback occurs. We accomplish this by contriving a
7+
// situation where we have a goal that can be proven either via another repeat expr
8+
// check or by integer fallback. In the integer fallback case an array length would
9+
// be inferred to `2` requiring `NotCopy: Copy`, and in the repeat expr case it would
10+
// be inferred to `1`.
11+
12+
use std::marker::PhantomData;
13+
14+
struct NotCopy;
15+
16+
struct Foo<T>(PhantomData<T>);
17+
18+
impl Clone for Foo<u32> {
19+
fn clone(&self) -> Self {
20+
Foo(PhantomData)
21+
}
22+
}
23+
24+
impl Copy for Foo<u32> {}
25+
26+
fn tie<T>(_: &T, _: [Foo<T>; 2]) {}
27+
28+
trait Trait<const N: usize> {}
29+
30+
impl Trait<2> for i32 {}
31+
impl Trait<1> for u32 {}
32+
33+
fn make_goal<T: Trait<N>, const N: usize>(_: &T, _: [NotCopy; N]) {}
34+
35+
fn main() {
36+
let a = 1;
37+
let b: [Foo<_>; 2] = [Foo(PhantomData); _];
38+
tie(&a, b);
39+
let c = [NotCopy; _];
40+
41+
// a is of type `?y`
42+
// b is of type `[Foo<?y>; 2]`
43+
// c is of type `[NotCopy; ?x]`
44+
// there is a goal ?y: Trait<?x>` with two candidates:
45+
// - `i32: Trait<2>`, ?y=i32 ?x=2 which requires `NotCopy: Copy` when expr checks happen
46+
// - `u32: Trait<1>` ?y=u32 ?x=1 which doesnt require `NotCopy: Copy`
47+
make_goal(&a, c);
48+
49+
// final repeat expr checks:
50+
//
51+
// `Foo<?y>; 2`
52+
// - Foo<?y>: Copy
53+
// - requires ?y=u32
54+
//
55+
// `NotCopy; ?x`
56+
// - fails if fallback happens before repeat exprs as `i32: Trait<?x>` infers `?x=2`
57+
// - succeeds if repeat expr checks happen first as `?y=u32` means `u32: Trait<?x>` infers `?x=1`
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(generic_arg_infer)]
2+
3+
struct Foo<const N: usize>;
4+
5+
impl Clone for Foo<1> {
6+
fn clone(&self) -> Self {
7+
Foo
8+
}
9+
}
10+
impl Copy for Foo<1> {}
11+
12+
fn unify<const N: usize>(_: &[Foo<N>; N]) {
13+
loop {}
14+
}
15+
16+
fn main() {
17+
let x = &[Foo::<_>; _];
18+
//~^ ERROR: type annotations needed for `&[Foo<_>; _]`
19+
_ = unify(x);
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed for `&[Foo<_>; _]`
2+
--> $DIR/no-conservative-copy-impl-requirement.rs:17:9
3+
|
4+
LL | let x = &[Foo::<_>; _];
5+
| ^ -------- type must be known at this point
6+
|
7+
help: consider giving `x` an explicit type, where the value of const parameter `N` is specified
8+
|
9+
LL | let x: &[Foo<N>; N] = &[Foo::<_>; _];
10+
| ++++++++++++++
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)