File: associated-item-long-paths.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 (47 lines) | stat: -rw-r--r-- 934 bytes parent folder | download | duplicates (7)
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
//@ run-pass

use std::mem::size_of;

// The main point of this test is to ensure that we can parse and resolve
// associated items on associated types.

trait Foo {
    type U;
}

trait Bar {
    // Note 1: Chains of associated items in a path won't type-check.
    // Note 2: Associated consts can't depend on type parameters or `Self`,
    // which are the only types that an associated type can be referenced on for
    // now, so we can only test methods.
    fn method() -> u32;
    fn generic_method<T>() -> usize;
}

struct MyFoo;
struct MyBar;

impl Foo for MyFoo {
    type U = MyBar;
}

impl Bar for MyBar {
    fn method() -> u32 {
        2u32
    }
    fn generic_method<T>() -> usize {
        size_of::<T>()
    }
}

fn foo<T>()
    where T: Foo,
          T::U: Bar,
{
    assert_eq!(2u32, <T as Foo>::U::method());
    assert_eq!(8usize, <T as Foo>::U::generic_method::<f64>());
}

fn main() {
    foo::<MyFoo>();
}