File: lifetime-bound-will-change-warning.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 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; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (56 lines) | stat: -rw-r--r-- 1,285 bytes parent folder | download | duplicates (6)
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
//@ aux-build:lifetime_bound_will_change_warning_lib.rs

// Test that various corner cases cause an error. These are tests
// that used to pass before we tweaked object defaults.

#![allow(dead_code)]
#![allow(unused_variables)]


extern crate lifetime_bound_will_change_warning_lib as lib;

fn just_ref(x: &dyn Fn()) {
}

fn ref_obj(x: &Box<dyn Fn()>) {
    // this will change to &Box<Fn()+'static>...

    // Note: no warning is issued here, because the type of `x` will change to 'static
    if false { ref_obj(x); }
}

fn test1<'a>(x: &'a Box<dyn Fn() + 'a>) {
    // just_ref will stay the same.
    just_ref(&**x)
}

fn test1cc<'a>(x: &'a Box<dyn Fn() + 'a>) {
    // same as test1, but cross-crate
    lib::just_ref(&**x)
}

fn test2<'a>(x: &'a Box<dyn Fn() + 'a>) {
    // but ref_obj will not, so warn.
    ref_obj(x)
    //~^ ERROR borrowed data escapes
}

fn test2cc<'a>(x: &'a Box<dyn Fn() + 'a>) {
    // same as test2, but cross crate
    lib::ref_obj(x)
    //~^ ERROR borrowed data escapes
}

fn test3<'a>(x: &'a Box<dyn Fn() + 'static>) {
    // here, we have a 'static bound, so even when ref_obj changes, no error results
    ref_obj(x)
}

fn test3cc<'a>(x: &'a Box<dyn Fn() + 'static>) {
    // same as test3, but cross crate
    lib::ref_obj(x)
}


fn main() {
}