Skip to content

Commit 94b1960

Browse files
authored
Rollup merge of #95260 - compiler-errors:fn, r=davidtwco
Better suggestions for `Fn`-family trait selection errors 1. Suppress suggestions to add `std::ops::Fn{,Mut,Once}` bounds when a type already implements `Fn{,Mut,Once}` 2. Add a note that points out that a type does in fact implement `Fn{,Mut,Once}`, but the arguments vary (either by number or by actual arguments) 3. Add a note that points out that a type does in fact implement `Fn{,Mut,Once}`, but not the right one (e.g. implements `FnMut`, but `Fn` is required). Fixes #95147
2 parents 3cb5925 + e0c8780 commit 94b1960

File tree

8 files changed

+255
-9
lines changed

8 files changed

+255
-9
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10911091

10921092
/// Compares two given types, eliding parts that are the same between them and highlighting
10931093
/// relevant differences, and return two representation of those types for highlighted printing.
1094-
fn cmp(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> (DiagnosticStyledString, DiagnosticStyledString) {
1094+
pub fn cmp(
1095+
&self,
1096+
t1: Ty<'tcx>,
1097+
t2: Ty<'tcx>,
1098+
) -> (DiagnosticStyledString, DiagnosticStyledString) {
10951099
debug!("cmp(t1={}, t1.kind={:?}, t2={}, t2.kind={:?})", t1, t1.kind(), t2, t2.kind());
10961100

10971101
// helper functions

compiler/rustc_middle/src/ty/closure.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,21 @@ impl<'tcx> ClosureKind {
119119
/// See `Ty::to_opt_closure_kind` for more details.
120120
pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
121121
match self {
122-
ty::ClosureKind::Fn => tcx.types.i8,
123-
ty::ClosureKind::FnMut => tcx.types.i16,
124-
ty::ClosureKind::FnOnce => tcx.types.i32,
122+
ClosureKind::Fn => tcx.types.i8,
123+
ClosureKind::FnMut => tcx.types.i16,
124+
ClosureKind::FnOnce => tcx.types.i32,
125+
}
126+
}
127+
128+
pub fn from_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ClosureKind> {
129+
if Some(def_id) == tcx.lang_items().fn_once_trait() {
130+
Some(ClosureKind::FnOnce)
131+
} else if Some(def_id) == tcx.lang_items().fn_mut_trait() {
132+
Some(ClosureKind::FnMut)
133+
} else if Some(def_id) == tcx.lang_items().fn_trait() {
134+
Some(ClosureKind::Fn)
135+
} else {
136+
None
125137
}
126138
}
127139
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+121-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ pub mod on_unimplemented;
22
pub mod suggestions;
33

44
use super::{
5-
EvaluationResult, FulfillmentError, FulfillmentErrorCode, MismatchedProjectionTypes,
6-
Obligation, ObligationCause, ObligationCauseCode, OnUnimplementedDirective,
7-
OnUnimplementedNote, OutputTypeParameterMismatch, Overflow, PredicateObligation,
8-
SelectionContext, SelectionError, TraitNotObjectSafe,
5+
EvaluationResult, FulfillmentContext, FulfillmentError, FulfillmentErrorCode,
6+
MismatchedProjectionTypes, Obligation, ObligationCause, ObligationCauseCode,
7+
OnUnimplementedDirective, OnUnimplementedNote, OutputTypeParameterMismatch, Overflow,
8+
PredicateObligation, SelectionContext, SelectionError, TraitNotObjectSafe,
99
};
1010

1111
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
@@ -21,6 +21,8 @@ use rustc_hir::intravisit::Visitor;
2121
use rustc_hir::GenericParam;
2222
use rustc_hir::Item;
2323
use rustc_hir::Node;
24+
use rustc_infer::infer::error_reporting::same_type_modulo_infer;
25+
use rustc_infer::traits::TraitEngine;
2426
use rustc_middle::thir::abstract_const::NotConstEvaluatable;
2527
use rustc_middle::traits::select::OverflowError;
2628
use rustc_middle::ty::error::ExpectedFound;
@@ -103,6 +105,17 @@ pub trait InferCtxtExt<'tcx> {
103105
found_args: Vec<ArgKind>,
104106
is_closure: bool,
105107
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>;
108+
109+
/// Checks if the type implements one of `Fn`, `FnMut`, or `FnOnce`
110+
/// in that order, and returns the generic type corresponding to the
111+
/// argument of that trait (corresponding to the closure arguments).
112+
fn type_implements_fn_trait(
113+
&self,
114+
param_env: ty::ParamEnv<'tcx>,
115+
ty: ty::Binder<'tcx, Ty<'tcx>>,
116+
constness: ty::BoundConstness,
117+
polarity: ty::ImplPolarity,
118+
) -> Result<(ty::ClosureKind, ty::Binder<'tcx, Ty<'tcx>>), ()>;
106119
}
107120

108121
impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
@@ -563,7 +576,64 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
563576
}
564577

