@@ -37,12 +37,11 @@ use rustc_middle::hir::map::Map;
37
37
use rustc_middle:: middle:: codegen_fn_attrs:: { CodegenFnAttrFlags , CodegenFnAttrs } ;
38
38
use rustc_middle:: mir:: mono:: Linkage ;
39
39
use rustc_middle:: ty:: query:: Providers ;
40
- use rustc_middle:: ty:: subst:: { InternalSubsts , SubstsRef } ;
40
+ use rustc_middle:: ty:: subst:: InternalSubsts ;
41
41
use rustc_middle:: ty:: util:: Discr ;
42
42
use rustc_middle:: ty:: util:: IntTypeExt ;
43
43
use rustc_middle:: ty:: { self , AdtKind , Const , ToPolyTraitRef , Ty , TyCtxt } ;
44
44
use rustc_middle:: ty:: { ReprOptions , ToPredicate , WithConstness } ;
45
- use rustc_middle:: ty:: { TypeFoldable , TypeVisitor } ;
46
45
use rustc_session:: config:: SanitizerSet ;
47
46
use rustc_session:: lint;
48
47
use rustc_session:: parse:: feature_err;
@@ -51,8 +50,6 @@ use rustc_span::{Span, DUMMY_SP};
51
50
use rustc_target:: spec:: abi;
52
51
use rustc_trait_selection:: traits:: error_reporting:: suggestions:: NextTypeParamName ;
53
52
54
- use smallvec:: SmallVec ;
55
-
56
53
mod type_of;
57
54
58
55
struct OnlySelfBounds ( bool ) ;
@@ -1676,47 +1673,10 @@ fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicate
1676
1673
}
1677
1674
}
1678
1675
1679
- if tcx. features ( ) . const_evaluatable_checked {
1680
- let const_evaluatable = const_evaluatable_predicates_of ( tcx, def_id, & result) ;
1681
- result. predicates =
1682
- tcx. arena . alloc_from_iter ( result. predicates . iter ( ) . copied ( ) . chain ( const_evaluatable) ) ;
1683
- }
1684
-
1685
1676
debug ! ( "predicates_defined_on({:?}) = {:?}" , def_id, result) ;
1686
1677
result
1687
1678
}
1688
1679
1689
- pub fn const_evaluatable_predicates_of < ' tcx > (
1690
- tcx : TyCtxt < ' tcx > ,
1691
- def_id : DefId ,
1692
- predicates : & ty:: GenericPredicates < ' tcx > ,
1693
- ) -> impl Iterator < Item = ( ty:: Predicate < ' tcx > , Span ) > {
1694
- #[ derive( Default ) ]
1695
- struct ConstCollector < ' tcx > {
1696
- ct : SmallVec < [ ( ty:: WithOptConstParam < DefId > , SubstsRef < ' tcx > , Span ) ; 4 ] > ,
1697
- curr_span : Span ,
1698
- }
1699
-
1700
- impl < ' tcx > TypeVisitor < ' tcx > for ConstCollector < ' tcx > {
1701
- fn visit_const ( & mut self , ct : & ' tcx Const < ' tcx > ) -> bool {
1702
- if let ty:: ConstKind :: Unevaluated ( def, substs, None ) = ct. val {
1703
- self . ct . push ( ( def, substs, self . curr_span ) ) ;
1704
- }
1705
- false
1706
- }
1707
- }
1708
-
1709
- let mut collector = ConstCollector :: default ( ) ;
1710
- for & ( pred, span) in predicates. predicates . iter ( ) {
1711
- collector. curr_span = span;
1712
- pred. visit_with ( & mut collector) ;
1713
- }
1714
- warn ! ( "const_evaluatable_predicates_of({:?}) = {:?}" , def_id, collector. ct) ;
1715
- collector. ct . into_iter ( ) . map ( move |( def_id, subst, span) | {
1716
- ( ty:: PredicateAtom :: ConstEvaluatable ( def_id, subst) . to_predicate ( tcx) , span)
1717
- } )
1718
- }
1719
-
1720
1680
/// Returns a list of all type predicates (explicit and implicit) for the definition with
1721
1681
/// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
1722
1682
/// `Self: Trait` predicates for traits.
@@ -1754,29 +1714,6 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
1754
1714
1755
1715
debug ! ( "explicit_predicates_of(def_id={:?})" , def_id) ;
1756
1716
1757
- /// A data structure with unique elements, which preserves order of insertion.
1758
- /// Preserving the order of insertion is important here so as not to break
1759
- /// compile-fail UI tests.
1760
- struct UniquePredicates < ' tcx > {
1761
- predicates : FxIndexSet < ( ty:: Predicate < ' tcx > , Span ) > ,
1762
- }
1763
-
1764
- impl < ' tcx > UniquePredicates < ' tcx > {
1765
- fn new ( ) -> Self {
1766
- UniquePredicates { predicates : FxIndexSet :: default ( ) }
1767
- }
1768
-
1769
- fn push ( & mut self , value : ( ty:: Predicate < ' tcx > , Span ) ) {
1770
- self . predicates . insert ( value) ;
1771
- }
1772
-
1773
- fn extend < I : IntoIterator < Item = ( ty:: Predicate < ' tcx > , Span ) > > ( & mut self , iter : I ) {
1774
- for value in iter {
1775
- self . push ( value) ;
1776
- }
1777
- }
1778
- }
1779
-
1780
1717
let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id. expect_local ( ) ) ;
1781
1718
let node = tcx. hir ( ) . get ( hir_id) ;
1782
1719
@@ -1789,7 +1726,10 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
1789
1726
1790
1727
const NO_GENERICS : & hir:: Generics < ' _ > = & hir:: Generics :: empty ( ) ;
1791
1728
1792
- let mut predicates = UniquePredicates :: new ( ) ;
1729
+ // We use an `IndexSet` to preserves order of insertion.
1730
+ // Preserving the order of insertion is important here so as not to break
1731
+ // compile-fail UI tests.
1732
+ let mut predicates: FxIndexSet < ( ty:: Predicate < ' _ > , Span ) > = FxIndexSet :: default ( ) ;
1793
1733
1794
1734
let ast_generics = match node {
1795
1735
Node :: TraitItem ( item) => {
@@ -1891,7 +1831,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
1891
1831
// (see below). Recall that a default impl is not itself an impl, but rather a
1892
1832
// set of defaults that can be incorporated into another impl.
1893
1833
if let Some ( trait_ref) = is_default_impl_trait {
1894
- predicates. push ( (
1834
+ predicates. insert ( (
1895
1835
trait_ref. to_poly_trait_ref ( ) . without_const ( ) . to_predicate ( tcx) ,
1896
1836
tcx. def_span ( def_id) ,
1897
1837
) ) ;
@@ -1915,7 +1855,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
1915
1855
hir:: GenericBound :: Outlives ( lt) => {
1916
1856
let bound = AstConv :: ast_region_to_region ( & icx, & lt, None ) ;
1917
1857
let outlives = ty:: Binder :: bind ( ty:: OutlivesPredicate ( region, bound) ) ;
1918
- predicates. push ( ( outlives. to_predicate ( tcx) , lt. span ) ) ;
1858
+ predicates. insert ( ( outlives. to_predicate ( tcx) , lt. span ) ) ;
1919
1859
}
1920
1860
_ => bug ! ( ) ,
1921
1861
} ) ;
@@ -1970,7 +1910,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
1970
1910
let span = bound_pred. bounded_ty . span ;
1971
1911
let re_root_empty = tcx. lifetimes . re_root_empty ;
1972
1912
let predicate = ty:: OutlivesPredicate ( ty, re_root_empty) ;
1973
- predicates. push ( (
1913
+ predicates. insert ( (
1974
1914
ty:: PredicateAtom :: TypeOutlives ( predicate)
1975
1915
. potentially_quantified ( tcx, ty:: PredicateKind :: ForAll ) ,
1976
1916
span,
@@ -2014,11 +1954,11 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
2014
1954
2015
1955
& hir:: GenericBound :: Outlives ( ref lifetime) => {
2016
1956
let region = AstConv :: ast_region_to_region ( & icx, lifetime, None ) ;
2017
- predicates. push ( (
1957
+ predicates. insert ( (
2018
1958
ty:: PredicateAtom :: TypeOutlives ( ty:: OutlivesPredicate ( ty, region) )
2019
1959
. potentially_quantified ( tcx, ty:: PredicateKind :: ForAll ) ,
2020
1960
lifetime. span ,
2021
- ) )
1961
+ ) ) ;
2022
1962
}
2023
1963
}
2024
1964
}
@@ -2063,7 +2003,11 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
2063
2003
} ) )
2064
2004
}
2065
2005
2066
- let mut predicates: Vec < _ > = predicates. predicates . into_iter ( ) . collect ( ) ;
2006
+ if tcx. features ( ) . const_evaluatable_checked {
2007
+ predicates. extend ( const_evaluatable_predicates_of ( tcx, def_id. expect_local ( ) ) ) ;
2008
+ }
2009
+
2010
+ let mut predicates: Vec < _ > = predicates. into_iter ( ) . collect ( ) ;
2067
2011
2068
2012
// Subtle: before we store the predicates into the tcx, we
2069
2013
// sort them so that predicates like `T: Foo<Item=U>` come
@@ -2089,6 +2033,97 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
2089
2033
result
2090
2034
}
2091
2035
2036
+ fn const_evaluatable_predicates_of < ' tcx > (
2037
+ tcx : TyCtxt < ' tcx > ,
2038
+ def_id : LocalDefId ,
2039
+ ) -> FxIndexSet < ( ty:: Predicate < ' tcx > , Span ) > {
2040
+ struct ConstCollector < ' tcx > {
2041
+ tcx : TyCtxt < ' tcx > ,
2042
+ preds : FxIndexSet < ( ty:: Predicate < ' tcx > , Span ) > ,
2043
+ }
2044
+
2045
+ impl < ' tcx > intravisit:: Visitor < ' tcx > for ConstCollector < ' tcx > {
2046
+ type Map = Map < ' tcx > ;
2047
+
2048
+ fn nested_visit_map ( & mut self ) -> intravisit:: NestedVisitorMap < Self :: Map > {
2049
+ intravisit:: NestedVisitorMap :: None
2050
+ }
2051
+
2052
+ fn visit_anon_const ( & mut self , c : & ' tcx hir:: AnonConst ) {
2053
+ let def_id = self . tcx . hir ( ) . local_def_id ( c. hir_id ) ;
2054
+ let ct = ty:: Const :: from_anon_const ( self . tcx , def_id) ;
2055
+ if let ty:: ConstKind :: Unevaluated ( def, substs, None ) = ct. val {
2056
+ let span = self . tcx . hir ( ) . span ( c. hir_id ) ;
2057
+ self . preds . insert ( (
2058
+ ty:: PredicateAtom :: ConstEvaluatable ( def, substs) . to_predicate ( self . tcx ) ,
2059
+ span,
2060
+ ) ) ;
2061
+ }
2062
+ }
2063
+
2064
+ // Look into `TyAlias`.
2065
+ fn visit_ty ( & mut self , ty : & ' tcx hir:: Ty < ' tcx > ) {
2066
+ use ty:: fold:: { TypeFoldable , TypeVisitor } ;
2067
+ struct TyAliasVisitor < ' a , ' tcx > {
2068
+ tcx : TyCtxt < ' tcx > ,
2069
+ preds : & ' a mut FxIndexSet < ( ty:: Predicate < ' tcx > , Span ) > ,
2070
+ span : Span ,
2071
+ }
2072
+
2073
+ impl < ' a , ' tcx > TypeVisitor < ' tcx > for TyAliasVisitor < ' a , ' tcx > {
2074
+ fn visit_const ( & mut self , ct : & ' tcx Const < ' tcx > ) -> bool {
2075
+ if let ty:: ConstKind :: Unevaluated ( def, substs, None ) = ct. val {
2076
+ self . preds . insert ( (
2077
+ ty:: PredicateAtom :: ConstEvaluatable ( def, substs) . to_predicate ( self . tcx ) ,
2078
+ self . span ,
2079
+ ) ) ;
2080
+ }
2081
+ false
2082
+ }
2083
+ }
2084
+
2085
+ if let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , path) ) = ty. kind {
2086
+ if let Res :: Def ( DefKind :: TyAlias , def_id) = path. res {
2087
+ let mut visitor =
2088
+ TyAliasVisitor { tcx : self . tcx , preds : & mut self . preds , span : path. span } ;
2089
+ self . tcx . type_of ( def_id) . visit_with ( & mut visitor) ;
2090
+ }
2091
+ }
2092
+
2093
+ intravisit:: walk_ty ( self , ty)
2094
+ }
2095
+ }
2096
+
2097
+ let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
2098
+ let node = tcx. hir ( ) . get ( hir_id) ;
2099
+
2100
+ let mut collector = ConstCollector { tcx, preds : FxIndexSet :: default ( ) } ;
2101
+ if let hir:: Node :: Item ( item) = node {
2102
+ if let hir:: ItemKind :: Impl { ref of_trait, ref self_ty, .. } = item. kind {
2103
+ if let Some ( of_trait) = of_trait {
2104
+ warn ! ( "const_evaluatable_predicates_of({:?}): visit impl trait_ref" , def_id) ;
2105
+ collector. visit_trait_ref ( of_trait) ;
2106
+ }
2107
+
2108
+ warn ! ( "const_evaluatable_predicates_of({:?}): visit_self_ty" , def_id) ;
2109
+ collector. visit_ty ( self_ty) ;
2110
+ }
2111
+ }
2112
+
2113
+ if let Some ( generics) = node. generics ( ) {
2114
+ warn ! ( "const_evaluatable_predicates_of({:?}): visit_generics" , def_id) ;
2115
+ collector. visit_generics ( generics) ;
2116
+ }
2117
+
2118
+ if let Some ( fn_sig) = tcx. hir ( ) . fn_sig_by_hir_id ( hir_id) {
2119
+ warn ! ( "const_evaluatable_predicates_of({:?}): visit_fn_decl" , def_id) ;
2120
+ collector. visit_fn_decl ( fn_sig. decl ) ;
2121
+ }
2122
+ warn ! ( "const_evaluatable_predicates_of({:?}) = {:?}" , def_id, collector. preds) ;
2123
+
2124
+ collector. preds
2125
+ }
2126
+
2092
2127
fn projection_ty_from_predicates (
2093
2128
tcx : TyCtxt < ' tcx > ,
2094
2129
key : (
0 commit comments