about summary refs log tree commit diff
path: root/tests/ui/impl-trait/non-defining-uses/ambiguous-ops.rs
blob: 0aa5715339dc2ba48666ab9822b6343b5bc9c73f (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//@ revisions: current next
//@[next] compile-flags: -Znext-solver
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] check-pass

// Make sure we support non-call operations for opaque types even if
// its not part of its item bounds.

use std::ops::{Deref, DerefMut, Index, IndexMut};

fn mk<T>() -> T {
    todo!()
}

fn add() -> impl Sized {
    let unconstrained = if false {
        add() + 1
        //[current]~^ ERROR cannot add `{integer}` to `impl Sized
    } else {
        let with_infer = mk();
        let _ = with_infer + 1;
        with_infer
    };
    let _: u32 = unconstrained;
    1u32
}

fn mul_assign() -> impl Sized {
    if false {
        let mut temp = mul_assign();
        temp *= 2;
        //[current]~^ ERROR binary assignment operation `*=` cannot be applied to type `impl Sized`
    }

    let mut with_infer = mk();
    with_infer *= 2;
    let _: u32 = with_infer;

    1u32
}

struct DerefWrapper<T>(T);
impl<T: Deref> Deref for DerefWrapper<T> {
    type Target = T::Target;
    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}
impl<T: DerefMut> DerefMut for DerefWrapper<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.0
    }
}

fn explicit_deref() -> DerefWrapper<impl Sized> {
    if false {
        let _rarw = &*explicit_deref();
        //[current]~^ ERROR type `DerefWrapper<impl Sized>` cannot be dereferenced

        let mut with_infer = DerefWrapper(mk());
        let _rarw = &*with_infer;
        with_infer
    } else {
        DerefWrapper(&1u32)
    }
}
fn explicit_deref_mut() -> DerefWrapper<impl Sized> {
    if false {
        *explicit_deref_mut() = 1;
        //[current]~^ ERROR type `DerefWrapper<impl Sized>` cannot be dereferenced

        let mut with_infer = DerefWrapper(Default::default());
        *with_infer = 1;
        with_infer
    } else {
        DerefWrapper(Box::new(1u32))
    }
}

struct IndexWrapper<T>(T);
impl<T: Index<U>, U> Index<U> for IndexWrapper<T> {
    type Output = T::Output;
    fn index(&self, index: U) -> &Self::Output {
        &self.0[index]
    }
}
impl<T: IndexMut<U>, U> IndexMut<U> for IndexWrapper<T> {
    fn index_mut(&mut self, index: U) -> &mut Self::Output {
        &mut self.0[index]
    }
}
fn explicit_index() -> IndexWrapper<impl Sized> {
    if false {
        let _y = explicit_index()[0];
        //[current]~^ ERROR the type `impl Sized` cannot be indexed by `_`

        let with_infer = IndexWrapper(Default::default());
        let _y = with_infer[0];
        with_infer
    } else {
        IndexWrapper([1u32])
    }
}
fn explicit_index_mut() -> IndexWrapper<impl Sized> {
    if false {
        explicit_index_mut()[0] = 1;
        //[current]~^ ERROR the type `impl Sized` cannot be indexed by `_`

        let mut with_infer = IndexWrapper(Default::default());
        with_infer[0] = 1;
        with_infer
    } else {
        IndexWrapper([1u32])
    }
}

fn main() {}