File: issue-61631-default-type-param-cannot-reference-self.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 (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;
}