565578
// Try to report a help message
566-
if !trait_ref.has_infer_types_or_consts()
579+
if is_fn_trait
580+
&& let Ok((implemented_kind, params)) = self.type_implements_fn_trait(
581+
obligation.param_env,
582+
trait_ref.self_ty(),
583+
trait_predicate.skip_binder().constness,
584+
trait_predicate.skip_binder().polarity,
585+
)
586+
{
587+
// If the type implements `Fn`, `FnMut`, or `FnOnce`, suppress the following
588+
// suggestion to add trait bounds for the type, since we only typically implement
589+
// these traits once.
590+
591+
// Note if the `FnMut` or `FnOnce` is less general than the trait we're trying
592+
// to implement.
593+
let selected_kind =
594+
ty::ClosureKind::from_def_id(self.tcx, trait_ref.def_id())
595+
.expect("expected to map DefId to ClosureKind");
596+
if !implemented_kind.extends(selected_kind) {
597+
err.note(
598+
&format!(
599+
"`{}` implements `{}`, but it must implement `{}`, which is more general",
600+
trait_ref.skip_binder().self_ty(),
601+
implemented_kind,
602+
selected_kind
603+
)
604+
);
605+
}
606+
607+
// Note any argument mismatches
608+
let given_ty = params.skip_binder();
609+
let expected_ty = trait_ref.skip_binder().substs.type_at(1);
610+
if let ty::Tuple(given) = given_ty.kind()
611+
&& let ty::Tuple(expected) = expected_ty.kind()
612+
{
613+
if expected.len() != given.len() {
614+
// Note number of types that were expected and given
615+
err.note(
616+
&format!(
617+
"expected a closure taking {} argument{}, but one taking {} argument{} was given",
618+
given.len(),
619+
if given.len() == 1 { "" } else { "s" },
620+
expected.len(),
621+
if expected.len() == 1 { "" } else { "s" },
622+
)
623+
);
624+
} else if !same_type_modulo_infer(given_ty, expected_ty) {
625+
// Print type mismatch
626+
let (expected_args, given_args) =
627+
self.cmp(given_ty, expected_ty);
628+
err.note_expected_found(
629+
&"a closure with arguments",
630+
expected_args,
631+
&"a closure with arguments",
632+
given_args,
633+
);
634+
}
635+
}
636+
} else if !trait_ref.has_infer_types_or_consts()
567637
&& self.predicate_can_apply(obligation.param_env, trait_ref)
568638
{
569639
// If a where-clause may be useful, remind the
@@ -1148,6 +1218,52 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11481218

11491219
err
11501220
}
1221+
1222+
fn type_implements_fn_trait(
1223+
&self,
1224+
param_env: ty::ParamEnv<'tcx>,
1225+
ty: ty::Binder<'tcx, Ty<'tcx>>,
1226+
constness: ty::BoundConstness,
1227+
polarity: ty::ImplPolarity,
1228+
) -> Result<(ty::ClosureKind, ty::Binder<'tcx, Ty<'tcx>>), ()> {
1229+
self.commit_if_ok(|_| {
1230+
for trait_def_id in [
1231+
self.tcx.lang_items().fn_trait(),
1232+
self.tcx.lang_items().fn_mut_trait(),
1233+
self.tcx.lang_items().fn_once_trait(),
1234+
] {
1235+
let Some(trait_def_id) = trait_def_id else { continue };
1236+
// Make a fresh inference variable so we can determine what the substitutions
1237+
// of the trait are.
1238+
let var = self.next_ty_var(TypeVariableOrigin {
1239+
span: DUMMY_SP,
1240+
kind: TypeVariableOriginKind::MiscVariable,
1241+
});
1242+
let substs = self.tcx.mk_substs_trait(ty.skip_binder(), &[var.into()]);
1243+
let obligation = Obligation::new(
1244+
ObligationCause::dummy(),
1245+
param_env,
1246+
ty.rebind(ty::TraitPredicate {
1247+
trait_ref: ty::TraitRef::new(trait_def_id, substs),
1248+
constness,
1249+
polarity,
1250+
})
1251+
.to_predicate(self.tcx),
1252+
);
1253+
let mut fulfill_cx = FulfillmentContext::new_in_snapshot();
1254+
fulfill_cx.register_predicate_obligation(self, obligation);
1255+
if fulfill_cx.select_all_or_error(self).is_empty() {
1256+
return Ok((
1257+
ty::ClosureKind::from_def_id(self.tcx, trait_def_id)
1258+
.expect("expected to map DefId to ClosureKind"),
1259+
ty.rebind(self.resolve_vars_if_possible(var)),
1260+
));
1261+
}
1262+
}
1263+
1264+
Err(())
1265+
})
1266+
}
11511267
}
11521268

