@@ -29,11 +29,10 @@ use rustc::util::common;
2929use rustc_data_structures:: graph:: scc:: Sccs ;
3030use rustc_data_structures:: indexed_set:: IdxSet ;
3131use rustc_data_structures:: indexed_vec:: IndexVec ;
32- use rustc_errors:: Diagnostic ;
32+ use rustc_errors:: { DiagnosticBuilder , Diagnostic } ;
3333
3434use std:: rc:: Rc ;
3535
36- mod annotation;
3736mod dump_mir;
3837mod error_reporting;
3938mod graphviz;
@@ -359,6 +358,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
359358 self . universal_regions . to_region_vid ( r)
360359 }
361360
361+ /// Add annotations for `#[rustc_regions]`; see `UniversalRegions::annotate`.
362+ crate fn annotate ( & self , tcx : TyCtxt < ' _ , ' _ , ' tcx > , err : & mut DiagnosticBuilder < ' _ > ) {
363+ self . universal_regions . annotate ( tcx, err)
364+ }
365+
362366 /// Returns true if the region `r` contains the point `p`.
363367 ///
364368 /// Panics if called before `solve()` executes,
@@ -686,7 +690,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
686690 test : _,
687691 } = type_test;
688692
689-
690693 let generic_ty = generic_kind. to_ty ( tcx) ;
691694 let subject = match self . try_promote_type_test_subject ( infcx, generic_ty) {
692695 Some ( s) => s,
@@ -698,20 +701,38 @@ impl<'tcx> RegionInferenceContext<'tcx> {
698701 // `ClosureOutlivesRequirement`.
699702 let r_scc = self . constraint_sccs . scc ( * lower_bound) ;
700703 for ur in self . scc_values . universal_regions_outlived_by ( r_scc) {
704+ // Check whether we can already prove that the "subject" outlives `ur`.
705+ // If so, we don't have to propagate this requirement to our caller.
706+ //
707+ // To continue the example from the function, if we are trying to promote
708+ // a requirement that `T: 'X`, and we know that `'X = '1 + '2` (i.e., the union
709+ // `'1` and `'2`), then in this loop `ur` will be `'1` (and `'2`). So here
710+ // we check whether `T: '1` is something we *can* prove. If so, no need
711+ // to propagate that requirement.
712+ //
713+ // This is needed because -- particularly in the case
714+ // where `ur` is a local bound -- we are sometimes in a
715+ // position to prove things that our caller cannot. See
716+ // #53570 for an example.
717+ if self . eval_region_test ( mir, ur, & type_test. test ) {
718+ continue ;
719+ }
720+
721+ debug ! ( "try_promote_type_test: ur={:?}" , ur) ;
722+
701723 let non_local_ub = self . universal_region_relations . non_local_upper_bound ( ur) ;
724+ debug ! ( "try_promote_type_test: non_local_ub={:?}" , non_local_ub) ;
702725
703726 assert ! ( self . universal_regions. is_universal_region( non_local_ub) ) ;
704- assert ! (
705- !self
706- . universal_regions
707- . is_local_free_region( non_local_ub)
708- ) ;
727+ assert ! ( !self . universal_regions. is_local_free_region( non_local_ub) ) ;
709728
710- propagated_outlives_requirements . push ( ClosureOutlivesRequirement {
729+ let requirement = ClosureOutlivesRequirement {
711730 subject,
712731 outlived_free_region : non_local_ub,
713732 blame_span : locations. span ( mir) ,
714- } ) ;
733+ } ;
734+ debug ! ( "try_promote_type_test: pushing {:#?}" , requirement) ;
735+ propagated_outlives_requirements. push ( requirement) ;
715736 }
716737 true
717738 }
@@ -917,8 +938,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
917938 // now). Therefore, the sup-region outlives the sub-region if,
918939 // for each universal region R1 in the sub-region, there
919940 // exists some region R2 in the sup-region that outlives R1.
920- let universal_outlives = self
921- . scc_values
941+ let universal_outlives = self . scc_values
922942 . universal_regions_outlived_by ( sub_region_scc)
923943 . all ( |r1| {
924944 self . scc_values
@@ -1029,8 +1049,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10291049 // (because `fr` includes `end(o)`).
10301050 for shorter_fr in self . scc_values . universal_regions_outlived_by ( longer_fr_scc) {
10311051 // If it is known that `fr: o`, carry on.
1032- if self
1033- . universal_region_relations
1052+ if self . universal_region_relations
10341053 . outlives ( longer_fr, shorter_fr)
10351054 {
10361055 continue ;
@@ -1046,8 +1065,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10461065 if let Some ( propagated_outlives_requirements) = propagated_outlives_requirements {
10471066 // Shrink `fr` until we find a non-local region (if we do).
10481067 // We'll call that `fr-` -- it's ever so slightly smaller than `fr`.
1049- if let Some ( fr_minus) = self
1050- . universal_region_relations
1068+ if let Some ( fr_minus) = self . universal_region_relations
10511069 . non_local_lower_bound ( longer_fr)
10521070 {
10531071 debug ! ( "check_universal_region: fr_minus={:?}" , fr_minus) ;
@@ -1056,8 +1074,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10561074 // region. (We always will.) We'll call that
10571075 // `shorter_fr+` -- it's ever so slightly larger than
10581076 // `fr`.
1059- let shorter_fr_plus = self
1060- . universal_region_relations
1077+ let shorter_fr_plus = self . universal_region_relations
10611078 . non_local_upper_bound ( shorter_fr) ;
10621079 debug ! (
10631080 "check_universal_region: shorter_fr_plus={:?}" ,
@@ -1117,8 +1134,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
11171134 let error_region = match error_element {
11181135 RegionElement :: Location ( l) => self . find_sub_region_live_at ( longer_fr, l) ,
11191136 RegionElement :: RootUniversalRegion ( r) => r,
1120- RegionElement :: SubUniversalRegion ( error_ui) => self
1121- . definitions
1137+ RegionElement :: SubUniversalRegion ( error_ui) => self . definitions
11221138 . iter_enumerated ( )
11231139 . filter_map ( |( r, definition) | match definition. origin {
11241140 NLLRegionVariableOrigin :: BoundRegion ( ui) if error_ui == ui => Some ( r) ,
@@ -1215,7 +1231,11 @@ impl<'gcx, 'tcx> ClosureRegionRequirementsExt<'gcx, 'tcx> for ClosureRegionRequi
12151231 // into a vector. These are the regions that we will be
12161232 // relating to one another.
12171233 let closure_mapping = & UniversalRegions :: closure_mapping (
1218- tcx, user_closure_ty, self . num_external_vids , tcx. closure_base_def_id ( closure_def_id) ) ;
1234+ tcx,
1235+ user_closure_ty,
1236+ self . num_external_vids ,
1237+ tcx. closure_base_def_id ( closure_def_id) ,
1238+ ) ;
12191239 debug ! ( "apply_requirements: closure_mapping={:?}" , closure_mapping) ;
12201240
12211241 // Create the predicates.
0 commit comments