From 5a8c28541b74247f8ee68984cfc0994b5fd2c3f2 Mon Sep 17 00:00:00 2001 From: AndyJado <101876416+AndyJado@users.noreply.github.com> Date: Thu, 1 Sep 2022 20:50:15 +0800 Subject: [PATCH 1/4] init --- .../src/diagnostics/mutability_errors.rs | 15 +++++++-------- .../rustc_borrowck/src/session_diagnostics.rs | 11 +++++++++++ .../locales/en-US/borrowck.ftl | 3 +++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index dd9590016b990..a9f6a4df9209f 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -1,3 +1,6 @@ +// #![deny(rustc::untranslatable_diagnostic)] +// #![deny(rustc::diagnostic_outside_of_impl)] + use rustc_errors::{ Applicability, Diagnostic, DiagnosticBuilder, EmissionGuarantee, ErrorGuaranteed, }; @@ -19,6 +22,7 @@ use rustc_span::symbol::{kw, Symbol}; use rustc_span::{sym, BytePos, Span}; use crate::diagnostics::BorrowedContentSource; +use crate::session_diagnostics::ShowMutatingUpvar; use crate::MirBorrowckCtxt; use rustc_const_eval::util::collect_writes::FindAssignments; @@ -864,14 +868,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { } else { bug!("not an upvar") }; - err.span_label( - *span, - format!( - "calling `{}` requires mutable binding due to {}", - self.describe_place(the_place_err).unwrap(), - reason - ), - ); + let place = self.describe_place(the_place_err).unwrap(); + let sub_label = ShowMutatingUpvar::RequireMutableBinding { place, reason, span: *span }; + err.subdiagnostic(sub_label); } } diff --git a/compiler/rustc_borrowck/src/session_diagnostics.rs b/compiler/rustc_borrowck/src/session_diagnostics.rs index 5d750c6ca8c7b..6aea649466906 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -157,3 +157,14 @@ pub(crate) enum RequireStaticErr { multi_span: MultiSpan, }, } +//mutability_errors.rs +#[derive(SessionSubdiagnostic)] +pub(crate) enum ShowMutatingUpvar { + #[label(borrowck::require_mutable_binding)] + RequireMutableBinding { + place: String, + reason: String, + #[primary_span] + span: Span, + }, +} diff --git a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl index 67f2156f32e50..3b5e00cd516f9 100644 --- a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl @@ -58,3 +58,6 @@ borrowck_returned_lifetime_short = borrowck_used_impl_require_static = the used `impl` has a `'static` requirement + +borrowck_require_mutable_binding = + calling `{$place}` requires mutable binding due to {$reason} From b7444c51bf17520d5b76118f415ec9a6187470e2 Mon Sep 17 00:00:00 2001 From: AndyJado <101876416+AndyJado@users.noreply.github.com> Date: Thu, 1 Sep 2022 21:21:09 +0800 Subject: [PATCH 2/4] fnmutBumpfn --- .../src/diagnostics/mutability_errors.rs | 10 ++++---- .../rustc_borrowck/src/session_diagnostics.rs | 25 +++++++++++++++++++ .../locales/en-US/borrowck.ftl | 13 ++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index a9f6a4df9209f..1cf04ce496bdc 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -22,7 +22,7 @@ use rustc_span::symbol::{kw, Symbol}; use rustc_span::{sym, BytePos, Span}; use crate::diagnostics::BorrowedContentSource; -use crate::session_diagnostics::ShowMutatingUpvar; +use crate::session_diagnostics::{FnMutBumpFn, ShowMutatingUpvar}; use crate::MirBorrowckCtxt; use rustc_const_eval::util::collect_writes::FindAssignments; @@ -971,7 +971,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { /// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected. fn expected_fn_found_fn_mut_call(&self, err: &mut Diagnostic, sp: Span, act: &str) { - err.span_label(sp, format!("cannot {act}")); + err.subdiagnostic(FnMutBumpFn::Cannot { act, sp }); let hir = self.infcx.tcx.hir(); let closure_id = self.mir_hir_id(); @@ -1024,9 +1024,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { _ => None, }; if let Some(span) = arg { - err.span_label(span, "change this to accept `FnMut` instead of `Fn`"); - err.span_label(func.span, "expects `Fn` instead of `FnMut`"); - err.span_label(self.body.span, "in this closure"); + err.subdiagnostic(FnMutBumpFn::NotFnHere { span }); + err.subdiagnostic(FnMutBumpFn::NotFnMutHere { span: func.span }); + err.subdiagnostic(FnMutBumpFn::Here { span: self.body.span }); look_at_return = false; } } diff --git a/compiler/rustc_borrowck/src/session_diagnostics.rs b/compiler/rustc_borrowck/src/session_diagnostics.rs index 6aea649466906..176851e39570d 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -168,3 +168,28 @@ pub(crate) enum ShowMutatingUpvar { span: Span, }, } + +#[derive(SessionSubdiagnostic)] +pub(crate) enum FnMutBumpFn<'a> { + #[label(borrowck::cannot_act)] + Cannot { + act: &'a str, + #[primary_span] + sp: Span, + }, + #[label(borrowck::expects_fnmut_not_fn)] + NotFnHere { + #[primary_span] + span: Span, + }, + #[label(borrowck::expects_fn_not_fnmut)] + NotFnMutHere { + #[primary_span] + span: Span, + }, + #[label(borrowck::in_this_closure)] + Here { + #[primary_span] + span: Span, + }, +} diff --git a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl index 3b5e00cd516f9..16d4239b897e6 100644 --- a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl @@ -61,3 +61,16 @@ borrowck_used_impl_require_static = borrowck_require_mutable_binding = calling `{$place}` requires mutable binding due to {$reason} + +borrowck_cannot_act = + cannot {$act} + +borrowck_expects_fnmut_not_fn = + change this to accept `FnMut` instead of `Fn` + +borrowck_expects_fn_not_fnmut = + expects `Fn` instead of `FnMut` + +borrowck_in_this_closure = + in this closure + From ceca8c33584ed08f080ac3370a4e788c5db018d8 Mon Sep 17 00:00:00 2001 From: AndyJado <101876416+AndyJado@users.noreply.github.com> Date: Thu, 1 Sep 2022 21:29:50 +0800 Subject: [PATCH 3/4] remain case: empty fluent string --- .../src/diagnostics/mutability_errors.rs | 12 +++++------- compiler/rustc_borrowck/src/session_diagnostics.rs | 9 +++++++-- .../rustc_error_messages/locales/en-US/borrowck.ftl | 2 ++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 1cf04ce496bdc..858f6579347bb 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -1024,8 +1024,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { _ => None, }; if let Some(span) = arg { - err.subdiagnostic(FnMutBumpFn::NotFnHere { span }); - err.subdiagnostic(FnMutBumpFn::NotFnMutHere { span: func.span }); + err.subdiagnostic(FnMutBumpFn::AcceptFnMut { span }); + err.subdiagnostic(FnMutBumpFn::AcceptFn { span: func.span }); err.subdiagnostic(FnMutBumpFn::Here { span: self.body.span }); look_at_return = false; } @@ -1047,12 +1047,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { kind: hir::ImplItemKind::Fn(sig, _), .. }) => { + //FIXME: empty string here don't pass lint err.span_label(ident.span, ""); - err.span_label( - sig.decl.output.span(), - "change this to return `FnMut` instead of `Fn`", - ); - err.span_label(self.body.span, "in this closure"); + err.subdiagnostic(FnMutBumpFn::ReturnFnMut { span: sig.decl.output.span() }); + err.subdiagnostic(FnMutBumpFn::Here { span: self.body.span }); } _ => {} } diff --git a/compiler/rustc_borrowck/src/session_diagnostics.rs b/compiler/rustc_borrowck/src/session_diagnostics.rs index 176851e39570d..10d34f9a93d53 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -178,12 +178,12 @@ pub(crate) enum FnMutBumpFn<'a> { sp: Span, }, #[label(borrowck::expects_fnmut_not_fn)] - NotFnHere { + AcceptFnMut { #[primary_span] span: Span, }, #[label(borrowck::expects_fn_not_fnmut)] - NotFnMutHere { + AcceptFn { #[primary_span] span: Span, }, @@ -192,4 +192,9 @@ pub(crate) enum FnMutBumpFn<'a> { #[primary_span] span: Span, }, + #[label(borrowck::return_fnmut)] + ReturnFnMut { + #[primary_span] + span: Span, + }, } diff --git a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl index 16d4239b897e6..70d84703cb558 100644 --- a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl @@ -74,3 +74,5 @@ borrowck_expects_fn_not_fnmut = borrowck_in_this_closure = in this closure +borrowck_return_fnmut = + change this to return `FnMut` instead of `Fn` From 8e04a1c1fc7ac593dd6cd19357a6cd0e417f0b17 Mon Sep 17 00:00:00 2001 From: AndyJado <101876416+AndyJado@users.noreply.github.com> Date: Sat, 3 Sep 2022 11:53:17 +0800 Subject: [PATCH 4/4] empty_label --- .../rustc_borrowck/src/diagnostics/mutability_errors.rs | 7 +++---- compiler/rustc_borrowck/src/session_diagnostics.rs | 7 ++++++- compiler/rustc_error_messages/locales/en-US/borrowck.ftl | 2 ++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 858f6579347bb..61c5676b4053a 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -1,5 +1,5 @@ -// #![deny(rustc::untranslatable_diagnostic)] -// #![deny(rustc::diagnostic_outside_of_impl)] +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] use rustc_errors::{ Applicability, Diagnostic, DiagnosticBuilder, EmissionGuarantee, ErrorGuaranteed, @@ -1047,8 +1047,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { kind: hir::ImplItemKind::Fn(sig, _), .. }) => { - //FIXME: empty string here don't pass lint - err.span_label(ident.span, ""); + err.subdiagnostic(FnMutBumpFn::EmptyLabel { span: ident.span }); err.subdiagnostic(FnMutBumpFn::ReturnFnMut { span: sig.decl.output.span() }); err.subdiagnostic(FnMutBumpFn::Here { span: self.body.span }); } diff --git a/compiler/rustc_borrowck/src/session_diagnostics.rs b/compiler/rustc_borrowck/src/session_diagnostics.rs index 10d34f9a93d53..a63bbfa32a1f8 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -157,7 +157,7 @@ pub(crate) enum RequireStaticErr { multi_span: MultiSpan, }, } -//mutability_errors.rs + #[derive(SessionSubdiagnostic)] pub(crate) enum ShowMutatingUpvar { #[label(borrowck::require_mutable_binding)] @@ -187,6 +187,11 @@ pub(crate) enum FnMutBumpFn<'a> { #[primary_span] span: Span, }, + #[label(borrowck::empty_label)] + EmptyLabel { + #[primary_span] + span: Span, + }, #[label(borrowck::in_this_closure)] Here { #[primary_span] diff --git a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl index 70d84703cb558..3ef282daffb13 100644 --- a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl @@ -71,6 +71,8 @@ borrowck_expects_fnmut_not_fn = borrowck_expects_fn_not_fnmut = expects `Fn` instead of `FnMut` +borrowck_empty_label = {""} + borrowck_in_this_closure = in this closure