Skip to content

Commit 44def58

Browse files
authored
Rollup merge of rust-lang#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.
2 parents 3559a48 + 028a920 commit 44def58

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
418418
};
419419

420420
let sugg = match (expected.is_ref(), found.is_ref()) {
421-
(true, false) => FunctionPointerSuggestion::UseRef { span, fn_name },
421+
(true, false) => {
422+
FunctionPointerSuggestion::UseRef { span: span.shrink_to_lo() }
423+
}
422424
(false, true) => FunctionPointerSuggestion::RemoveRef { span, fn_name },
423425
(true, true) => {
424426
diag.subdiagnostic(FnItemsAreDistinct);
425427
FunctionPointerSuggestion::CastRef { span, fn_name, sig }
426428
}
427429
(false, false) => {
428430
diag.subdiagnostic(FnItemsAreDistinct);
429-
FunctionPointerSuggestion::Cast { span, fn_name, sig }
431+
FunctionPointerSuggestion::Cast { span: span.shrink_to_hi(), sig }
430432
}
431433
};
432434
diag.subdiagnostic(sugg);
@@ -466,8 +468,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
466468
}
467469
} else {
468470
FunctionPointerSuggestion::CastBoth {
469-
span,
470-
fn_name,
471+
span: span.shrink_to_hi(),
471472
found_sig: *found_sig,
472473
expected_sig: *expected_sig,
473474
}

compiler/rustc_trait_selection/src/errors.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1391,15 +1391,13 @@ pub struct OpaqueCapturesLifetime<'tcx> {
13911391
pub enum FunctionPointerSuggestion<'a> {
13921392
#[suggestion(
13931393
trait_selection_fps_use_ref,
1394-
code = "&{fn_name}",
1394+
code = "&",
13951395
style = "verbose",
13961396
applicability = "maybe-incorrect"
13971397
)]
13981398
UseRef {
13991399
#[primary_span]
14001400
span: Span,
1401-
#[skip_arg]
1402-
fn_name: String,
14031401
},
14041402
#[suggestion(
14051403
trait_selection_fps_remove_ref,
@@ -1429,30 +1427,26 @@ pub enum FunctionPointerSuggestion<'a> {
14291427
},
14301428
#[suggestion(
14311429
trait_selection_fps_cast,
1432-
code = "{fn_name} as {sig}",
1430+
code = " as {sig}",
14331431
style = "verbose",
14341432
applicability = "maybe-incorrect"
14351433
)]
14361434
Cast {
14371435
#[primary_span]
14381436
span: Span,
14391437
#[skip_arg]
1440-
fn_name: String,
1441-
#[skip_arg]
14421438
sig: Binder<'a, FnSig<'a>>,
14431439
},
14441440
#[suggestion(
14451441
trait_selection_fps_cast_both,
1446-
code = "{fn_name} as {found_sig}",
1442+
code = " as {found_sig}",
14471443
style = "hidden",
14481444
applicability = "maybe-incorrect"
14491445
)]
14501446
CastBoth {
14511447
#[primary_span]
14521448
span: Span,
14531449
#[skip_arg]
1454-
fn_name: String,
1455-
#[skip_arg]
14561450
found_sig: Binder<'a, FnSig<'a>>,
14571451
expected_sig: Binder<'a, FnSig<'a>>,
14581452
},

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ LL | let d: &fn(u32) -> u32 = foo;
6767
help: consider using a reference
6868
|
6969
LL | let d: &fn(u32) -> u32 = &foo;
70-
| ~~~~
70+
| +
7171

7272
error[E0308]: mismatched types
7373
--> $DIR/fn-pointer-mismatch.rs:48:30

tests/ui/force-inlining/cast.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | let _: fn(isize) -> usize = callee;
1212
help: consider casting to a fn pointer
1313
|
1414
LL | let _: fn(isize) -> usize = callee as fn(isize) -> usize;
15-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15+
| +++++++++++++++++++++
1616

1717
error[E0605]: non-primitive cast: `fn(isize) -> usize {callee}` as `fn(isize) -> usize`
1818
--> $DIR/cast.rs:15:13
@@ -32,7 +32,7 @@ LL | callee,
3232
help: consider casting to a fn pointer
3333
|
3434
LL | callee as fn(isize) -> usize,
35-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35+
| +++++++++++++++++++++
3636

3737
error: aborting due to 3 previous errors
3838

0 commit comments

Comments
 (0)