11531269
trait InferCtxtPrivExt<'hir, 'tcx> {

src/test/ui/higher-rank-trait-bounds/normalize-under-binder/issue-62529-3.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ error[E0277]: expected a `Fn<(<_ as ATC<'a>>::Type,)>` closure, found `F`
44
LL | call(f, ());
55
| ^^^^ expected an `Fn<(<_ as ATC<'a>>::Type,)>` closure, found `F`
66
|
7+
= note: expected a closure with arguments `((),)`
8+
found a closure with arguments `(<_ as ATC<'a>>::Type,)`
79
note: required by a bound in `call`
810
--> $DIR/issue-62529-3.rs:9:36
911
|

src/test/ui/issues/issue-59494.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | let t8 = t8n(t7, t7p(f, g));
77
| required by a bound introduced by this call
88
|
99
= help: the trait `Fn<(_,)>` is not implemented for `impl Fn(((_, _), _))`
10+
= note: expected a closure with arguments `(((_, _), _),)`
11+
found a closure with arguments `(_,)`
1012
note: required by a bound in `t8n`
1113
--> $DIR/issue-59494.rs:5:45
1214
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fn take(_f: impl FnMut(i32)) {}
2+
3+
fn test1(f: impl FnMut(u32)) {
4+
take(f)
5+
//~^ ERROR [E0277]
6+
}
7+
8+
fn test2(f: impl FnMut(i32, i32)) {
9+
take(f)
10+
//~^ ERROR [E0277]
11+
}
12+
13+
fn test3(f: impl FnMut()) {
14+
take(f)
15+
//~^ ERROR [E0277]
16+
}
17+
18+
fn test4(f: impl FnOnce(i32)) {
19+
take(f)
20+
//~^ ERROR [E0277]
21+
}
22+
23+
fn test5(f: impl FnOnce(u32)) {
24+
take(f)
25+
//~^ ERROR [E0277]
26+
}
27+
28+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
error[E0277]: expected a `FnMut<(i32,)>` closure, found `impl FnMut(u32)`
2+
--> $DIR/mismatch-fn-trait.rs:4:10
3+
|
4+
LL | take(f)
5+
| ---- ^ expected an `FnMut<(i32,)>` closure, found `impl FnMut(u32)`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= note: expected a closure with arguments `(u32,)`
10+
found a closure with arguments `(i32,)`
11+
note: required by a bound in `take`
12+
--> $DIR/mismatch-fn-trait.rs:1:18
13+
|
14+
LL | fn take(_f: impl FnMut(i32)) {}
15+
| ^^^^^^^^^^ required by this bound in `take`
16+
17+
error[E0277]: expected a `FnMut<(i32,)>` closure, found `impl FnMut(i32, i32)`
18+
--> $DIR/mismatch-fn-trait.rs:9:10
19+
|
20+
LL | take(f)
21+
| ---- ^ expected an `FnMut<(i32,)>` closure, found `impl FnMut(i32, i32)`
22+
| |
23+
| required by a bound introduced by this call
24+
|
25+
= note: expected a closure taking 2 arguments, but one taking 1 argument was given
26+
note: required by a bound in `take`
27+
--> $DIR/mismatch-fn-trait.rs:1:18
28+
|
29+
LL | fn take(_f: impl FnMut(i32)) {}
30+
| ^^^^^^^^^^ required by this bound in `take`
31+
32+
error[E0277]: expected a `FnMut<(i32,)>` closure, found `impl FnMut()`
33+
--> $DIR/mismatch-fn-trait.rs:14:10
34+
|
35+
LL | take(f)
36+
| ---- ^ expected an `FnMut<(i32,)>` closure, found `impl FnMut()`
37+
| |
38+
| required by a bound introduced by this call
39+
|
40+
= note: expected a closure taking 0 arguments, but one taking 1 argument was given
41+
note: required by a bound in `take`
42+
--> $DIR/mismatch-fn-trait.rs:1:18
43+
|
44+
LL | fn take(_f: impl FnMut(i32)) {}
45+
| ^^^^^^^^^^ required by this bound in `take`
46+
47+
error[E0277]: expected a `FnMut<(i32,)>` closure, found `impl FnOnce(i32)`
48+
--> $DIR/mismatch-fn-trait.rs:19:10
49+
|
50+
LL | take(f)
51+
| ---- ^ expected an `FnMut<(i32,)>` closure, found `impl FnOnce(i32)`
52+
| |
53+
| required by a bound introduced by this call
54+
|
55+
= note: `impl FnOnce(i32)` implements `FnOnce`, but it must implement `FnMut`, which is more general
56+
note: required by a bound in `take`
57+
--> $DIR/mismatch-fn-trait.rs:1:18
58+
|
59+
LL | fn take(_f: impl FnMut(i32)) {}
60+
| ^^^^^^^^^^ required by this bound in `take`
61+
62+
error[E0277]: expected a `FnMut<(i32,)>` closure, found `impl FnOnce(u32)`
63+
--> $DIR/mismatch-fn-trait.rs:24:10
64+
|
65+
LL | take(f)
66+
| ---- ^ expected an `FnMut<(i32,)>` closure, found `impl FnOnce(u32)`
67+
| |
68+
| required by a bound introduced by this call
69+
|
70+
= note: `impl FnOnce(u32)` implements `FnOnce`, but it must implement `FnMut`, which is more general
71+
= note: expected a closure with arguments `(u32,)`
72+
found a closure with arguments `(i32,)`
73+
note: required by a bound in `take`
74+
--> $DIR/mismatch-fn-trait.rs:1:18
75+
|
76+
LL | fn take(_f: impl FnMut(i32)) {}
77+
| ^^^^^^^^^^ required by this bound in `take`
78+
79+
error: aborting due to 5 previous errors
80+
81+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/unboxed-closures/unboxed-closures-fnmut-as-fn.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LL | let x = call_it(&S, 22);
77
| required by a bound introduced by this call
88
|
99
= help: the trait `Fn<(isize,)>` is not implemented for `S`
10+
= note: `S` implements `FnMut`, but it must implement `Fn`, which is more general
1011
note: required by a bound in `call_it`
1112
--> $DIR/unboxed-closures-fnmut-as-fn.rs:22:14
1213
|

0 commit comments

Comments
 (0)