File: copy-check-deferred-before-fallback.rs

package info (click to toggle)
rustc 1.87.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 925,564 kB
  • sloc: xml: 158,127; python: 36,039; javascript: 19,761; sh: 19,737; cpp: 18,981; ansic: 13,133; asm: 4,376; makefile: 710; perl: 29; lisp: 28; ruby: 19; sql: 11
file content (59 lines) | stat: -rw-r--r-- 1,673 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//@ check-pass

#![feature(generic_arg_infer)]

// Test that if we defer repeat expr copy checks to end of typechecking they're
// checked before integer fallback occurs. We accomplish this by contriving a
// situation where we have a goal that can be proven either via another repeat expr
// check or by integer fallback. In the integer fallback case an array length would
// be inferred to `2` requiring `NotCopy: Copy`, and in the repeat expr case it would
// be inferred to `1`.

use std::marker::PhantomData;

struct NotCopy;

struct Foo<T>(PhantomData<T>);

impl Clone for Foo<u32> {
    fn clone(&self) -> Self {
        Foo(PhantomData)
    }
}

impl Copy for Foo<u32> {}

fn tie<T>(_: &T, _: [Foo<T>; 2]) {}

trait Trait<const N: usize> {}

impl Trait<2> for i32 {}
impl Trait<1> for u32 {}

fn make_goal<T: Trait<N>, const N: usize>(_: &T, _: [NotCopy; N]) {}

fn main() {
    let a = 1;
    let b: [Foo<_>; 2] = [Foo(PhantomData); _];
    tie(&a, b);
    let c = [NotCopy; _];

    // a is of type `?y`
    // b is of type `[Foo<?y>; 2]`
    // c is of type `[NotCopy; ?x]`
    // there is a goal ?y: Trait<?x>` with two candidates:
    // - `i32: Trait<2>`, ?y=i32 ?x=2 which requires `NotCopy: Copy` when expr checks happen
    // - `u32: Trait<1>` ?y=u32 ?x=1 which doesnt require `NotCopy: Copy`
    make_goal(&a, c);

    // final repeat expr checks:
    //
    // `Foo<?y>; 2`
    // - Foo<?y>: Copy
    // - requires ?y=u32
    //
    // `NotCopy; ?x`
    // - fails if fallback happens before repeat exprs as `i32: Trait<?x>` infers `?x=2`
    // - succeeds if repeat expr checks happen first as `?y=u32` means `u32: Trait<?x>`
    //    infers `?x=1`
}