File: defaults-in-other-trait-items.rs

package info (click to toggle)
rustc-web 1.85.0%2Bdfsg3-1~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,759,988 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,056; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (61 lines) | stat: -rw-r--r-- 1,698 bytes parent folder | download | duplicates (10)
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
60
61
#![feature(associated_type_defaults)]

// Associated type defaults may not be assumed inside the trait defining them.
// ie. they only resolve to `<Self as Tr>::A`, not the actual type `()`
trait Tr {
    type A = (); //~ NOTE associated type defaults can't be assumed inside the trait defining them

    fn f(p: Self::A) {
        let () = p;
        //~^ ERROR mismatched types
        //~| NOTE expected associated type, found `()`
        //~| NOTE expected associated type `<Self as Tr>::A`
        //~| NOTE this expression has type `<Self as Tr>::A`
    }
}

// An impl that doesn't override the type *can* assume the default.
impl Tr for () {
    fn f(p: Self::A) {
        let () = p;
    }
}

impl Tr for u8 {
    type A = ();

    fn f(p: Self::A) {
        let () = p;
    }
}

trait AssocConst {
    type Ty = u8; //~ NOTE associated type defaults can't be assumed inside the trait defining them

    // Assoc. consts also cannot assume that default types hold
    const C: Self::Ty = 0u8;
    //~^ ERROR mismatched types
    //~| NOTE expected associated type, found `u8`
    //~| NOTE expected associated type `<Self as AssocConst>::Ty`
}

// An impl can, however
impl AssocConst for () {
    const C: Self::Ty = 0u8;
}

pub trait Trait {
    type Res = isize; //~ NOTE associated type defaults can't be assumed inside the trait defining them

    fn infer_me_correctly() -> Self::Res {
        //~^ NOTE expected `<Self as Trait>::Res` because of return type

        // {integer} == isize
        2
        //~^ ERROR mismatched types
        //~| NOTE expected associated type, found integer
        //~| NOTE expected associated type `<Self as Trait>::Res`
    }
}

fn main() {}