Skip to content

Commit b7444c5

Browse files
committed
fnmutBumpfn
1 parent 5a8c285 commit b7444c5

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_span::symbol::{kw, Symbol};
2222
use rustc_span::{sym, BytePos, Span};
2323

2424
use crate::diagnostics::BorrowedContentSource;
25-
use crate::session_diagnostics::ShowMutatingUpvar;
25+
use crate::session_diagnostics::{FnMutBumpFn, ShowMutatingUpvar};
2626
use crate::MirBorrowckCtxt;
2727
use rustc_const_eval::util::collect_writes::FindAssignments;
2828

@@ -971,7 +971,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
971971

972972
/// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected.
973973
fn expected_fn_found_fn_mut_call(&self, err: &mut Diagnostic, sp: Span, act: &str) {
974-
err.span_label(sp, format!("cannot {act}"));
974+
err.subdiagnostic(FnMutBumpFn::Cannot { act, sp });
975975

976976
let hir = self.infcx.tcx.hir();
977977
let closure_id = self.mir_hir_id();
@@ -1024,9 +1024,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10241024
_ => None,
10251025
};
10261026
if let Some(span) = arg {
1027-
err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
1028-
err.span_label(func.span, "expects `Fn` instead of `FnMut`");
1029-
err.span_label(self.body.span, "in this closure");
1027+
err.subdiagnostic(FnMutBumpFn::NotFnHere { span });
1028+
err.subdiagnostic(FnMutBumpFn::NotFnMutHere { span: func.span });
1029+
err.subdiagnostic(FnMutBumpFn::Here { span: self.body.span });
10301030
look_at_return = false;
10311031
}
10321032
}

compiler/rustc_borrowck/src/session_diagnostics.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,28 @@ pub(crate) enum ShowMutatingUpvar {
168168
span: Span,
169169
},
170170
}
171+
172+
#[derive(SessionSubdiagnostic)]
173+
pub(crate) enum FnMutBumpFn<'a> {
174+
#[label(borrowck::cannot_act)]
175+
Cannot {
176+
act: &'a str,
177+
#[primary_span]
178+
sp: Span,
179+
},
180+
#[label(borrowck::expects_fnmut_not_fn)]
181+
NotFnHere {
182+
#[primary_span]
183+
span: Span,
184+
},
185+
#[label(borrowck::expects_fn_not_fnmut)]
186+
NotFnMutHere {
187+
#[primary_span]
188+
span: Span,
189+
},
190+
#[label(borrowck::in_this_closure)]
191+
Here {
192+
#[primary_span]
193+
span: Span,
194+
},
195+
}

compiler/rustc_error_messages/locales/en-US/borrowck.ftl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,16 @@ borrowck_used_impl_require_static =
6161
6262
borrowck_require_mutable_binding =
6363
calling `{$place}` requires mutable binding due to {$reason}
64+
65+
borrowck_cannot_act =
66+
cannot {$act}
67+
68+
borrowck_expects_fnmut_not_fn =
69+
change this to accept `FnMut` instead of `Fn`
70+
71+
borrowck_expects_fn_not_fnmut =
72+
expects `Fn` instead of `FnMut`
73+
74+
borrowck_in_this_closure =
75+
in this closure
76+

0 commit comments

Comments
 (0)