diff --git a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs index 2f61849c383c5..af8492ed04328 100644 --- a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs +++ b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs @@ -16,6 +16,7 @@ use rustc_span::symbol::{kw, Symbol}; use rustc_span::{sym, DesugaringKind, Span}; use crate::region_infer::BlameConstraint; +use crate::session_diagnostics::RequireBorrowLasts; use crate::{ borrow_set::BorrowData, nll::ConstraintDescription, region_infer::Cause, MirBorrowckCtxt, WriteKind, @@ -257,15 +258,14 @@ impl<'tcx> BorrowExplanation<'tcx> { ), ); } else { - err.span_label( + //FIXME: src/test/ui/consts/const-eval/const-eval-intrinsic-promotion.rs + let sub_label = RequireBorrowLasts::Lasts { + category: category.description(), + borrow_desc, + region_name, span, - format!( - "{}requires that {}borrow lasts for `{}`", - category.description(), - borrow_desc, - region_name, - ), - ); + }; + err.subdiagnostic(sub_label); }; self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name); diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index a87e8bd5ba16f..83f21d7b8e60a 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -10,6 +10,7 @@ use rustc_middle::ty::{self, DefIdTree, RegionVid, Ty}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; +use crate::session_diagnostics::RegionNameLables; use crate::{nll::ToRegionVid, universal_regions::DefiningTy, MirBorrowckCtxt}; /// A name for a particular region used in emitting diagnostics. This name could be a generated @@ -106,7 +107,11 @@ impl RegionName { match &self.source { RegionNameSource::NamedFreeRegion(span) | RegionNameSource::NamedEarlyBoundRegion(span) => { - diag.span_label(*span, format!("lifetime `{self}` defined here")); + let name = format!("{self}"); + diag.subdiagnostic(RegionNameLables::LifetimeDefinedHere { + span: *span, + rg_name: name, + }); } RegionNameSource::SynthesizedFreeEnvRegion(span, note) => { diag.span_label(*span, format!("lifetime `{self}` represents this closure's body")); diff --git a/compiler/rustc_borrowck/src/session_diagnostics.rs b/compiler/rustc_borrowck/src/session_diagnostics.rs index 5d750c6ca8c7b..202c70e39f05d 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -157,3 +157,25 @@ pub(crate) enum RequireStaticErr { multi_span: MultiSpan, }, } + +#[derive(SessionSubdiagnostic)] +pub(crate) enum RegionNameLables { + #[label(borrowck::lifetime_defined_here)] + LifetimeDefinedHere { + rg_name: String, + #[primary_span] + span: Span, + }, +} + +#[derive(SessionSubdiagnostic)] +pub(crate) enum RequireBorrowLasts<'a> { + #[label(borrowck::outlive_constraint_need_borrow_lasts_for)] + Lasts { + category: &'a str, + borrow_desc: &'a str, + region_name: &'a RegionName, + #[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..ba9500a2426f9 100644 --- a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl @@ -58,3 +58,9 @@ borrowck_returned_lifetime_short = borrowck_used_impl_require_static = the used `impl` has a `'static` requirement + +borrowck_lifetime_defined_here = + lifetime `{$rg_name}` defined here + +borrowck_outlive_constraint_need_borrow_lasts_for = + {$category}requires that `{$borrow_desc}` lasts for `{$region_name}`