From 422fde3f9eb2db7b7165b9f688e2894c5fbaa138 Mon Sep 17 00:00:00 2001 From: AndyJado <101876416+AndyJado@users.noreply.github.com> Date: Thu, 1 Sep 2022 17:49:06 +0800 Subject: [PATCH 1/2] lifetime defined here err --- compiler/rustc_borrowck/src/diagnostics/region_name.rs | 7 ++++++- compiler/rustc_borrowck/src/session_diagnostics.rs | 10 ++++++++++ .../rustc_error_messages/locales/en-US/borrowck.ftl | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) 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..0da5678a34865 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -157,3 +157,13 @@ 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, + }, +} diff --git a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl index 67f2156f32e50..8e8380c4da2ca 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_lifetime_defined_here = + lifetime `{$rg_name}` defined here From c770ac03f1566e02184c06c0ef9e929b09f01da0 Mon Sep 17 00:00:00 2001 From: AndyJado <101876416+AndyJado@users.noreply.github.com> Date: Thu, 1 Sep 2022 20:30:13 +0800 Subject: [PATCH 2/2] explain_borrow bug --- .../src/diagnostics/explain_borrow.rs | 16 ++++++++-------- .../rustc_borrowck/src/session_diagnostics.rs | 12 ++++++++++++ .../locales/en-US/borrowck.ftl | 3 +++ 3 files changed, 23 insertions(+), 8 deletions(-) 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/session_diagnostics.rs b/compiler/rustc_borrowck/src/session_diagnostics.rs index 0da5678a34865..202c70e39f05d 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -167,3 +167,15 @@ pub(crate) enum RegionNameLables { 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 8e8380c4da2ca..ba9500a2426f9 100644 --- a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl @@ -61,3 +61,6 @@ borrowck_used_impl_require_static = 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}`