File: with-self-in-projection-output-repeated-supertrait.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 893,176 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,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (51 lines) | stat: -rw-r--r-- 1,489 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
//@ build-pass (FIXME(62277): could be check-pass?)

// FIXME(eddyb) shorten the name so windows doesn't choke on it.
#![crate_name = "trait_test"]

// Regression test related to #56288. Check that a supertrait projection (of
// `Output`) that references `Self` is ok if there is another occurrence of
// the same supertrait that specifies the projection explicitly, even if
// the projection's associated type is not explicitly specified in the object type.
//
// Note that in order for this to compile, we need the `Self`-referencing projection
// to normalize fairly directly to a concrete type, otherwise the trait resolver
// will hate us.
//
// There is a test in `trait-object-with-self-in-projection-output-bad.rs` that
// having a normalizing, but `Self`-containing projection does not *by itself*
// allow you to avoid writing the projected type (`Output`, in this example)
// explicitly.

trait ConstI32 {
    type Out;
}

impl<T: ?Sized> ConstI32 for T {
    type Out = i32;
}

trait Base {
    type Output;
}

trait NormalizingHelper: Base<Output=<Self as ConstI32>::Out> + Base<Output=i32> {
    type Target;
}

impl Base for u32
{
    type Output = i32;
}

impl NormalizingHelper for u32
{
    type Target = i32;
}

fn main() {
    // Make sure this works both with and without the associated type
    // being specified.
    let _x: Box<dyn NormalizingHelper<Target=i32>> = Box::new(2u32);
    let _y: Box<dyn NormalizingHelper<Target=i32, Output=i32>> = Box::new(2u32);
}