From 955ba37aa597843abe5171fd80079f4f456785fb Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 11 Apr 2023 17:14:53 +1000 Subject: [PATCH 1/4] Remove the unused `anon_num_here` error. --- compiler/rustc_infer/messages.ftl | 1 - compiler/rustc_infer/src/errors/note_and_explain.rs | 2 -- 2 files changed, 3 deletions(-) diff --git a/compiler/rustc_infer/messages.ftl b/compiler/rustc_infer/messages.ftl index 4d4a7880b001..1ae53182046f 100644 --- a/compiler/rustc_infer/messages.ftl +++ b/compiler/rustc_infer/messages.ftl @@ -163,7 +163,6 @@ infer_region_explanation = {$pref_kind -> [as_defined] the lifetime `{$desc_arg}` as defined here [as_defined_anon] the anonymous lifetime as defined here [defined_here] the anonymous lifetime defined here - [anon_num_here] the anonymous lifetime #{$desc_num_arg} defined here [defined_here_reg] the lifetime `{$desc_arg}` as defined here }{$suff_kind -> *[should_not_happen] [{$suff_kind}] diff --git a/compiler/rustc_infer/src/errors/note_and_explain.rs b/compiler/rustc_infer/src/errors/note_and_explain.rs index 30f6af74b83e..744fb27635aa 100644 --- a/compiler/rustc_infer/src/errors/note_and_explain.rs +++ b/compiler/rustc_infer/src/errors/note_and_explain.rs @@ -9,7 +9,6 @@ struct DescriptionCtx<'a> { span: Option, kind: &'a str, arg: String, - num_arg: u32, } impl<'a> DescriptionCtx<'a> { @@ -113,7 +112,6 @@ impl<'a> DescriptionCtx<'a> { fn add_to(self, diag: &mut rustc_errors::Diagnostic) { diag.set_arg("desc_kind", self.kind); diag.set_arg("desc_arg", self.arg); - diag.set_arg("desc_num_arg", self.num_arg); } } From c9b0635679fc5bb4fd157e1eaad8e7f21705d68a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 11 Apr 2023 17:29:51 +1000 Subject: [PATCH 2/4] Inline and remove `DescriptionCtx::add_to`. It has a single callsite. --- compiler/rustc_infer/src/errors/note_and_explain.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_infer/src/errors/note_and_explain.rs b/compiler/rustc_infer/src/errors/note_and_explain.rs index 744fb27635aa..c4f2cf62ddda 100644 --- a/compiler/rustc_infer/src/errors/note_and_explain.rs +++ b/compiler/rustc_infer/src/errors/note_and_explain.rs @@ -108,11 +108,6 @@ impl<'a> DescriptionCtx<'a> { } Some(me) } - - fn add_to(self, diag: &mut rustc_errors::Diagnostic) { - diag.set_arg("desc_kind", self.kind); - diag.set_arg("desc_arg", self.arg); - } } pub enum PrefixKind { @@ -196,10 +191,11 @@ impl AddToDiagnostic for RegionExplanation<'_> { { diag.set_arg("pref_kind", self.prefix); diag.set_arg("suff_kind", self.suffix); - let desc_span = self.desc.span; - self.desc.add_to(diag); + diag.set_arg("desc_kind", self.desc.kind); + diag.set_arg("desc_arg", self.desc.arg); + let msg = f(diag, fluent::infer_region_explanation.into()); - if let Some(span) = desc_span { + if let Some(span) = self.desc.span { diag.span_note(span, msg); } else { diag.note(msg); From 877777f5bc0d02d409bbc6d68d8a7de8529254be Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 11 Apr 2023 17:14:41 +1000 Subject: [PATCH 3/4] Inline and remove `DescriptionCtx::from_early_bound_and_free_regions`. It has a single call site, and the code is clearer with all region kinds handled in one function, instead of splitting the handling across two functions. The commit also changes `DescriptionCtx::new` to use a more declarative style, instead of creating a default `DescriptionCtx` and modifying it, which I find easier to read. --- .../src/errors/note_and_explain.rs | 133 ++++++++---------- 1 file changed, 55 insertions(+), 78 deletions(-) diff --git a/compiler/rustc_infer/src/errors/note_and_explain.rs b/compiler/rustc_infer/src/errors/note_and_explain.rs index c4f2cf62ddda..7328241dfbca 100644 --- a/compiler/rustc_infer/src/errors/note_and_explain.rs +++ b/compiler/rustc_infer/src/errors/note_and_explain.rs @@ -4,7 +4,6 @@ use rustc_errors::{self, AddToDiagnostic, Diagnostic, IntoDiagnosticArg, Subdiag use rustc_middle::ty::{self, TyCtxt}; use rustc_span::{symbol::kw, Span}; -#[derive(Default)] struct DescriptionCtx<'a> { span: Option, kind: &'a str, @@ -17,19 +16,63 @@ impl<'a> DescriptionCtx<'a> { region: ty::Region<'tcx>, alt_span: Option, ) -> Option { - let mut me = DescriptionCtx::default(); - me.span = alt_span; - match *region { - ty::ReEarlyBound(_) | ty::ReFree(_) => { - return Self::from_early_bound_and_free_regions(tcx, region); + let (span, kind, arg) = match *region { + ty::ReEarlyBound(ref br) => { + let scope = region.free_region_binding_scope(tcx).expect_local(); + let span = if let Some(param) = + tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name)) + { + param.span + } else { + tcx.def_span(scope) + }; + if br.has_name() { + (Some(span), "as_defined", br.name.to_string()) + } else { + (Some(span), "as_defined_anon", String::new()) + } } - ty::ReStatic => { - me.kind = "restatic"; + ty::ReFree(ref fr) => { + if !fr.bound_region.is_named() + && let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) + { + (Some(ty.span), "defined_here", String::new()) + } else { + let scope = region.free_region_binding_scope(tcx).expect_local(); + match fr.bound_region { + ty::BoundRegionKind::BrNamed(_, name) => { + let span = if let Some(param) = tcx + .hir() + .get_generics(scope) + .and_then(|generics| generics.get_named(name)) + { + param.span + } else { + tcx.def_span(scope) + }; + if name == kw::UnderscoreLifetime { + (Some(span), "as_defined_anon", String::new()) + } else { + (Some(span), "as_defined", name.to_string()) + } + } + ty::BrAnon(span) => { + let span = match span { + Some(_) => span, + None => Some(tcx.def_span(scope)), + }; + (span, "defined_here", String::new()) + } + _ => { + (Some(tcx.def_span(scope)), "defined_here_reg", region.to_string()) + } + } + } } - ty::RePlaceholder(_) => return None, + ty::ReStatic => (alt_span, "restatic", String::new()), - ty::ReError(_) => return None, + ty::RePlaceholder(_) | ty::ReError(_) => return None, // FIXME(#13998) RePlaceholder should probably print like // ReFree rather than dumping Debug output on the user. @@ -37,76 +80,10 @@ impl<'a> DescriptionCtx<'a> { // We shouldn't really be having unification failures with ReVar // and ReLateBound though. ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => { - me.kind = "revar"; - me.arg = format!("{:?}", region); + (alt_span, "revar", format!("{:?}", region)) } }; - Some(me) - } - - fn from_early_bound_and_free_regions<'tcx>( - tcx: TyCtxt<'tcx>, - region: ty::Region<'tcx>, - ) -> Option { - let mut me = DescriptionCtx::default(); - let scope = region.free_region_binding_scope(tcx).expect_local(); - match *region { - ty::ReEarlyBound(ref br) => { - let mut sp = tcx.def_span(scope); - if let Some(param) = - tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name)) - { - sp = param.span; - } - if br.has_name() { - me.kind = "as_defined"; - me.arg = br.name.to_string(); - } else { - me.kind = "as_defined_anon"; - }; - me.span = Some(sp) - } - ty::ReFree(ref fr) => { - if !fr.bound_region.is_named() - && let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) - { - me.kind = "defined_here"; - me.span = Some(ty.span); - } else { - match fr.bound_region { - ty::BoundRegionKind::BrNamed(_, name) => { - let mut sp = tcx.def_span(scope); - if let Some(param) = - tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name)) - { - sp = param.span; - } - if name == kw::UnderscoreLifetime { - me.kind = "as_defined_anon"; - } else { - me.kind = "as_defined"; - me.arg = name.to_string(); - }; - me.span = Some(sp); - } - ty::BrAnon(span) => { - me.kind = "defined_here"; - me.span = match span { - Some(_) => span, - None => Some(tcx.def_span(scope)), - } - }, - _ => { - me.kind = "defined_here_reg"; - me.arg = region.to_string(); - me.span = Some(tcx.def_span(scope)); - }, - } - } - } - _ => bug!(), - } - Some(me) + Some(DescriptionCtx { span, kind, arg }) } } From 76d0c6f5186826d57d80bb5f2435454837c8a449 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 11 Apr 2023 18:50:52 +1000 Subject: [PATCH 4/4] Inline and remove `msg_span_from_early_bound_and_free_regions`. For similar reasons to the previous commit. --- .../src/infer/error_reporting/mod.rs | 114 ++++++++---------- 1 file changed, 52 insertions(+), 62 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 7901bc940212..b68ffaed69da 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -185,9 +185,58 @@ fn msg_span_from_named_region<'tcx>( alt_span: Option, ) -> (String, Option) { match *region { - ty::ReEarlyBound(_) | ty::ReFree(_) => { - let (msg, span) = msg_span_from_early_bound_and_free_regions(tcx, region); - (msg, Some(span)) + ty::ReEarlyBound(ref br) => { + let scope = region.free_region_binding_scope(tcx).expect_local(); + let span = if let Some(param) = + tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name)) + { + param.span + } else { + tcx.def_span(scope) + }; + let text = if br.has_name() { + format!("the lifetime `{}` as defined here", br.name) + } else { + "the anonymous lifetime as defined here".to_string() + }; + (text, Some(span)) + } + ty::ReFree(ref fr) => { + if !fr.bound_region.is_named() + && let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) + { + ("the anonymous lifetime defined here".to_string(), Some(ty.span)) + } else { + let scope = region.free_region_binding_scope(tcx).expect_local(); + match fr.bound_region { + ty::BoundRegionKind::BrNamed(_, name) => { + let span = if let Some(param) = + tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name)) + { + param.span + } else { + tcx.def_span(scope) + }; + let text = if name == kw::UnderscoreLifetime { + "the anonymous lifetime as defined here".to_string() + } else { + format!("the lifetime `{}` as defined here", name) + }; + (text, Some(span)) + } + ty::BrAnon(span) => ( + "the anonymous lifetime as defined here".to_string(), + Some(match span { + Some(span) => span, + None => tcx.def_span(scope) + }) + ), + _ => ( + format!("the lifetime `{}` as defined here", region), + Some(tcx.def_span(scope)), + ), + } + } } ty::ReStatic => ("the static lifetime".to_owned(), alt_span), ty::RePlaceholder(ty::PlaceholderRegion { @@ -206,65 +255,6 @@ fn msg_span_from_named_region<'tcx>( } } -fn msg_span_from_early_bound_and_free_regions<'tcx>( - tcx: TyCtxt<'tcx>, - region: ty::Region<'tcx>, -) -> (String, Span) { - let scope = region.free_region_binding_scope(tcx).expect_local(); - match *region { - ty::ReEarlyBound(ref br) => { - let mut sp = tcx.def_span(scope); - if let Some(param) = - tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name)) - { - sp = param.span; - } - let text = if br.has_name() { - format!("the lifetime `{}` as defined here", br.name) - } else { - "the anonymous lifetime as defined here".to_string() - }; - (text, sp) - } - ty::ReFree(ref fr) => { - if !fr.bound_region.is_named() - && let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) - { - ("the anonymous lifetime defined here".to_string(), ty.span) - } else { - match fr.bound_region { - ty::BoundRegionKind::BrNamed(_, name) => { - let mut sp = tcx.def_span(scope); - if let Some(param) = - tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name)) - { - sp = param.span; - } - let text = if name == kw::UnderscoreLifetime { - "the anonymous lifetime as defined here".to_string() - } else { - format!("the lifetime `{}` as defined here", name) - }; - (text, sp) - } - ty::BrAnon(span) => ( - "the anonymous lifetime as defined here".to_string(), - match span { - Some(span) => span, - None => tcx.def_span(scope) - } - ), - _ => ( - format!("the lifetime `{}` as defined here", region), - tcx.def_span(scope), - ), - } - } - } - _ => bug!(), - } -} - fn emit_msg_span( err: &mut Diagnostic, prefix: &str,