about summary refs log tree commit diff
path: root/tests/ui
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-02-02 12:31:58 +0100
committerGitHub <noreply@github.com>2025-02-02 12:31:58 +0100
commit44def58274eb0bea097a1e7f545b06800c7dcdc4 (patch)
tree1552a1f9beaaf48e1b59a0eeb610125139c6de0d /tests/ui
parent3559a48b8ee156351663da26ee39058e20d7837d (diff)
parent028a920c5310afc918c617e50074348976332a08 (diff)
downloadrust-44def58274eb0bea097a1e7f545b06800c7dcdc4.tar.gz
rust-44def58274eb0bea097a1e7f545b06800c7dcdc4.zip
Rollup merge of #136412 - estebank:fn-ptr-cast-suggestion, r=jieyouxu
Tweak fn pointer suggestion span

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.
Diffstat (limited to 'tests/ui')
-rw-r--r--tests/ui/fn/fn-pointer-mismatch.stderr2
-rw-r--r--tests/ui/force-inlining/cast.stderr4
2 files changed, 3 insertions, 3 deletions
diff --git a/tests/ui/fn/fn-pointer-mismatch.stderr b/tests/ui/fn/fn-pointer-mismatch.stderr
index 9cda11639d0..2aa840911f6 100644
--- a/tests/ui/fn/fn-pointer-mismatch.stderr
+++ b/tests/ui/fn/fn-pointer-mismatch.stderr
@@ -67,7 +67,7 @@ LL |     let d: &fn(u32) -> u32 = foo;
 help: consider using a reference
    |
 LL |     let d: &fn(u32) -> u32 = &foo;
-   |                              ~~~~
+   |                              +
 
 error[E0308]: mismatched types
   --> $DIR/fn-pointer-mismatch.rs:48:30
diff --git a/tests/ui/force-inlining/cast.stderr b/tests/ui/force-inlining/cast.stderr
index 116919e5fe7..53132959659 100644
--- a/tests/ui/force-inlining/cast.stderr
+++ b/tests/ui/force-inlining/cast.stderr
@@ -12,7 +12,7 @@ LL |     let _: fn(isize) -> usize = callee;
 help: consider casting to a fn pointer
    |
 LL |     let _: fn(isize) -> usize = callee as fn(isize) -> usize;
-   |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+   |                                        +++++++++++++++++++++
 
 error[E0605]: non-primitive cast: `fn(isize) -> usize {callee}` as `fn(isize) -> usize`
   --> $DIR/cast.rs:15:13
@@ -32,7 +32,7 @@ LL |         callee,
 help: consider casting to a fn pointer
    |
 LL |         callee as fn(isize) -> usize,
-   |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+   |                +++++++++++++++++++++
 
 error: aborting due to 3 previous errors