]> git.lizzy.rs Git - rust.git/blob - tests/ui/traits/track-obligations.rs
Auto merge of #106696 - kylematsuda:early-binder, r=lcnr
[rust.git] / tests / ui / traits / track-obligations.rs
1 // These are simplifications of the tower traits by the same name:
2
3 pub trait Service<Request> {
4     type Response;
5 }
6
7 pub trait Layer<C> {
8     type Service;
9 }
10
11 // Any type will do here:
12
13 pub struct Req;
14 pub struct Res;
15
16 // This is encoding a trait alias.
17
18 pub trait ParticularService:
19     Service<Req, Response = Res> {
20 }
21
22 impl<T> ParticularService for T
23 where
24     T: Service<Req, Response = Res>,
25 {
26 }
27
28 // This is also a trait alias.
29 // The weird = <Self as ...> bound is there so that users of the trait do not
30 // need to repeat the bounds. See https://github.com/rust-lang/rust/issues/20671
31 // for context, and in particular the workaround in:
32 // https://github.com/rust-lang/rust/issues/20671#issuecomment-529752828
33
34 pub trait ParticularServiceLayer<C>:
35     Layer<C, Service = <Self as ParticularServiceLayer<C>>::Service>
36 {
37     type Service: ParticularService;
38 }
39
40 impl<T, C> ParticularServiceLayer<C> for T
41 where
42     T: Layer<C>,
43     T::Service: ParticularService,
44 {
45     type Service = T::Service;
46 }
47
48 // These are types that implement the traits that the trait aliases refer to.
49 // They should also implement the alias traits due to the blanket impls.
50
51 struct ALayer<C>(C);
52 impl<C> Layer<C> for ALayer<C> {
53     type Service = AService;
54 }
55
56 struct AService;
57 impl Service<Req> for AService {
58     // However, AService does _not_ meet the blanket implementation,
59     // since its Response type is bool, not Res as it should be.
60     type Response = bool;
61 }
62
63 // This is a wrapper type around ALayer that uses the trait alias
64 // as a way to communicate the requirements of the provided types.
65 struct Client<C>(C);
66
67 // The method and the free-standing function below both have the same bounds.
68
69 impl<C> Client<C>
70 where
71     ALayer<C>: ParticularServiceLayer<C>,
72 {
73     fn check(&self) {}
74 }
75
76 fn check<C>(_: C) where ALayer<C>: ParticularServiceLayer<C> {}
77
78 // But, they give very different error messages.
79
80 fn main() {
81     // This gives a very poor error message that does nothing to point the user
82     // at the underlying cause of why the types involved do not meet the bounds.
83     Client(()).check(); //~ ERROR E0599
84
85     // This gives a good(ish) error message that points the user at _why_ the
86     // bound isn't met, and thus how they might fix it.
87     check(()); //~ ERROR E0271
88 }