about summary refs log tree commit diff
path: root/tests/ui/fn/fn-pointer-mismatch.rs
blob: 3854c8eab688f900573ddaf6c60beacabc916a43 (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
//@ dont-require-annotations: NOTE

fn foo(x: u32) -> u32 {
    x * 2
}

fn bar(x: u32) -> u32 {
    x * 3
}

// original example from Issue #102608
fn foobar(n: u32) -> u32 {
    let g = if n % 2 == 0 { &foo } else { &bar };
    //~^ ERROR `if` and `else` have incompatible types
    //~| NOTE different fn items have unique types, even if their signatures are the same
    g(n)
}

fn main() {
    assert_eq!(foobar(7), 21);
    assert_eq!(foobar(8), 16);

    // general mismatch of fn item types
    let mut a = foo;
    a = bar;
    //~^ ERROR mismatched types
    //~| NOTE expected fn item `fn(_) -> _ {foo}`
    //~| NOTE found fn item `fn(_) -> _ {bar}`
    //~| NOTE different fn items have unique types, even if their signatures are the same

    // display note even when boxed
    let mut b = Box::new(foo);
    b = Box::new(bar);
    //~^ ERROR mismatched types
    //~| NOTE different fn items have unique types, even if their signatures are the same

    // suggest removing reference
    let c: fn(u32) -> u32 = &foo;
    //~^ ERROR mismatched types
    //~| NOTE expected fn pointer `fn(_) -> _`
    //~| NOTE found reference `&fn(_) -> _ {foo}`

    // suggest using reference
    let d: &fn(u32) -> u32 = foo;
    //~^ ERROR mismatched types
    //~| NOTE expected reference `&fn(_) -> _`
    //~| NOTE found fn item `fn(_) -> _ {foo}`

    // suggest casting with reference
    let e: &fn(u32) -> u32 = &foo;
    //~^ ERROR mismatched types
    //~| NOTE expected reference `&fn(_) -> _`
    //~| NOTE found reference `&fn(_) -> _ {foo}`

    // OK
    let mut z: fn(u32) -> u32 = foo as fn(u32) -> u32;
    z = bar;
}