From c27633489d038765922d996ebd59ba032007d25c Mon Sep 17 00:00:00 2001 From: SNCPlay42 Date: Tue, 21 Jul 2020 22:50:56 +0100 Subject: [PATCH 1/8] add RegionName::span --- .../borrow_check/diagnostics/region_errors.rs | 16 +++------------- .../borrow_check/diagnostics/region_name.rs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs index 26c2aea41d5dc..cc8a5e0768cba 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs @@ -19,7 +19,7 @@ use crate::borrow_check::{ MirBorrowckCtxt, }; -use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource}; +use super::{OutlivesSuggestionBuilder, RegionName}; impl ConstraintDescription for ConstraintCategory { fn description(&self) -> &'static str { @@ -396,18 +396,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { diag.span_label(upvar_span, "variable captured here"); } - match self.give_region_a_name(*outlived_fr).unwrap().source { - RegionNameSource::NamedEarlyBoundRegion(fr_span) - | RegionNameSource::NamedFreeRegion(fr_span) - | RegionNameSource::SynthesizedFreeEnvRegion(fr_span, _) - | RegionNameSource::CannotMatchHirTy(fr_span, _) - | RegionNameSource::MatchedHirTy(fr_span) - | RegionNameSource::MatchedAdtAndSegment(fr_span) - | RegionNameSource::AnonRegionFromUpvar(fr_span, _) - | RegionNameSource::AnonRegionFromOutput(fr_span, _, _) => { - diag.span_label(fr_span, "inferred to be a `FnMut` closure"); - } - _ => {} + if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() { + diag.span_label(fr_span, "inferred to be a `FnMut` closure"); } diag.note( diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 2240eb81e1fa7..81148d888ed9c 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -68,6 +68,22 @@ impl RegionName { } } + crate fn span(&self) -> Option { + match self.source { + RegionNameSource::Static => None, + RegionNameSource::NamedEarlyBoundRegion(span) + | RegionNameSource::NamedFreeRegion(span) + | RegionNameSource::SynthesizedFreeEnvRegion(span, _) + | RegionNameSource::CannotMatchHirTy(span, _) + | RegionNameSource::MatchedHirTy(span) + | RegionNameSource::MatchedAdtAndSegment(span) + | RegionNameSource::AnonRegionFromUpvar(span, _) + | RegionNameSource::AnonRegionFromOutput(span, _, _) + | RegionNameSource::AnonRegionFromYieldTy(span, _) + | RegionNameSource::AnonRegionFromAsyncFn(span) => Some(span), + } + } + crate fn highlight_region_name(&self, diag: &mut DiagnosticBuilder<'_>) { match &self.source { RegionNameSource::NamedFreeRegion(span) From 51af5af6fde92f6a15314310e756b3b2d19f22fa Mon Sep 17 00:00:00 2001 From: SNCPlay42 Date: Wed, 22 Jul 2020 19:44:33 +0100 Subject: [PATCH 2/8] extract RegionNameHighlight --- .../diagnostics/outlives_suggestion.rs | 4 +- .../borrow_check/diagnostics/region_name.rs | 59 ++++++++++++------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/librustc_mir/borrow_check/diagnostics/outlives_suggestion.rs b/src/librustc_mir/borrow_check/diagnostics/outlives_suggestion.rs index dd970d800fba0..8521f900988e4 100644 --- a/src/librustc_mir/borrow_check/diagnostics/outlives_suggestion.rs +++ b/src/librustc_mir/borrow_check/diagnostics/outlives_suggestion.rs @@ -60,9 +60,7 @@ impl OutlivesSuggestionBuilder { // Don't give suggestions for upvars, closure return types, or other unnamable // regions. RegionNameSource::SynthesizedFreeEnvRegion(..) - | RegionNameSource::CannotMatchHirTy(..) - | RegionNameSource::MatchedHirTy(..) - | RegionNameSource::MatchedAdtAndSegment(..) + | RegionNameSource::AnonRegionFromArgument(..) | RegionNameSource::AnonRegionFromUpvar(..) | RegionNameSource::AnonRegionFromOutput(..) | RegionNameSource::AnonRegionFromYieldTy(..) diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 81148d888ed9c..233f8158e2b76 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -34,13 +34,8 @@ crate enum RegionNameSource { Static, /// The free region corresponding to the environment of a closure. SynthesizedFreeEnvRegion(Span, String), - /// The region name corresponds to a region where the type annotation is completely missing - /// from the code, e.g. in a closure arguments `|x| { ... }`, where `x` is a reference. - CannotMatchHirTy(Span, String), - /// The region name corresponds a reference that was found by traversing the type in the HIR. - MatchedHirTy(Span), - /// A region name from the generics list of a struct/enum/union. - MatchedAdtAndSegment(Span), + /// The region corresponding to an argument. + AnonRegionFromArgument(RegionNameHighlight), /// The region corresponding to a closure upvar. AnonRegionFromUpvar(Span, String), /// The region corresponding to the return type of a closure. @@ -51,6 +46,19 @@ crate enum RegionNameSource { AnonRegionFromAsyncFn(Span), } +/// Describes what to highlight to explain to the user that we're giving an anonymous region a +/// synthesized name, and how to highlight it. +#[derive(Debug, Clone)] +crate enum RegionNameHighlight { + /// The anonymous region corresponds to a reference that was found by traversing the type in the HIR. + MatchedHirTy(Span), + /// The anonymous region corresponds to a `'_` in the generics list of a struct/enum/union. + MatchedAdtAndSegment(Span), + /// The anonymous region corresponds to a region where the type annotation is completely missing + /// from the code, e.g. in a closure arguments `|x| { ... }`, where `x` is a reference. + CannotMatchHirTy(Span, String), +} + impl RegionName { crate fn was_named(&self) -> bool { match self.source { @@ -58,9 +66,7 @@ impl RegionName { | RegionNameSource::NamedFreeRegion(..) | RegionNameSource::Static => true, RegionNameSource::SynthesizedFreeEnvRegion(..) - | RegionNameSource::CannotMatchHirTy(..) - | RegionNameSource::MatchedHirTy(..) - | RegionNameSource::MatchedAdtAndSegment(..) + | RegionNameSource::AnonRegionFromArgument(..) | RegionNameSource::AnonRegionFromUpvar(..) | RegionNameSource::AnonRegionFromOutput(..) | RegionNameSource::AnonRegionFromYieldTy(..) @@ -74,13 +80,15 @@ impl RegionName { RegionNameSource::NamedEarlyBoundRegion(span) | RegionNameSource::NamedFreeRegion(span) | RegionNameSource::SynthesizedFreeEnvRegion(span, _) - | RegionNameSource::CannotMatchHirTy(span, _) - | RegionNameSource::MatchedHirTy(span) - | RegionNameSource::MatchedAdtAndSegment(span) | RegionNameSource::AnonRegionFromUpvar(span, _) | RegionNameSource::AnonRegionFromOutput(span, _, _) | RegionNameSource::AnonRegionFromYieldTy(span, _) | RegionNameSource::AnonRegionFromAsyncFn(span) => Some(span), + RegionNameSource::AnonRegionFromArgument(ref highlight) => match *highlight { + RegionNameHighlight::MatchedHirTy(span) + | RegionNameHighlight::MatchedAdtAndSegment(span) + | RegionNameHighlight::CannotMatchHirTy(span, _) => Some(span), + }, } } @@ -97,17 +105,22 @@ impl RegionName { ); diag.note(¬e); } - RegionNameSource::CannotMatchHirTy(span, type_name) => { + RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::CannotMatchHirTy( + span, + type_name, + )) => { diag.span_label(*span, format!("has type `{}`", type_name)); } - RegionNameSource::MatchedHirTy(span) + RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::MatchedHirTy(span)) | RegionNameSource::AnonRegionFromAsyncFn(span) => { diag.span_label( *span, format!("let's call the lifetime of this reference `{}`", self), ); } - RegionNameSource::MatchedAdtAndSegment(span) => { + RegionNameSource::AnonRegionFromArgument( + RegionNameHighlight::MatchedAdtAndSegment(span), + ) => { diag.span_label(*span, format!("let's call this `{}`", self)); } RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => { @@ -393,7 +406,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { // it so the next value will be used next and return the region name that would // have been used. name: self.synthesize_region_name(), - source: RegionNameSource::CannotMatchHirTy(span, type_name), + source: RegionNameSource::AnonRegionFromArgument( + RegionNameHighlight::CannotMatchHirTy(span, type_name), + ), }) } else { None @@ -453,7 +468,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { return Some(RegionName { name: region_name, - source: RegionNameSource::MatchedHirTy(ampersand_span), + source: RegionNameSource::AnonRegionFromArgument( + RegionNameHighlight::MatchedHirTy(ampersand_span), + ), }); } @@ -534,10 +551,12 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { | hir::LifetimeName::Static | hir::LifetimeName::Underscore => { let region_name = self.synthesize_region_name(); - let ampersand_span = lifetime.span; + let lifetime_span = lifetime.span; Some(RegionName { name: region_name, - source: RegionNameSource::MatchedAdtAndSegment(ampersand_span), + source: RegionNameSource::AnonRegionFromArgument( + RegionNameHighlight::MatchedAdtAndSegment(lifetime_span), + ), }) } From 601518edcd3d570918bbfebfe71e7905dacd4482 Mon Sep 17 00:00:00 2001 From: SNCPlay42 Date: Wed, 22 Jul 2020 20:10:39 +0100 Subject: [PATCH 3/8] change returns to RegionNameHighlight --- .../borrow_check/diagnostics/region_name.rs | 67 ++++++++----------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 233f8158e2b76..0f199de330897 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -336,13 +336,27 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys [implicit_inputs + argument_index]; - if let Some(region_name) = + if let Some(highlight) = self.give_name_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index) { - return Some(region_name); + return Some(RegionName { + name: self.synthesize_region_name(), + source: RegionNameSource::AnonRegionFromArgument(highlight), + }); } - self.give_name_if_we_cannot_match_hir_ty(fr, arg_ty) + let counter = *self.next_region_name.try_borrow().unwrap(); + if let Some(highlight) = self.give_name_if_we_cannot_match_hir_ty(fr, arg_ty, counter) { + Some(RegionName { + // This counter value will already have been used, so this function will increment + // it so the next value will be used next and return the region name that would + // have been used. + name: self.synthesize_region_name(), + source: RegionNameSource::AnonRegionFromArgument(highlight), + }) + } else { + None + } } fn give_name_if_we_can_match_hir_ty_from_argument( @@ -350,7 +364,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { needle_fr: RegionVid, argument_ty: Ty<'tcx>, argument_index: usize, - ) -> Option { + ) -> Option { let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id); let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?; let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?; @@ -381,8 +395,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { &self, needle_fr: RegionVid, argument_ty: Ty<'tcx>, - ) -> Option { - let counter = *self.next_region_name.try_borrow().unwrap(); + counter: usize, + ) -> Option { let mut highlight = RegionHighlightMode::default(); highlight.highlighting_region_vid(needle_fr, counter); let type_name = self.infcx.extract_type_name(&argument_ty, Some(highlight)).0; @@ -391,7 +405,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { "give_name_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}", type_name, needle_fr ); - let assigned_region_name = if type_name.find(&format!("'{}", counter)).is_some() { + if type_name.find(&format!("'{}", counter)).is_some() { // Only add a label if we can confirm that a region was labelled. let argument_index = self.regioncx.get_argument_index_for_region(self.infcx.tcx, needle_fr)?; @@ -401,20 +415,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { argument_index, ); - Some(RegionName { - // This counter value will already have been used, so this function will increment - // it so the next value will be used next and return the region name that would - // have been used. - name: self.synthesize_region_name(), - source: RegionNameSource::AnonRegionFromArgument( - RegionNameHighlight::CannotMatchHirTy(span, type_name), - ), - }) + Some(RegionNameHighlight::CannotMatchHirTy(span, type_name)) } else { None - }; - - assigned_region_name + } } /// Attempts to highlight the specific part of a type annotation @@ -443,7 +447,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { needle_fr: RegionVid, argument_ty: Ty<'tcx>, argument_hir_ty: &hir::Ty<'_>, - ) -> Option { + ) -> Option { let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty<'_>)> = &mut vec![(argument_ty, argument_hir_ty)]; @@ -460,18 +464,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { hir::TyKind::Rptr(_lifetime, referent_hir_ty), ) => { if region.to_region_vid() == needle_fr { - let region_name = self.synthesize_region_name(); - // Just grab the first character, the `&`. let source_map = self.infcx.tcx.sess.source_map(); let ampersand_span = source_map.start_point(hir_ty.span); - return Some(RegionName { - name: region_name, - source: RegionNameSource::AnonRegionFromArgument( - RegionNameHighlight::MatchedHirTy(ampersand_span), - ), - }); + return Some(RegionNameHighlight::MatchedHirTy(ampersand_span)); } // Otherwise, let's descend into the referent types. @@ -491,13 +488,13 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { Res::Def(DefKind::TyAlias, _) => (), _ => { if let Some(last_segment) = path.segments.last() { - if let Some(name) = self.match_adt_and_segment( + if let Some(highlight) = self.match_adt_and_segment( substs, needle_fr, last_segment, search_stack, ) { - return Some(name); + return Some(highlight); } } } @@ -540,7 +537,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { needle_fr: RegionVid, last_segment: &'hir hir::PathSegment<'hir>, search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>, - ) -> Option { + ) -> Option { // Did the user give explicit arguments? (e.g., `Foo<..>`) let args = last_segment.args.as_ref()?; let lifetime = @@ -550,14 +547,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { | hir::LifetimeName::Error | hir::LifetimeName::Static | hir::LifetimeName::Underscore => { - let region_name = self.synthesize_region_name(); let lifetime_span = lifetime.span; - Some(RegionName { - name: region_name, - source: RegionNameSource::AnonRegionFromArgument( - RegionNameHighlight::MatchedAdtAndSegment(lifetime_span), - ), - }) + Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime_span)) } hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Implicit => { From 723ea909929498380615f56e652660229adcdfef Mon Sep 17 00:00:00 2001 From: SNCPlay42 Date: Wed, 22 Jul 2020 20:17:10 +0100 Subject: [PATCH 4/8] rename functions --- .../borrow_check/diagnostics/region_name.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 0f199de330897..9e5851fd81165 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -337,7 +337,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys [implicit_inputs + argument_index]; if let Some(highlight) = - self.give_name_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index) + self.highlight_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index) { return Some(RegionName { name: self.synthesize_region_name(), @@ -346,7 +346,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { } let counter = *self.next_region_name.try_borrow().unwrap(); - if let Some(highlight) = self.give_name_if_we_cannot_match_hir_ty(fr, arg_ty, counter) { + if let Some(highlight) = self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, counter) { Some(RegionName { // This counter value will already have been used, so this function will increment // it so the next value will be used next and return the region name that would @@ -359,7 +359,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { } } - fn give_name_if_we_can_match_hir_ty_from_argument( + fn highlight_if_we_can_match_hir_ty_from_argument( &self, needle_fr: RegionVid, argument_ty: Ty<'tcx>, @@ -376,7 +376,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { // (`give_name_if_anonymous_region_appears_in_arguments`). hir::TyKind::Infer => None, - _ => self.give_name_if_we_can_match_hir_ty(needle_fr, argument_ty, argument_hir_ty), + _ => self.highlight_if_we_can_match_hir_ty(needle_fr, argument_ty, argument_hir_ty), } } @@ -391,7 +391,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { /// | | has type `&'1 u32` /// | has type `&'2 u32` /// ``` - fn give_name_if_we_cannot_match_hir_ty( + fn highlight_if_we_cannot_match_hir_ty( &self, needle_fr: RegionVid, argument_ty: Ty<'tcx>, @@ -402,7 +402,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { let type_name = self.infcx.extract_type_name(&argument_ty, Some(highlight)).0; debug!( - "give_name_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}", + "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}", type_name, needle_fr ); if type_name.find(&format!("'{}", counter)).is_some() { @@ -442,7 +442,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { /// keep track of the **closest** type we've found. If we fail to /// find the exact `&` or `'_` to highlight, then we may fall back /// to highlighting that closest type instead. - fn give_name_if_we_can_match_hir_ty( + fn highlight_if_we_can_match_hir_ty( &self, needle_fr: RegionVid, argument_ty: Ty<'tcx>, From b56f5b9df5a41fbf3c82310b5c3d901b0b0f78dc Mon Sep 17 00:00:00 2001 From: SNCPlay42 Date: Wed, 22 Jul 2020 20:38:44 +0100 Subject: [PATCH 5/8] clean up give_name_if_anonymous_region_appears_in_arguments --- .../borrow_check/diagnostics/region_name.rs | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 9e5851fd81165..da86a0c3bb9d5 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -336,27 +336,18 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys [implicit_inputs + argument_index]; - if let Some(highlight) = - self.highlight_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index) - { - return Some(RegionName { - name: self.synthesize_region_name(), - source: RegionNameSource::AnonRegionFromArgument(highlight), - }); - } - - let counter = *self.next_region_name.try_borrow().unwrap(); - if let Some(highlight) = self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, counter) { - Some(RegionName { - // This counter value will already have been used, so this function will increment - // it so the next value will be used next and return the region name that would - // have been used. + self.highlight_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index) + .or_else(|| { + // `highlight_if_we_cannot_match_hir_ty` needs to know the number we will give to + // the anonymous region. If it succeeds, the `synthesize_region_name` call below + // will increment the counter, "reserving" the number we just used. + let counter = *self.next_region_name.try_borrow().unwrap(); + self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, counter) + }) + .map(|highlight| RegionName { name: self.synthesize_region_name(), source: RegionNameSource::AnonRegionFromArgument(highlight), }) - } else { - None - } } fn highlight_if_we_can_match_hir_ty_from_argument( From a7450b7989a5e94ec964a589826a1089d2e7108b Mon Sep 17 00:00:00 2001 From: SNCPlay42 Date: Wed, 22 Jul 2020 22:04:02 +0100 Subject: [PATCH 6/8] decouple highlight_if_we_cannot_match_hir_ty --- .../borrow_check/diagnostics/region_name.rs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index da86a0c3bb9d5..378713c52a2b4 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -336,13 +336,19 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys [implicit_inputs + argument_index]; + let (_, span) = self.regioncx.get_argument_name_and_span_for_region( + &self.body, + &self.local_names, + argument_index, + ); + self.highlight_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index) .or_else(|| { // `highlight_if_we_cannot_match_hir_ty` needs to know the number we will give to // the anonymous region. If it succeeds, the `synthesize_region_name` call below // will increment the counter, "reserving" the number we just used. let counter = *self.next_region_name.try_borrow().unwrap(); - self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, counter) + self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, span, counter) }) .map(|highlight| RegionName { name: self.synthesize_region_name(), @@ -385,12 +391,13 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { fn highlight_if_we_cannot_match_hir_ty( &self, needle_fr: RegionVid, - argument_ty: Ty<'tcx>, + ty: Ty<'tcx>, + span: Span, counter: usize, ) -> Option { let mut highlight = RegionHighlightMode::default(); highlight.highlighting_region_vid(needle_fr, counter); - let type_name = self.infcx.extract_type_name(&argument_ty, Some(highlight)).0; + let type_name = self.infcx.extract_type_name(&ty, Some(highlight)).0; debug!( "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}", @@ -398,13 +405,6 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { ); if type_name.find(&format!("'{}", counter)).is_some() { // Only add a label if we can confirm that a region was labelled. - let argument_index = - self.regioncx.get_argument_index_for_region(self.infcx.tcx, needle_fr)?; - let (_, span) = self.regioncx.get_argument_name_and_span_for_region( - &self.body, - &self.local_names, - argument_index, - ); Some(RegionNameHighlight::CannotMatchHirTy(span, type_name)) } else { From ebb4ababfc86bc34b42bae7607adedd28ed05b30 Mon Sep 17 00:00:00 2001 From: SNCPlay42 Date: Wed, 22 Jul 2020 22:31:18 +0100 Subject: [PATCH 7/8] move highlight_if_we_can_match_hir_ty call out of highlight_if_we_can_match_hir_ty_from_argument, which is then renamed --- .../borrow_check/diagnostics/region_name.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 378713c52a2b4..992df06e57919 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -342,7 +342,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { argument_index, ); - self.highlight_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index) + self.get_argument_hir_ty_for_highlighting(argument_index) + .and_then(|arg_hir_ty| self.highlight_if_we_can_match_hir_ty(fr, arg_ty, arg_hir_ty)) .or_else(|| { // `highlight_if_we_cannot_match_hir_ty` needs to know the number we will give to // the anonymous region. If it succeeds, the `synthesize_region_name` call below @@ -356,12 +357,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { }) } - fn highlight_if_we_can_match_hir_ty_from_argument( + fn get_argument_hir_ty_for_highlighting( &self, - needle_fr: RegionVid, - argument_ty: Ty<'tcx>, argument_index: usize, - ) -> Option { + ) -> Option<&hir::Ty<'tcx>> { let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id); let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?; let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?; @@ -373,7 +372,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { // (`give_name_if_anonymous_region_appears_in_arguments`). hir::TyKind::Infer => None, - _ => self.highlight_if_we_can_match_hir_ty(needle_fr, argument_ty, argument_hir_ty), + _ => Some(argument_hir_ty), } } From 8a776ee2390cb99f915cda22393ce4e297208396 Mon Sep 17 00:00:00 2001 From: SNCPlay42 Date: Thu, 23 Jul 2020 00:07:57 +0100 Subject: [PATCH 8/8] rename arguments to highlight_if_we_can_match_hir_ty --- .../borrow_check/diagnostics/region_name.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 992df06e57919..32195adc60ef8 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -420,9 +420,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { /// | - let's call the lifetime of this reference `'1` /// ``` /// - /// the way this works is that we match up `argument_ty`, which is + /// the way this works is that we match up `ty`, which is /// a `Ty<'tcx>` (the internal form of the type) with - /// `argument_hir_ty`, a `hir::Ty` (the syntax of the type + /// `hir_ty`, a `hir::Ty` (the syntax of the type /// annotation). We are descending through the types stepwise, /// looking in to find the region `needle_fr` in the internal /// type. Once we find that, we can use the span of the `hir::Ty` @@ -435,15 +435,14 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { fn highlight_if_we_can_match_hir_ty( &self, needle_fr: RegionVid, - argument_ty: Ty<'tcx>, - argument_hir_ty: &hir::Ty<'_>, + ty: Ty<'tcx>, + hir_ty: &hir::Ty<'_>, ) -> Option { - let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty<'_>)> = - &mut vec![(argument_ty, argument_hir_ty)]; + let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty<'_>)> = &mut vec![(ty, hir_ty)]; while let Some((ty, hir_ty)) = search_stack.pop() { match (&ty.kind, &hir_ty.kind) { - // Check if the `argument_ty` is `&'X ..` where `'X` + // Check if the `ty` is `&'X ..` where `'X` // is the region we are looking for -- if so, and we have a `&T` // on the RHS, then we want to highlight the `&` like so: //