File: defaults-cyclic-pass-1.rs

package info (click to toggle)
rustc 1.88.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 934,128 kB
  • sloc: xml: 158,127; python: 36,062; javascript: 19,855; sh: 19,700; cpp: 18,947; ansic: 12,993; asm: 4,792; makefile: 690; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (56 lines) | stat: -rw-r--r-- 1,219 bytes parent folder | download | duplicates (15)
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
//@ check-pass

#![feature(associated_type_defaults)]

// Having a cycle in assoc. type defaults is okay, as long as there's no impl
// that retains it.
trait Tr {
    type A = Self::B;
    type B = Self::A;

    fn f();
}

// An impl has to break the cycle to be accepted.
impl Tr for u8 {
    type A = u8;

    fn f() {
        // Check that the type propagates as expected (seen from inside the impl)
        let _: Self::A = 0u8;
        let _: Self::B = 0u8;
    }
}

impl Tr for String {
    type B = ();

    fn f() {
        // Check that the type propagates as expected (seen from inside the impl)
        let _: Self::A = ();
        let _: Self::B = ();
    }
}

impl Tr for () {
    type A = Vec<()>;
    type B = u8;

    fn f() {
        // Check that the type propagates as expected (seen from inside the impl)
        let _: Self::A = Vec::<()>::new();
        let _: Self::B = 0u8;
    }
}

fn main() {
    // Check that both impls now have the right types (seen from outside the impls)
    let _: <u8 as Tr>::A = 0u8;
    let _: <u8 as Tr>::B = 0u8;

    let _: <String as Tr>::A = ();
    let _: <String as Tr>::B = ();

    let _: <() as Tr>::A = Vec::<()>::new();
    let _: <() as Tr>::B = 0u8;
}