Skip to content

Commit d868357

Browse files
committed
Make structured suggestion for fn casting verbose
1 parent 6c2c8ed commit d868357

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
404404
(msg, sug)
405405
}
406406
};
407-
diag.span_suggestion(span, msg, sug, Applicability::MaybeIncorrect);
407+
diag.span_suggestion_verbose(span, msg, sug, Applicability::MaybeIncorrect);
408408
}
409409
(ty::FnDef(did1, substs1), ty::FnDef(did2, substs2)) => {
410410
let expected_sig =

tests/ui/fn/fn-pointer-mismatch.stderr

+18-12
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,48 @@ error[E0308]: mismatched types
4343
--> $DIR/fn-pointer-mismatch.rs:36:29
4444
|
4545
LL | let c: fn(u32) -> u32 = &foo;
46-
| -------------- ^^^^
47-
| | |
48-
| | expected fn pointer, found `&fn(u32) -> u32 {foo}`
49-
| | help: consider removing the reference: `foo`
46+
| -------------- ^^^^ expected fn pointer, found `&fn(u32) -> u32 {foo}`
47+
| |
5048
| expected due to this
5149
|
5250
= note: expected fn pointer `fn(u32) -> u32`
5351
found reference `&fn(u32) -> u32 {foo}`
52+
help: consider removing the reference
53+
|
54+
LL | let c: fn(u32) -> u32 = foo;
55+
| ~~~
5456

5557
error[E0308]: mismatched types
5658
--> $DIR/fn-pointer-mismatch.rs:42:30
5759
|
5860
LL | let d: &fn(u32) -> u32 = foo;
59-
| --------------- ^^^
60-
| | |
61-
| | expected `&fn(u32) -> u32`, found fn item
62-
| | help: consider using a reference: `&foo`
61+
| --------------- ^^^ expected `&fn(u32) -> u32`, found fn item
62+
| |
6363
| expected due to this
6464
|
6565
= note: expected reference `&fn(u32) -> u32`
6666
found fn item `fn(u32) -> u32 {foo}`
67+
help: consider using a reference
68+
|
69+
LL | let d: &fn(u32) -> u32 = &foo;
70+
| ~~~~
6771

6872
error[E0308]: mismatched types
6973
--> $DIR/fn-pointer-mismatch.rs:48:30
7074
|
7175
LL | let e: &fn(u32) -> u32 = &foo;
72-
| --------------- ^^^^
73-
| | |
74-
| | expected `&fn(u32) -> u32`, found `&fn(u32) -> u32 {foo}`
75-
| | help: consider casting to a fn pointer: `&(foo as fn(u32) -> u32)`
76+
| --------------- ^^^^ expected `&fn(u32) -> u32`, found `&fn(u32) -> u32 {foo}`
77+
| |
7678
| expected due to this
7779
|
7880
= note: expected reference `&fn(u32) -> u32`
7981
found reference `&fn(u32) -> u32 {foo}`
8082
= note: fn items are distinct from fn pointers
8183
= note: when the arguments and return types match, functions can be coerced to function pointers
84+
help: consider casting to a fn pointer
85+
|
86+
LL | let e: &fn(u32) -> u32 = &(foo as fn(u32) -> u32);
87+
| ~~~~~~~~~~~~~~~~~~~~~~~~
8288

8389
error: aborting due to 6 previous errors
8490

tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.mir.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ LL | #[target_feature(enable = "sse2")]
55
| ---------------------------------- `#[target_feature]` added here
66
...
77
LL | let foo: fn() = foo;
8-
| ---- ^^^
9-
| | |
10-
| | cannot coerce functions with `#[target_feature]` to safe function pointers
11-
| | help: consider casting to a fn pointer: `foo as fn()`
8+
| ---- ^^^ cannot coerce functions with `#[target_feature]` to safe function pointers
9+
| |
1210
| expected due to this
1311
|
1412
= note: expected fn pointer `fn()`
1513
found fn item `fn() {foo}`
1614
= note: fn items are distinct from fn pointers
1715
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
1816
= note: when the arguments and return types match, functions can be coerced to function pointers
17+
help: consider casting to a fn pointer
18+
|
19+
LL | let foo: fn() = foo as fn();
20+
| ~~~~~~~~~~~
1921

2022
error: aborting due to previous error
2123

tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.thir.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ LL | #[target_feature(enable = "sse2")]
55
| ---------------------------------- `#[target_feature]` added here
66
...
77
LL | let foo: fn() = foo;
8-
| ---- ^^^
9-
| | |
10-
| | cannot coerce functions with `#[target_feature]` to safe function pointers
11-
| | help: consider casting to a fn pointer: `foo as fn()`
8+
| ---- ^^^ cannot coerce functions with `#[target_feature]` to safe function pointers
9+
| |
1210
| expected due to this
1311
|
1412
= note: expected fn pointer `fn()`
1513
found fn item `fn() {foo}`
1614
= note: fn items are distinct from fn pointers
1715
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
1816
= note: when the arguments and return types match, functions can be coerced to function pointers
17+
help: consider casting to a fn pointer
18+
|
19+
LL | let foo: fn() = foo as fn();
20+
| ~~~~~~~~~~~
1921

2022
error: aborting due to previous error
2123

tests/ui/static/static-reference-to-fn-1.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ error[E0308]: mismatched types
22
--> $DIR/static-reference-to-fn-1.rs:17:15
33
|
44
LL | func: &foo,
5-
| ^^^^
6-
| |
7-
| expected `&fn() -> Option<isize>`, found `&fn() -> Option<isize> {foo}`
8-
| help: consider casting to a fn pointer: `&(foo as fn() -> Option<isize>)`
5+
| ^^^^ expected `&fn() -> Option<isize>`, found `&fn() -> Option<isize> {foo}`
96
|
107
= note: expected reference `&fn() -> Option<isize>`
118
found reference `&fn() -> Option<isize> {foo}`
129
= note: fn items are distinct from fn pointers
1310
= note: when the arguments and return types match, functions can be coerced to function pointers
11+
help: consider casting to a fn pointer
12+
|
13+
LL | func: &(foo as fn() -> Option<isize>),
14+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1415

1516
error: aborting due to previous error
1617

0 commit comments

Comments
 (0)