mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-03 13:37:37 +00:00

Use a more targeted span when suggesting casting an `fn` item to an `fn` pointer. ``` error[E0308]: cannot coerce functions which must be inlined to function pointers --> $DIR/cast.rs:10:33 | LL | let _: fn(isize) -> usize = callee; | ------------------ ^^^^^^ cannot coerce functions which must be inlined to function pointers | | | expected due to this | = note: expected fn pointer `fn(_) -> _` found fn item `fn(_) -> _ {callee}` = note: fn items are distinct from fn pointers help: consider casting to a fn pointer | LL | let _: fn(isize) -> usize = callee as fn(isize) -> usize; | +++++++++++++++++++++ ``` ``` error[E0308]: mismatched types --> $DIR/fn-pointer-mismatch.rs:42:30 | LL | let d: &fn(u32) -> u32 = foo; | --------------- ^^^ expected `&fn(u32) -> u32`, found fn item | | | expected due to this | = note: expected reference `&fn(_) -> _` found fn item `fn(_) -> _ {foo}` help: consider using a reference | LL | let d: &fn(u32) -> u32 = &foo; | + ``` Previously we'd point at the whole expression for replacement, instead of marking what was being added. We could also modify the suggestions for `&(name as fn())`, but for that we require storing more accurate spans than we have now.
91 lines
3.3 KiB
Plaintext
91 lines
3.3 KiB
Plaintext
error[E0308]: `if` and `else` have incompatible types
|
|
--> $DIR/fn-pointer-mismatch.rs:11:43
|
|
|
|
|
LL | let g = if n % 2 == 0 { &foo } else { &bar };
|
|
| ---- ^^^^ expected `&fn(u32) -> u32 {foo}`, found `&fn(u32) -> u32 {bar}`
|
|
| |
|
|
| expected because of this
|
|
|
|
|
= note: expected reference `&fn(_) -> _ {foo}`
|
|
found reference `&fn(_) -> _ {bar}`
|
|
= note: different fn items have unique types, even if their signatures are the same
|
|
= help: consider casting both fn items to fn pointers using `as fn(u32) -> u32`
|
|
|
|
error[E0308]: mismatched types
|
|
--> $DIR/fn-pointer-mismatch.rs:23:9
|
|
|
|
|
LL | let mut a = foo;
|
|
| --- expected due to this value
|
|
LL | a = bar;
|
|
| ^^^ expected fn item, found a different fn item
|
|
|
|
|
= note: expected fn item `fn(_) -> _ {foo}`
|
|
found fn item `fn(_) -> _ {bar}`
|
|
= note: different fn items have unique types, even if their signatures are the same
|
|
= help: consider casting both fn items to fn pointers using `as fn(u32) -> u32`
|
|
|
|
error[E0308]: mismatched types
|
|
--> $DIR/fn-pointer-mismatch.rs:31:18
|
|
|
|
|
LL | b = Box::new(bar);
|
|
| -------- ^^^ expected fn item, found a different fn item
|
|
| |
|
|
| arguments to this function are incorrect
|
|
|
|
|
= note: expected fn item `fn(_) -> _ {foo}`
|
|
found fn item `fn(_) -> _ {bar}`
|
|
= note: different fn items have unique types, even if their signatures are the same
|
|
note: associated function defined here
|
|
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
|
|
= help: consider casting both fn items to fn pointers using `as fn(u32) -> u32`
|
|
|
|
error[E0308]: mismatched types
|
|
--> $DIR/fn-pointer-mismatch.rs:36:29
|
|
|
|
|
LL | let c: fn(u32) -> u32 = &foo;
|
|
| -------------- ^^^^ expected fn pointer, found `&fn(u32) -> u32 {foo}`
|
|
| |
|
|
| expected due to this
|
|
|
|
|
= note: expected fn pointer `fn(_) -> _`
|
|
found reference `&fn(_) -> _ {foo}`
|
|
help: consider removing the reference
|
|
|
|
|
LL | let c: fn(u32) -> u32 = foo;
|
|
| ~~~
|
|
|
|
error[E0308]: mismatched types
|
|
--> $DIR/fn-pointer-mismatch.rs:42:30
|
|
|
|
|
LL | let d: &fn(u32) -> u32 = foo;
|
|
| --------------- ^^^ expected `&fn(u32) -> u32`, found fn item
|
|
| |
|
|
| expected due to this
|
|
|
|
|
= note: expected reference `&fn(_) -> _`
|
|
found fn item `fn(_) -> _ {foo}`
|
|
help: consider using a reference
|
|
|
|
|
LL | let d: &fn(u32) -> u32 = &foo;
|
|
| +
|
|
|
|
error[E0308]: mismatched types
|
|
--> $DIR/fn-pointer-mismatch.rs:48:30
|
|
|
|
|
LL | let e: &fn(u32) -> u32 = &foo;
|
|
| --------------- ^^^^ expected `&fn(u32) -> u32`, found `&fn(u32) -> u32 {foo}`
|
|
| |
|
|
| expected due to this
|
|
|
|
|
= note: expected reference `&fn(_) -> _`
|
|
found reference `&fn(_) -> _ {foo}`
|
|
= note: fn items are distinct from fn pointers
|
|
help: consider casting to a fn pointer
|
|
|
|
|
LL | let e: &fn(u32) -> u32 = &(foo as fn(u32) -> u32);
|
|
| ~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
error: aborting due to 6 previous errors
|
|
|
|
For more information about this error, try `rustc --explain E0308`.
|