@@ -1780,18 +1780,20 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1780
1780
}
1781
1781
1782
1782
/// Walks the graph of constraints (where `'a: 'b` is considered
1783
- /// an edge `'a -> 'b`) to find all paths from `from_region` to
1784
- /// `to_region`. The paths are accumulated into the vector
1785
- /// `results`. The paths are stored as a series of
1786
- /// `ConstraintIndex` values -- in other words, a list of *edges*.
1787
- ///
1783
+ /// an edge `'a -> 'b`) to find a path from `from_region` to
1784
+ /// the first region `R` for which the predicate function
1785
+ /// `target_test` returns `true`.
1788
1786
/// Returns: a series of constraints as well as the region `R`
1789
1787
/// that passed the target test.
1788
+ /// If `include_static_outlives_all` is `true`, then the synthetic
1789
+ /// outlives constraints `'static -> a` for every region `a` are
1790
+ /// considered in the search, otherwise they are ignored.
1790
1791
#[ instrument( skip( self , target_test) , ret) ]
1791
- pub ( crate ) fn find_constraint_paths_between_regions (
1792
+ pub ( crate ) fn find_constraint_path_to (
1792
1793
& self ,
1793
1794
from_region : RegionVid ,
1794
1795
target_test : impl Fn ( RegionVid ) -> bool ,
1796
+ include_static_outlives_all : bool ,
1795
1797
) -> Option < ( Vec < OutlivesConstraint < ' tcx > > , RegionVid ) > {
1796
1798
let mut context = IndexVec :: from_elem ( Trace :: NotVisited , & self . definitions ) ;
1797
1799
context[ from_region] = Trace :: StartRegion ;
@@ -1804,7 +1806,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1804
1806
1805
1807
while let Some ( r) = deque. pop_front ( ) {
1806
1808
debug ! (
1807
- "find_constraint_paths_between_regions : from_region={:?} r={:?} value={}" ,
1809
+ "find_constraint_path_to : from_region={:?} r={:?} value={}" ,
1808
1810
from_region,
1809
1811
r,
1810
1812
self . region_value_str( r) ,
@@ -1840,7 +1842,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1840
1842
1841
1843
// A constraint like `'r: 'x` can come from our constraint
1842
1844
// graph.
1843
- let fr_static = self . universal_regions . fr_static ;
1845
+ let fr_static = if include_static_outlives_all {
1846
+ Some ( self . universal_regions . fr_static )
1847
+ } else {
1848
+ None
1849
+ } ;
1844
1850
let outgoing_edges_from_graph =
1845
1851
self . constraint_graph . outgoing_edges ( r, & self . constraints , fr_static) ;
1846
1852
@@ -1886,11 +1892,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1886
1892
pub ( crate ) fn find_sub_region_live_at ( & self , fr1 : RegionVid , location : Location ) -> RegionVid {
1887
1893
trace ! ( scc = ?self . constraint_sccs. scc( fr1) ) ;
1888
1894
trace ! ( universe = ?self . region_universe( fr1) ) ;
1889
- self . find_constraint_paths_between_regions ( fr1, |r| {
1895
+ self . find_constraint_path_to ( fr1, |r| {
1890
1896
// First look for some `r` such that `fr1: r` and `r` is live at `location`
1891
1897
trace ! ( ?r, liveness_constraints=?self . liveness_constraints. pretty_print_live_points( r) ) ;
1892
1898
self . liveness_constraints . is_live_at ( r, location)
1893
- } )
1899
+ } ,
1900
+ true
1901
+ )
1894
1902
. map ( |( _path, r) | r)
1895
1903
. unwrap ( )
1896
1904
}
@@ -1922,6 +1930,66 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1922
1930
self . universal_regions . as_ref ( )
1923
1931
}
1924
1932
1933
+ /// Find a path of outlives constraints from `from` to `to`,
1934
+ /// taking placeholder blame constraints into account, e.g.
1935
+ /// if there is a relationship where `r1` reaches `r2` and
1936
+ /// r2 has a larger universe or if r1 and r2 both come from
1937
+ /// placeholder regions.
1938
+ ///
1939
+ /// Returns the path and the target region, which may or may
1940
+ /// not be the original `to`. It panics if there is no such
1941
+ /// path.
1942
+ fn path_to_modulo_placeholders (
1943
+ & self ,
1944
+ from : RegionVid ,
1945
+ to : RegionVid ,
1946
+ ) -> ( Vec < OutlivesConstraint < ' tcx > > , RegionVid ) {
1947
+ let path = self . find_constraint_path_to ( from, |r| r == to, true ) . unwrap ( ) . 0 ;
1948
+
1949
+ // If we are looking for a path to 'static, and we are passing
1950
+ // through a constraint synthesised from an illegal placeholder
1951
+ // relation, redirect the search to the placeholder to blame.
1952
+ if self . is_static ( to) {
1953
+ for constraint in path. iter ( ) {
1954
+ let ConstraintCategory :: IllegalPlaceholder ( culprit_r) = constraint. category else {
1955
+ continue ;
1956
+ } ;
1957
+
1958
+ debug ! ( "{culprit_r:?} is the reason {from:?}: 'static!" ) ;
1959
+ // FIXME: think: this may be for transitive reasons and
1960
+ // we may have to do this arbitrarily many times. Or may we?
1961
+ return self . find_constraint_path_to ( from, |r| r == culprit_r, false ) . unwrap ( ) ;
1962
+ }
1963
+ }
1964
+ // No funny business; just return the path!
1965
+ ( path, to)
1966
+ }
1967
+
1968
+ /// Find interesting spans from bound placeholders' predicates
1969
+ /// from a constraint path.
1970
+ fn find_bound_region_predicate_span (
1971
+ & self ,
1972
+ path : & [ OutlivesConstraint < ' _ > ] ,
1973
+ ) -> Vec < ExtraConstraintInfo > {
1974
+ for constraint in path. iter ( ) {
1975
+ let outlived = constraint. sub ;
1976
+ let Some ( origin) = self . var_infos . get ( outlived) else {
1977
+ continue ;
1978
+ } ;
1979
+ let RegionVariableOrigin :: Nll ( NllRegionVariableOrigin :: Placeholder ( p) ) = origin. origin
1980
+ else {
1981
+ continue ;
1982
+ } ;
1983
+ debug ! ( ?constraint, ?p) ;
1984
+ let ConstraintCategory :: Predicate ( span) = constraint. category else {
1985
+ continue ;
1986
+ } ;
1987
+ // We only want to point to one
1988
+ return vec ! [ ExtraConstraintInfo :: PlaceholderFromPredicate ( span) ] ;
1989
+ }
1990
+ vec ! [ ]
1991
+ }
1992
+
1925
1993
/// Tries to find the best constraint to blame for the fact that
1926
1994
/// `to_region: from_region`.
1927
1995
/// This works by following the constraint graph,
@@ -1935,34 +2003,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1935
2003
from_region_origin : NllRegionVariableOrigin ,
1936
2004
to_region : RegionVid ,
1937
2005
) -> ( BlameConstraint < ' tcx > , Vec < ExtraConstraintInfo > ) {
1938
- let result = self . best_blame_constraint_ ( from_region, from_region_origin, to_region) ;
1939
-
1940
- // We are trying to blame an outlives-static constraint added
1941
- // by an issue with placeholder regions. We figure out why the placeholder
1942
- // region issue happened instead.
1943
- if let ConstraintCategory :: IllegalPlaceholder ( offending_r) = result. 0 . category {
1944
- debug ! ( "best_blame_constraint: placeholder issue caused by {offending_r:?}" ) ;
1945
-
1946
- if to_region == offending_r {
1947
- // We do not want an infinite loop.
1948
- return result;
1949
- }
1950
- return self . best_blame_constraint ( from_region, from_region_origin, offending_r) ;
1951
- }
2006
+ assert ! ( from_region != to_region, "Trying to blame a region for itself!" ) ;
1952
2007
1953
- result
1954
- }
2008
+ let ( path, new_to_region) = self . path_to_modulo_placeholders ( from_region, to_region) ;
1955
2009
1956
- #[ instrument( level = "debug" , skip( self ) ) ]
1957
- pub ( crate ) fn best_blame_constraint_ (
1958
- & self ,
1959
- from_region : RegionVid ,
1960
- from_region_origin : NllRegionVariableOrigin ,
1961
- to_region : RegionVid ,
1962
- ) -> ( BlameConstraint < ' tcx > , Vec < ExtraConstraintInfo > ) {
1963
- // Find all paths
1964
- let ( path, target_region) =
1965
- self . find_constraint_paths_between_regions ( from_region, |r| r == to_region) . unwrap ( ) ;
1966
2010
debug ! (
1967
2011
"path={:#?}" ,
1968
2012
path. iter( )
@@ -1975,24 +2019,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1975
2019
. collect:: <Vec <_>>( )
1976
2020
) ;
1977
2021
1978
- let mut extra_info = vec ! [ ] ;
1979
- for constraint in path. iter ( ) {
1980
- let outlived = constraint. sub ;
1981
- let Some ( origin) = self . var_infos . get ( outlived) else {
1982
- continue ;
1983
- } ;
1984
- let RegionVariableOrigin :: Nll ( NllRegionVariableOrigin :: Placeholder ( p) ) = origin. origin
1985
- else {
1986
- continue ;
1987
- } ;
1988
- debug ! ( ?constraint, ?p) ;
1989
- let ConstraintCategory :: Predicate ( span) = constraint. category else {
1990
- continue ;
1991
- } ;
1992
- extra_info. push ( ExtraConstraintInfo :: PlaceholderFromPredicate ( span) ) ;
1993
- // We only want to point to one
1994
- break ;
1995
- }
2022
+ let extra_info = self . find_bound_region_predicate_span ( & path) ;
1996
2023
1997
2024
// We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
1998
2025
// Instead, we use it to produce an improved `ObligationCauseCode`.
@@ -2043,7 +2070,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
2043
2070
// most likely to be the point where the value escapes -- but
2044
2071
// we still want to screen for an "interesting" point to
2045
2072
// highlight (e.g., a call site or something).
2046
- let target_scc = self . constraint_sccs . scc ( target_region ) ;
2073
+ let target_scc = self . constraint_sccs . scc ( new_to_region ) ;
2047
2074
let mut range = 0 ..path. len ( ) ;
2048
2075
2049
2076
// As noted above, when reporting an error, there is typically a chain of constraints
@@ -2240,6 +2267,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
2240
2267
fn scc_representative ( & self , scc : ConstraintSccIndex ) -> RegionVid {
2241
2268
self . constraint_sccs . annotation ( scc) . representative
2242
2269
}
2270
+
2271
+ /// Returns true if `r` is `'static`.
2272
+ fn is_static ( & self , r : RegionVid ) -> bool {
2273
+ r == self . universal_regions . fr_static
2274
+ }
2243
2275
}
2244
2276
2245
2277
impl < ' tcx > RegionDefinition < ' tcx > {
0 commit comments