File: expect-fn-supply-fn.rs

package info (click to toggle)
rustc 1.87.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 925,564 kB
  • sloc: xml: 158,127; python: 36,039; javascript: 19,761; sh: 19,737; cpp: 18,981; ansic: 13,133; asm: 4,376; makefile: 710; perl: 29; lisp: 28; ruby: 19; sql: 11
file content (62 lines) | stat: -rw-r--r-- 2,205 bytes parent folder | download | duplicates (16)
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
fn with_closure_expecting_fn_with_free_region<F>(_: F)
where
    F: for<'a> FnOnce(fn(&'a u32), &i32),
{
}

fn with_closure_expecting_fn_with_bound_region<F>(_: F)
where
    F: FnOnce(fn(&u32), &i32),
{
}

fn expect_free_supply_free_from_fn<'x>(x: &'x u32) {
    // Here, the type given for `'x` "obscures" a region from the
    // expected signature that is bound at closure level.
    with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {});
    //~^ ERROR lifetime may not live long enough
    //~| ERROR lifetime may not live long enough
}

fn expect_free_supply_free_from_closure() {
    // A variant on the previous test. Here, the region `'a` will be
    // bound at the closure level, just as is expected, so no error
    // results.
    type Foo<'a> = fn(&'a u32);
    with_closure_expecting_fn_with_free_region(|_x: Foo<'_>, y| {});
}

fn expect_free_supply_bound() {
    // Here, we are given a function whose region is bound at closure level,
    // but we expect one bound in the argument. Error results.
    with_closure_expecting_fn_with_free_region(|x: fn(&u32), y| {});
    //~^ ERROR mismatched types [E0308]
    //~| ERROR lifetime may not live long enough
}

fn expect_bound_supply_free_from_fn<'x>(x: &'x u32) {
    // Here, we are given a `fn(&u32)` but we expect a `fn(&'x
    // u32)`. In principle, this could be ok, but we demand equality.
    with_closure_expecting_fn_with_bound_region(|x: fn(&'x u32), y| {});
    //~^ ERROR mismatched types [E0308]
    //~| ERROR lifetime may not live long enough
}

fn expect_bound_supply_free_from_closure() {
    // A variant on the previous test. Here, the region `'a` will be
    // bound at the closure level, but we expect something bound at
    // the argument level.
    type Foo<'a> = fn(&'a u32);
    with_closure_expecting_fn_with_bound_region(|x: Foo<'_>, y| {
        //~^ ERROR mismatched types
    });
}

fn expect_bound_supply_bound<'x>(x: &'x u32) {
    // No error in this case. The supplied type supplies the bound
    // regions, and hence we are able to figure out the type of `y`
    // from the expected type
    with_closure_expecting_fn_with_bound_region(|x: for<'z> fn(&'z u32), y| {});
}

fn main() {}