@@ -1778,18 +1778,20 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1778
1778
}
1779
1779
1780
1780
/// 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`.
1786
1784
/// Returns: a series of constraints as well as the region `R`
1787
1785
/// 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.
1788
1789
#[ instrument( skip( self , target_test) , ret) ]
1789
- pub ( crate ) fn find_constraint_paths_between_regions (
1790
+ pub ( crate ) fn find_constraint_path_to (
1790
1791
& self ,
1791
1792
from_region : RegionVid ,
1792
1793
target_test : impl Fn ( RegionVid ) -> bool ,
1794
+ include_static_outlives_all : bool ,
1793
1795
) -> Option < ( Vec < OutlivesConstraint < ' tcx > > , RegionVid ) > {
1794
1796
let mut context = IndexVec :: from_elem ( Trace :: NotVisited , & self . definitions ) ;
1795
1797
context[ from_region] = Trace :: StartRegion ;
@@ -1802,7 +1804,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1802
1804
1803
1805
while let Some ( r) = deque. pop_front ( ) {
1804
1806
debug ! (
1805
- "find_constraint_paths_between_regions : from_region={:?} r={:?} value={}" ,
1807
+ "find_constraint_path_to : from_region={:?} r={:?} value={}" ,
1806
1808
from_region,
1807
1809
r,
1808
1810
self . region_value_str( r) ,
@@ -1838,7 +1840,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1838
1840
1839
1841
// A constraint like `'r: 'x` can come from our constraint
1840
1842
// 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
+ } ;
1842
1848
let outgoing_edges_from_graph =
1843
1849
self . constraint_graph . outgoing_edges ( r, & self . constraints , fr_static) ;
1844
1850
@@ -1884,11 +1890,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1884
1890
pub ( crate ) fn find_sub_region_live_at ( & self , fr1 : RegionVid , location : Location ) -> RegionVid {
1885
1891
trace ! ( scc = ?self . constraint_sccs. scc( fr1) ) ;
1886
1892
trace ! ( universe = ?self . region_universe( fr1) ) ;
1887
- self . find_constraint_paths_between_regions ( fr1, |r| {
1893
+ self . find_constraint_path_to ( fr1, |r| {
1888
1894
// First look for some `r` such that `fr1: r` and `r` is live at `location`
1889
1895
trace ! ( ?r, liveness_constraints=?self . liveness_constraints. pretty_print_live_points( r) ) ;
1890
1896
self . liveness_constraints . is_live_at ( r, location)
1891
- } )
1897
+ } ,
1898
+ true
1899
+ )
1892
1900
. map ( |( _path, r) | r)
1893
1901
. unwrap ( )
1894
1902
}
@@ -1920,6 +1928,66 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1920
1928
self . universal_regions . as_ref ( )
1921
1929
}
1922
1930
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
+
1923
1991
/// Tries to find the best constraint to blame for the fact that
1924
1992
/// `to_region: from_region`.
1925
1993
/// This works by following the constraint graph,
@@ -1933,34 +2001,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1933
2001
from_region_origin : NllRegionVariableOrigin ,
1934
2002
to_region : RegionVid ,
1935
2003
) -> ( 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!" ) ;
1950
2005
1951
- result
1952
- }
2006
+ let ( path, new_to_region) = self . path_to_modulo_placeholders ( from_region, to_region) ;
1953
2007
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 ( ) ;
1964
2008
debug ! (
1965
2009
"path={:#?}" ,
1966
2010
path. iter( )
@@ -1973,24 +2017,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1973
2017
. collect:: <Vec <_>>( )
1974
2018
) ;
1975
2019
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) ;
1994
2021
1995
2022
// We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
1996
2023
// Instead, we use it to produce an improved `ObligationCauseCode`.
@@ -2041,7 +2068,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
2041
2068
// most likely to be the point where the value escapes -- but
2042
2069
// we still want to screen for an "interesting" point to
2043
2070
// 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 ) ;
2045
2072
let mut range = 0 ..path. len ( ) ;
2046
2073
2047
2074
// As noted above, when reporting an error, there is typically a chain of constraints
@@ -2238,6 +2265,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
2238
2265
fn scc_representative ( & self , scc : ConstraintSccIndex ) -> RegionVid {
2239
2266
self . constraint_sccs . annotation ( scc) . representative
2240
2267
}
2268
+
2269
+ /// Returns true if `r` is `'static`.
2270
+ fn is_static ( & self , r : RegionVid ) -> bool {
2271
+ r == self . universal_regions . fr_static
2272
+ }
2241
2273
}
2242
2274
2243
2275
impl < ' tcx > RegionDefinition < ' tcx > {
0 commit comments