]> git.lizzy.rs Git - rust.git/blob - tests/ui/generics/issue-61631-default-type-param-cannot-reference-self.rs
Rollup merge of #106661 - mjguzik:linux_statx, r=Mark-Simulacrum
[rust.git] / tests / ui / generics / issue-61631-default-type-param-cannot-reference-self.rs
1 #![crate_type="lib"]
2
3 // rust-lang/rust#61631: Uses of `Self` in the defaults of generic
4 // types for ADT's are not allowed. We justify this because the `Self`
5 // type could be considered the "final" type parameter, that is only
6 // well-defined after all of the other type parameters on the ADT have
7 // been instantiated.
8 //
9 // These were previously were ICE'ing at the usage point anyway (see
10 // `demo_usages` below), so there should not be any backwards
11 // compatibility concern.
12
13 struct Snobound<'a, P = Self> { x: Option<&'a P> }
14 //~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]
15
16 enum Enobound<'a, P = Self> { A, B(Option<&'a P>) }
17 //~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]
18
19 union Unobound<'a, P = Self> { x: i32, y: Option<&'a P> }
20 //~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]
21
22 // Disallowing `Self` in defaults sidesteps need to check the bounds
23 // on the defaults in cases like these.
24
25 struct Ssized<'a, P: Sized = [Self]> { x: Option<&'a P> }
26 //~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]
27
28 enum Esized<'a, P: Sized = [Self]> { A, B(Option<&'a P>) }
29 //~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]
30
31 union Usized<'a, P: Sized = [Self]> { x: i32, y: Option<&'a P> }
32 //~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]
33
34 fn demo_usages() {
35     // An ICE means you only get the error from the first line of the
36     // demo; comment each out to observe the other ICEs when trying
37     // this out on older versions of Rust.
38
39     let _ice: Snobound;
40     let _ice: Enobound;
41     let _ice: Unobound;
42     let _ice: Ssized;
43     let _ice: Esized;
44     let _ice: Usized;
45 }