]> git.lizzy.rs Git - rust.git/blob - tests/ui/object-lifetime/object-lifetime-default-from-box-error.rs
Rollup merge of #106664 - chenyukang:yukang/fix-106597-remove-lseek, r=cuviper
[rust.git] / tests / ui / object-lifetime / object-lifetime-default-from-box-error.rs
1 // Test various cases where the defaults should lead to errors being
2 // reported.
3
4 #![allow(dead_code)]
5
6 trait SomeTrait {
7     fn dummy(&self) { }
8 }
9
10 struct SomeStruct<'a> {
11     r: Box<dyn SomeTrait+'a>
12 }
13
14 fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait> {
15     // `Box<SomeTrait>` defaults to a `'static` bound, so this return
16     // is illegal.
17
18     ss.r
19     //~^ ERROR lifetime may not live long enough
20     //~| ERROR cannot move out of
21 }
22
23 fn store(ss: &mut SomeStruct, b: Box<dyn SomeTrait>) {
24     // No error: b is bounded by 'static which outlives the
25     // (anonymous) lifetime on the struct.
26
27     ss.r = b;
28 }
29
30 fn store1<'b>(ss: &mut SomeStruct, b: Box<dyn SomeTrait+'b>) {
31     // Here we override the lifetimes explicitly, and so naturally we get an error.
32
33     ss.r = b; //~ ERROR explicit lifetime required in the type of `ss` [E0621]
34 }
35
36 fn main() {
37 }