@@ -1776,13 +1776,73 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, F> {
1776
1776
}
1777
1777
}
1778
1778
1779
+ /// Folds through bound vars and placeholders, naming them
1780
+ struct RegionFolder < ' a , ' tcx > {
1781
+ tcx : TyCtxt < ' tcx > ,
1782
+ current_index : ty:: DebruijnIndex ,
1783
+ region_map : BTreeMap < ty:: BoundRegion , ty:: Region < ' tcx > > ,
1784
+ name : & ' a mut ( dyn FnMut ( ty:: BoundRegion ) -> ty:: Region < ' tcx > + ' a ) ,
1785
+ }
1786
+
1787
+ impl < ' a , ' tcx > ty:: TypeFolder < ' tcx > for RegionFolder < ' a , ' tcx > {
1788
+ fn tcx < ' b > ( & ' b self ) -> TyCtxt < ' tcx > {
1789
+ self . tcx
1790
+ }
1791
+
1792
+ fn fold_binder < T : TypeFoldable < ' tcx > > (
1793
+ & mut self ,
1794
+ t : ty:: Binder < ' tcx , T > ,
1795
+ ) -> ty:: Binder < ' tcx , T > {
1796
+ self . current_index . shift_in ( 1 ) ;
1797
+ let t = t. super_fold_with ( self ) ;
1798
+ self . current_index . shift_out ( 1 ) ;
1799
+ t
1800
+ }
1801
+
1802
+ fn fold_ty ( & mut self , t : Ty < ' tcx > ) -> Ty < ' tcx > {
1803
+ match * t. kind ( ) {
1804
+ _ if t. has_vars_bound_at_or_above ( self . current_index ) || t. has_placeholders ( ) => {
1805
+ return t. super_fold_with ( self ) ;
1806
+ }
1807
+ _ => { }
1808
+ }
1809
+ t
1810
+ }
1811
+
1812
+ fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
1813
+ let name = & mut self . name ;
1814
+ let region = match * r {
1815
+ ty:: ReLateBound ( _, br) => self . region_map . entry ( br) . or_insert_with ( || name ( br) ) ,
1816
+ ty:: RePlaceholder ( ty:: PlaceholderRegion { name : kind, .. } ) => {
1817
+ // If this is an anonymous placeholder, don't rename. Otherwise, in some
1818
+ // async fns, we get a `for<'r> Send` bound
1819
+ match kind {
1820
+ ty:: BrAnon ( _) | ty:: BrEnv => r,
1821
+ _ => {
1822
+ // Index doesn't matter, since this is just for naming and these never get bound
1823
+ let br = ty:: BoundRegion { var : ty:: BoundVar :: from_u32 ( 0 ) , kind } ;
1824
+ self . region_map . entry ( br) . or_insert_with ( || name ( br) )
1825
+ }
1826
+ }
1827
+ }
1828
+ _ => return r,
1829
+ } ;
1830
+ if let ty:: ReLateBound ( debruijn1, br) = * region {
1831
+ assert_eq ! ( debruijn1, ty:: INNERMOST ) ;
1832
+ self . tcx . mk_region ( ty:: ReLateBound ( self . current_index , br) )
1833
+ } else {
1834
+ region
1835
+ }
1836
+ }
1837
+ }
1838
+
1779
1839
// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
1780
1840
// `region_index` and `used_region_names`.
1781
1841
impl < F : fmt:: Write > FmtPrinter < ' _ , ' tcx , F > {
1782
1842
pub fn name_all_regions < T > (
1783
1843
mut self ,
1784
1844
value : & ty:: Binder < ' tcx , T > ,
1785
- ) -> Result < ( Self , ( T , BTreeMap < ty:: BoundRegion , ty:: Region < ' tcx > > ) ) , fmt:: Error >
1845
+ ) -> Result < ( Self , T , BTreeMap < ty:: BoundRegion , ty:: Region < ' tcx > > ) , fmt:: Error >
1786
1846
where
1787
1847
T : Print < ' tcx , Self , Output = Self , Error = fmt:: Error > + TypeFoldable < ' tcx > ,
1788
1848
{
@@ -1805,16 +1865,16 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
1805
1865
1806
1866
let mut empty = true ;
1807
1867
let mut start_or_continue = |cx : & mut Self , start : & str , cont : & str | {
1808
- write ! (
1809
- cx ,
1810
- "{}" ,
1811
- if empty {
1812
- empty = false ;
1813
- start
1814
- } else {
1815
- cont
1816
- }
1817
- )
1868
+ let w = if empty {
1869
+ empty = false ;
1870
+ start
1871
+ } else {
1872
+ cont
1873
+ } ;
1874
+ let _ = write ! ( cx , "{}" , w ) ;
1875
+ } ;
1876
+ let do_continue = | cx : & mut Self , cont : Symbol | {
1877
+ let _ = write ! ( cx , "{}" , cont ) ;
1818
1878
} ;
1819
1879
1820
1880
define_scoped_cx ! ( self ) ;
@@ -1824,44 +1884,44 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
1824
1884
// aren't named. Eventually, we might just want this as the default, but
1825
1885
// this is not *quite* right and changes the ordering of some output
1826
1886
// anyways.
1827
- let new_value = if self . tcx ( ) . sess . verbose ( ) {
1887
+ let ( new_value, map ) = if self . tcx ( ) . sess . verbose ( ) {
1828
1888
// anon index + 1 (BrEnv takes 0) -> name
1829
1889
let mut region_map: BTreeMap < u32 , Symbol > = BTreeMap :: default ( ) ;
1830
1890
let bound_vars = value. bound_vars ( ) ;
1831
1891
for var in bound_vars {
1832
1892
match var {
1833
1893
ty:: BoundVariableKind :: Region ( ty:: BrNamed ( _, name) ) => {
1834
- let _ = start_or_continue ( & mut self , "for<" , ", " ) ;
1835
- let _ = write ! ( self , "{}" , name) ;
1894
+ start_or_continue ( & mut self , "for<" , ", " ) ;
1895
+ do_continue ( & mut self , name) ;
1836
1896
}
1837
1897
ty:: BoundVariableKind :: Region ( ty:: BrAnon ( i) ) => {
1838
- let _ = start_or_continue ( & mut self , "for<" , ", " ) ;
1898
+ start_or_continue ( & mut self , "for<" , ", " ) ;
1839
1899
let name = loop {
1840
1900
let name = name_by_region_index ( region_index) ;
1841
1901
region_index += 1 ;
1842
1902
if !self . used_region_names . contains ( & name) {
1843
1903
break name;
1844
1904
}
1845
1905
} ;
1846
- let _ = write ! ( self , "{}" , name) ;
1906
+ do_continue ( & mut self , name) ;
1847
1907
region_map. insert ( i + 1 , name) ;
1848
1908
}
1849
1909
ty:: BoundVariableKind :: Region ( ty:: BrEnv ) => {
1850
- let _ = start_or_continue ( & mut self , "for<" , ", " ) ;
1910
+ start_or_continue ( & mut self , "for<" , ", " ) ;
1851
1911
let name = loop {
1852
1912
let name = name_by_region_index ( region_index) ;
1853
1913
region_index += 1 ;
1854
1914
if !self . used_region_names . contains ( & name) {
1855
1915
break name;
1856
1916
}
1857
1917
} ;
1858
- let _ = write ! ( self , "{}" , name) ;
1918
+ do_continue ( & mut self , name) ;
1859
1919
region_map. insert ( 0 , name) ;
1860
1920
}
1861
1921
_ => continue ,
1862
1922
}
1863
1923
}
1864
- start_or_continue ( & mut self , "" , "> " ) ? ;
1924
+ start_or_continue ( & mut self , "" , "> " ) ;
1865
1925
1866
1926
self . tcx . replace_late_bound_regions ( value. clone ( ) , |br| {
1867
1927
let kind = match br. kind {
@@ -1881,11 +1941,12 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
1881
1941
) )
1882
1942
} )
1883
1943
} else {
1884
- let new_value = self . tcx . replace_late_bound_regions ( value. clone ( ) , |br| {
1885
- let _ = start_or_continue ( & mut self , "for<" , ", " ) ;
1944
+ let tcx = self . tcx ;
1945
+ let mut name = |br : ty:: BoundRegion | {
1946
+ start_or_continue ( & mut self , "for<" , ", " ) ;
1886
1947
let kind = match br. kind {
1887
1948
ty:: BrNamed ( _, name) => {
1888
- let _ = write ! ( self , "{}" , name) ;
1949
+ do_continue ( & mut self , name) ;
1889
1950
br. kind
1890
1951
}
1891
1952
ty:: BrAnon ( _) | ty:: BrEnv => {
@@ -1896,31 +1957,36 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
1896
1957
break name;
1897
1958
}
1898
1959
} ;
1899
- let _ = write ! ( self , "{}" , name) ;
1960
+ do_continue ( & mut self , name) ;
1900
1961
ty:: BrNamed ( DefId :: local ( CRATE_DEF_INDEX ) , name)
1901
1962
}
1902
1963
} ;
1903
- self . tcx . mk_region ( ty:: ReLateBound (
1904
- ty:: INNERMOST ,
1905
- ty:: BoundRegion { var : br. var , kind } ,
1906
- ) )
1907
- } ) ;
1908
- start_or_continue ( & mut self , "" , "> " ) ?;
1909
- new_value
1964
+ tcx. mk_region ( ty:: ReLateBound ( ty:: INNERMOST , ty:: BoundRegion { var : br. var , kind } ) )
1965
+ } ;
1966
+ let mut folder = RegionFolder {
1967
+ tcx,
1968
+ current_index : ty:: INNERMOST ,
1969
+ name : & mut name,
1970
+ region_map : BTreeMap :: new ( ) ,
1971
+ } ;
1972
+ let new_value = value. clone ( ) . skip_binder ( ) . fold_with ( & mut folder) ;
1973
+ let region_map = folder. region_map ;
1974
+ start_or_continue ( & mut self , "" , "> " ) ;
1975
+ ( new_value, region_map)
1910
1976
} ;
1911
1977
1912
1978
self . binder_depth += 1 ;
1913
1979
self . region_index = region_index;
1914
- Ok ( ( self , new_value) )
1980
+ Ok ( ( self , new_value, map ) )
1915
1981
}
1916
1982
1917
1983
pub fn pretty_in_binder < T > ( self , value : & ty:: Binder < ' tcx , T > ) -> Result < Self , fmt:: Error >
1918
1984
where
1919
1985
T : Print < ' tcx , Self , Output = Self , Error = fmt:: Error > + TypeFoldable < ' tcx > ,
1920
1986
{
1921
1987
let old_region_index = self . region_index ;
1922
- let ( new, new_value) = self . name_all_regions ( value) ?;
1923
- let mut inner = new_value. 0 . print ( new) ?;
1988
+ let ( new, new_value, _ ) = self . name_all_regions ( value) ?;
1989
+ let mut inner = new_value. print ( new) ?;
1924
1990
inner. region_index = old_region_index;
1925
1991
inner. binder_depth -= 1 ;
1926
1992
Ok ( inner)
@@ -1935,8 +2001,8 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
1935
2001
T : Print < ' tcx , Self , Output = Self , Error = fmt:: Error > + TypeFoldable < ' tcx > ,
1936
2002
{
1937
2003
let old_region_index = self . region_index ;
1938
- let ( new, new_value) = self . name_all_regions ( value) ?;
1939
- let mut inner = f ( & new_value. 0 , new) ?;
2004
+ let ( new, new_value, _ ) = self . name_all_regions ( value) ?;
2005
+ let mut inner = f ( & new_value, new) ?;
1940
2006
inner. region_index = old_region_index;
1941
2007
inner. binder_depth -= 1 ;
1942
2008
Ok ( inner)
@@ -1960,6 +2026,12 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
1960
2026
debug ! ( "LateBoundRegionNameCollector::visit_region(r: {:?}, address: {:p})" , r, & r) ;
1961
2027
if let ty:: ReLateBound ( _, ty:: BoundRegion { kind : ty:: BrNamed ( _, name) , .. } ) = * r {
1962
2028
self . used_region_names . insert ( name) ;
2029
+ } else if let ty:: RePlaceholder ( ty:: PlaceholderRegion {
2030
+ name : ty:: BrNamed ( _, name) ,
2031
+ ..
2032
+ } ) = * r
2033
+ {
2034
+ self . used_region_names . insert ( name) ;
1963
2035
}
1964
2036
r. super_visit_with ( self )
1965
2037
}
0 commit comments