File: issue-61631-default-type-param-cannot-reference-self.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,245,360 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (45 lines) | stat: -rw-r--r-- 1,727 bytes parent folder | download | duplicates (12)
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
#![crate_type="lib"]

// rust-lang/rust#61631: Uses of `Self` in the defaults of generic
// types for ADT's are not allowed. We justify this because the `Self`
// type could be considered the "final" type parameter, that is only
// well-defined after all of the other type parameters on the ADT have
// been instantiated.
//
// These were previously were ICE'ing at the usage point anyway (see
// `demo_usages` below), so there should not be any backwards
// compatibility concern.

struct Snobound<'a, P = Self> { x: Option<&'a P> }
//~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]

enum Enobound<'a, P = Self> { A, B(Option<&'a P>) }
//~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]

union Unobound<'a, P = Self> { x: i32, y: Option<&'a P> }
//~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]

// Disallowing `Self` in defaults sidesteps need to check the bounds
// on the defaults in cases like these.

struct Ssized<'a, P: Sized = [Self]> { x: Option<&'a P> }
//~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]

enum Esized<'a, P: Sized = [Self]> { A, B(Option<&'a P>) }
//~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]

union Usized<'a, P: Sized = [Self]> { x: i32, y: Option<&'a P> }
//~^ ERROR generic parameters cannot use `Self` in their defaults [E0735]

fn demo_usages() {
    // An ICE means you only get the error from the first line of the
    // demo; comment each out to observe the other ICEs when trying
    // this out on older versions of Rust.

    let _ice: Snobound;
    let _ice: Enobound;
    let _ice: Unobound;
    let _ice: Ssized;
    let _ice: Esized;
    let _ice: Usized;
}