File: non-defaulted-item-fail.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 (54 lines) | stat: -rw-r--r-- 1,616 bytes parent folder | download | duplicates (24)
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
52
53
54
#![feature(specialization, associated_type_defaults)]
//~^ WARN the feature `specialization` is incomplete

// Test that attempting to override a non-default method or one not in the
// parent impl causes an error.

trait Foo {
    type Ty = ();
    const CONST: u8 = 123;
    fn foo(&self) -> bool { true }
}

// Specialization tree for Foo:
//
//       Box<T>              Vec<T>
//        / \                 / \
// Box<i32>  Box<i64>   Vec<()>  Vec<bool>

impl<T> Foo for Box<T> {
    type Ty = bool;
    const CONST: u8 = 0;
    fn foo(&self) -> bool { false }
}

// Allowed
impl Foo for Box<i32> {}

// Can't override a non-`default` fn
impl Foo for Box<i64> {
    type Ty = Vec<()>;
//~^ error: `Ty` specializes an item from a parent `impl`, but that item is not marked `default`
    const CONST: u8 = 42;
//~^ error: `CONST` specializes an item from a parent `impl`, but that item is not marked `default`
    fn foo(&self) -> bool { true }
//~^ error: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
}


// Doesn't mention the item = provided body/value is used and the method is final.
impl<T> Foo for Vec<T> {}

// Allowed
impl Foo for Vec<()> {}

impl Foo for Vec<bool> {
    type Ty = Vec<()>;
//~^ error: `Ty` specializes an item from a parent `impl`, but that item is not marked `default`
    const CONST: u8 = 42;
//~^ error: `CONST` specializes an item from a parent `impl`, but that item is not marked `default`
    fn foo(&self) -> bool { true }
//~^ error: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
}

fn main() {}