@@ -1778,18 +1778,20 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17781778 }
17791779
17801780 /// Walks the graph of constraints (where `'a: 'b` is considered
1781- /// an edge `'a -> 'b`) to find all paths from `from_region` to
1782- /// `to_region`. The paths are accumulated into the vector
1783- /// `results`. The paths are stored as a series of
1784- /// `ConstraintIndex` values -- in other words, a list of *edges*.
1785- ///
1781+ /// an edge `'a -> 'b`) to find a path from `from_region` to
1782+ /// the first region `R` for which the predicate function
1783+ /// `target_test` returns `true`.
17861784 /// Returns: a series of constraints as well as the region `R`
17871785 /// that passed the target test.
1786+ /// If `include_static_outlives_all` is `true`, then the synthetic
1787+ /// outlives constraints `'static -> a` for every region `a` are
1788+ /// considered in the search, otherwise they are ignored.
17881789 #[ instrument( skip( self , target_test) , ret) ]
1789- pub ( crate ) fn find_constraint_paths_between_regions (
1790+ pub ( crate ) fn find_constraint_path_to (
17901791 & self ,
17911792 from_region : RegionVid ,
17921793 target_test : impl Fn ( RegionVid ) -> bool ,
1794+ include_static_outlives_all : bool ,
17931795 ) -> Option < ( Vec < OutlivesConstraint < ' tcx > > , RegionVid ) > {
17941796 let mut context = IndexVec :: from_elem ( Trace :: NotVisited , & self . definitions ) ;
17951797 context[ from_region] = Trace :: StartRegion ;
@@ -1802,7 +1804,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18021804
18031805 while let Some ( r) = deque. pop_front ( ) {
18041806 debug ! (
1805- "find_constraint_paths_between_regions : from_region={:?} r={:?} value={}" ,
1807+ "find_constraint_path_to : from_region={:?} r={:?} value={}" ,
18061808 from_region,
18071809 r,
18081810 self . region_value_str( r) ,
@@ -1838,7 +1840,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18381840
18391841 // A constraint like `'r: 'x` can come from our constraint
18401842 // graph.
1841- let fr_static = self . universal_regions . fr_static ;
1843+ let fr_static = if include_static_outlives_all {
1844+ Some ( self . universal_regions . fr_static )
1845+ } else {
1846+ None
1847+ } ;
18421848 let outgoing_edges_from_graph =
18431849 self . constraint_graph . outgoing_edges ( r, & self . constraints , fr_static) ;
18441850
@@ -1884,11 +1890,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18841890 pub ( crate ) fn find_sub_region_live_at ( & self , fr1 : RegionVid , location : Location ) -> RegionVid {
18851891 trace ! ( scc = ?self . constraint_sccs. scc( fr1) ) ;
18861892 trace ! ( universe = ?self . region_universe( fr1) ) ;
1887- self . find_constraint_paths_between_regions ( fr1, |r| {
1893+ self . find_constraint_path_to ( fr1, |r| {
18881894 // First look for some `r` such that `fr1: r` and `r` is live at `location`
18891895 trace ! ( ?r, liveness_constraints=?self . liveness_constraints. pretty_print_live_points( r) ) ;
18901896 self . liveness_constraints . is_live_at ( r, location)
1891- } )
1897+ } ,
1898+ true
1899+ )
18921900 . map ( |( _path, r) | r)
18931901 . unwrap ( )
18941902 }
@@ -1920,6 +1928,66 @@ impl<'tcx> RegionInferenceContext<'tcx> {
19201928 self . universal_regions . as_ref ( )
19211929 }
19221930
1931+ /// Find a path of outlives constraints from `from` to `to`,
1932+ /// taking placeholder blame constraints into account, e.g.
1933+ /// if there is a relationship where `r1` reaches `r2` and
1934+ /// r2 has a larger universe or if r1 and r2 both come from
1935+ /// placeholder regions.
1936+ ///
1937+ /// Returns the path and the target region, which may or may
1938+ /// not be the original `to`. It panics if there is no such
1939+ /// path.
1940+ fn path_to_modulo_placeholders (
1941+ & self ,
1942+ from : RegionVid ,
1943+ to : RegionVid ,
1944+ ) -> ( Vec < OutlivesConstraint < ' tcx > > , RegionVid ) {
1945+ let path = self . find_constraint_path_to ( from, |r| r == to, true ) . unwrap ( ) . 0 ;
1946+
1947+ // If we are looking for a path to 'static, and we are passing
1948+ // through a constraint synthesised from an illegal placeholder
1949+ // relation, redirect the search to the placeholder to blame.
1950+ if self . is_static ( to) {
1951+ for constraint in path. iter ( ) {
1952+ let ConstraintCategory :: IllegalPlaceholder ( culprit_r) = constraint. category else {
1953+ continue ;
1954+ } ;
1955+
1956+ debug ! ( "{culprit_r:?} is the reason {from:?}: 'static!" ) ;
1957+ // FIXME: think: this may be for transitive reasons and
1958+ // we may have to do this arbitrarily many times. Or may we?
1959+ return self . find_constraint_path_to ( from, |r| r == culprit_r, false ) . unwrap ( ) ;
1960+ }
1961+ }
1962+ // No funny business; just return the path!
1963+ ( path, to)
1964+ }
1965+
1966+ /// Find interesting spans from bound placeholders' predicates
1967+ /// from a constraint path.
1968+ fn find_bound_region_predicate_span (
1969+ & self ,
1970+ path : & [ OutlivesConstraint < ' _ > ] ,
1971+ ) -> Vec < ExtraConstraintInfo > {
1972+ for constraint in path. iter ( ) {
1973+ let outlived = constraint. sub ;
1974+ let Some ( origin) = self . var_infos . get ( outlived) else {
1975+ continue ;
1976+ } ;
1977+ let RegionVariableOrigin :: Nll ( NllRegionVariableOrigin :: Placeholder ( p) ) = origin. origin
1978+ else {
1979+ continue ;
1980+ } ;
1981+ debug ! ( ?constraint, ?p) ;
1982+ let ConstraintCategory :: Predicate ( span) = constraint. category else {
1983+ continue ;
1984+ } ;
1985+ // We only want to point to one
1986+ return vec ! [ ExtraConstraintInfo :: PlaceholderFromPredicate ( span) ] ;
1987+ }
1988+ vec ! [ ]
1989+ }
1990+
19231991 /// Tries to find the best constraint to blame for the fact that
19241992 /// `to_region: from_region`.
19251993 /// This works by following the constraint graph,
@@ -1933,34 +2001,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
19332001 from_region_origin : NllRegionVariableOrigin ,
19342002 to_region : RegionVid ,
19352003 ) -> ( BlameConstraint < ' tcx > , Vec < ExtraConstraintInfo > ) {
1936- let result = self . best_blame_constraint_ ( from_region, from_region_origin, to_region) ;
1937-
1938- // We are trying to blame an outlives-static constraint added
1939- // by an issue with placeholder regions. We figure out why the placeholder
1940- // region issue happened instead.
1941- if let ConstraintCategory :: IllegalPlaceholder ( offending_r) = result. 0 . category {
1942- debug ! ( "best_blame_constraint: placeholder issue caused by {offending_r:?}" ) ;
1943-
1944- if to_region == offending_r {
1945- // We do not want an infinite loop.
1946- return result;
1947- }
1948- return self . best_blame_constraint ( from_region, from_region_origin, offending_r) ;
1949- }
2004+ assert ! ( from_region != to_region, "Trying to blame a region for itself!" ) ;
19502005
1951- result
1952- }
2006+ let ( path, new_to_region) = self . path_to_modulo_placeholders ( from_region, to_region) ;
19532007
1954- #[ instrument( level = "debug" , skip( self ) ) ]
1955- pub ( crate ) fn best_blame_constraint_ (
1956- & self ,
1957- from_region : RegionVid ,
1958- from_region_origin : NllRegionVariableOrigin ,
1959- to_region : RegionVid ,
1960- ) -> ( BlameConstraint < ' tcx > , Vec < ExtraConstraintInfo > ) {
1961- // Find all paths
1962- let ( path, target_region) =
1963- self . find_constraint_paths_between_regions ( from_region, |r| r == to_region) . unwrap ( ) ;
19642008 debug ! (
19652009 "path={:#?}" ,
19662010 path. iter( )
@@ -1973,24 +2017,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
19732017 . collect:: <Vec <_>>( )
19742018 ) ;
19752019
1976- let mut extra_info = vec ! [ ] ;
1977- for constraint in path. iter ( ) {
1978- let outlived = constraint. sub ;
1979- let Some ( origin) = self . var_infos . get ( outlived) else {
1980- continue ;
1981- } ;
1982- let RegionVariableOrigin :: Nll ( NllRegionVariableOrigin :: Placeholder ( p) ) = origin. origin
1983- else {
1984- continue ;
1985- } ;
1986- debug ! ( ?constraint, ?p) ;
1987- let ConstraintCategory :: Predicate ( span) = constraint. category else {
1988- continue ;
1989- } ;
1990- extra_info. push ( ExtraConstraintInfo :: PlaceholderFromPredicate ( span) ) ;
1991- // We only want to point to one
1992- break ;
1993- }
2020+ let extra_info = self . find_bound_region_predicate_span ( & path) ;
19942021
19952022 // We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
19962023 // Instead, we use it to produce an improved `ObligationCauseCode`.
@@ -2041,7 +2068,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20412068 // most likely to be the point where the value escapes -- but
20422069 // we still want to screen for an "interesting" point to
20432070 // highlight (e.g., a call site or something).
2044- let target_scc = self . constraint_sccs . scc ( target_region ) ;
2071+ let target_scc = self . constraint_sccs . scc ( new_to_region ) ;
20452072 let mut range = 0 ..path. len ( ) ;
20462073
20472074 // As noted above, when reporting an error, there is typically a chain of constraints
@@ -2238,6 +2265,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22382265 fn scc_representative ( & self , scc : ConstraintSccIndex ) -> RegionVid {
22392266 self . constraint_sccs . annotation ( scc) . representative
22402267 }
2268+
2269+ /// Returns true if `r` is `'static`.
2270+ fn is_static ( & self , r : RegionVid ) -> bool {
2271+ r == self . universal_regions . fr_static
2272+ }
22412273}
22422274
22432275impl < ' tcx > RegionDefinition < ' tcx > {
0 commit comments