]> git.lizzy.rs Git - rust.git/blob - tests/ui/destructure-trait-ref.rs
Rollup merge of #107306 - compiler-errors:correct-sugg-for-closure-arg-needs-borrow...
[rust.git] / tests / ui / destructure-trait-ref.rs
1 // The regression test for #15031 to make sure destructuring trait
2 // reference work properly.
3
4 #![feature(box_patterns)]
5
6 trait T { fn foo(&self) {} }
7 impl T for isize {}
8
9
10 fn main() {
11     // For an expression of the form:
12     //
13     //      let &...&x = &..&SomeTrait;
14     //
15     // Say we have n `&` at the left hand and m `&` right hand, then:
16     // if n < m, we are golden;
17     // if n == m, it's a derefing non-derefable type error;
18     // if n > m, it's a type mismatch error.
19
20     // n < m
21     let &x = &(&1isize as &dyn T);
22     let &x = &&(&1isize as &dyn T);
23     let &&x = &&(&1isize as &dyn T);
24
25     // n == m
26     let &x = &1isize as &dyn T;      //~ ERROR type `&dyn T` cannot be dereferenced
27     let &&x = &(&1isize as &dyn T);  //~ ERROR type `&dyn T` cannot be dereferenced
28     let box x = Box::new(1isize) as Box<dyn T>;
29     //~^ ERROR type `Box<dyn T>` cannot be dereferenced
30
31     // n > m
32     let &&x = &1isize as &dyn T;
33     //~^ ERROR mismatched types
34     //~| expected trait object `dyn T`
35     //~| found reference `&_`
36     let &&&x = &(&1isize as &dyn T);
37     //~^ ERROR mismatched types
38     //~| expected trait object `dyn T`
39     //~| found reference `&_`
40     let box box x = Box::new(1isize) as Box<dyn T>;
41     //~^ ERROR mismatched types
42     //~| expected trait object `dyn T`
43     //~| found struct `Box<_>`
44 }