@@ -31,7 +31,7 @@ use rustc_span::symbol::sym;
3131use rustc_span:: { MultiSpan , Span , DUMMY_SP } ;
3232use rustc_target:: spec:: abi;
3333use smallvec:: SmallVec ;
34- use syntax:: ast;
34+ use syntax:: ast:: { self , Constness } ;
3535use syntax:: util:: lev_distance:: find_best_match_for_name;
3636
3737use std:: collections:: BTreeSet ;
@@ -49,6 +49,8 @@ pub trait AstConv<'tcx> {
4949
5050 fn item_def_id ( & self ) -> Option < DefId > ;
5151
52+ fn default_constness_for_trait_bounds ( & self ) -> Constness ;
53+
5254 /// Returns predicates in scope of the form `X: Foo`, where `X` is
5355 /// a type parameter `X` with the given id `def_id`. This is a
5456 /// subset of the full set of predicates.
@@ -919,6 +921,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
919921 & self ,
920922 trait_ref : & hir:: TraitRef < ' _ > ,
921923 span : Span ,
924+ constness : Constness ,
922925 self_ty : Ty < ' tcx > ,
923926 bounds : & mut Bounds < ' tcx > ,
924927 speculative : bool ,
@@ -947,7 +950,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
947950 ) ;
948951 let poly_trait_ref = ty:: Binder :: bind ( ty:: TraitRef :: new ( trait_def_id, substs) ) ;
949952
950- bounds. trait_bounds . push ( ( poly_trait_ref, span) ) ;
953+ bounds. trait_bounds . push ( ( poly_trait_ref, span, constness ) ) ;
951954
952955 let mut dup_bindings = FxHashMap :: default ( ) ;
953956 for binding in & assoc_bindings {
@@ -993,12 +996,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
993996 pub fn instantiate_poly_trait_ref (
994997 & self ,
995998 poly_trait_ref : & hir:: PolyTraitRef < ' _ > ,
999+ constness : Constness ,
9961000 self_ty : Ty < ' tcx > ,
9971001 bounds : & mut Bounds < ' tcx > ,
9981002 ) -> Option < Vec < Span > > {
9991003 self . instantiate_poly_trait_ref_inner (
10001004 & poly_trait_ref. trait_ref ,
10011005 poly_trait_ref. span ,
1006+ constness,
10021007 self_ty,
10031008 bounds,
10041009 false ,
@@ -1181,18 +1186,22 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11811186 let mut trait_bounds = Vec :: new ( ) ;
11821187 let mut region_bounds = Vec :: new ( ) ;
11831188
1189+ let constness = self . default_constness_for_trait_bounds ( ) ;
11841190 for ast_bound in ast_bounds {
11851191 match * ast_bound {
11861192 hir:: GenericBound :: Trait ( ref b, hir:: TraitBoundModifier :: None ) => {
1187- trait_bounds. push ( b)
1193+ trait_bounds. push ( ( b, constness) )
1194+ }
1195+ hir:: GenericBound :: Trait ( ref b, hir:: TraitBoundModifier :: MaybeConst ) => {
1196+ trait_bounds. push ( ( b, Constness :: NotConst ) )
11881197 }
11891198 hir:: GenericBound :: Trait ( _, hir:: TraitBoundModifier :: Maybe ) => { }
11901199 hir:: GenericBound :: Outlives ( ref l) => region_bounds. push ( l) ,
11911200 }
11921201 }
11931202
1194- for bound in trait_bounds {
1195- let _ = self . instantiate_poly_trait_ref ( bound, param_ty, bounds) ;
1203+ for ( bound, constness ) in trait_bounds {
1204+ let _ = self . instantiate_poly_trait_ref ( bound, constness , param_ty, bounds) ;
11961205 }
11971206
11981207 bounds. region_bounds . extend (
@@ -1226,7 +1235,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12261235 let mut bounds = Bounds :: default ( ) ;
12271236
12281237 self . add_bounds ( param_ty, ast_bounds, & mut bounds) ;
1229- bounds. trait_bounds . sort_by_key ( |( t, _) | t. def_id ( ) ) ;
1238+ bounds. trait_bounds . sort_by_key ( |( t, _, _ ) | t. def_id ( ) ) ;
12301239
12311240 bounds. implicitly_sized = if let SizedByDefault :: Yes = sized_by_default {
12321241 if !self . is_unsized ( ast_bounds, span) { Some ( span) } else { None }
@@ -1417,15 +1426,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14171426 let mut potential_assoc_types = Vec :: new ( ) ;
14181427 let dummy_self = self . tcx ( ) . types . trait_object_dummy_self ;
14191428 for trait_bound in trait_bounds. iter ( ) . rev ( ) {
1420- let cur_potential_assoc_types =
1421- self . instantiate_poly_trait_ref ( trait_bound, dummy_self, & mut bounds) ;
1429+ let cur_potential_assoc_types = self . instantiate_poly_trait_ref (
1430+ trait_bound,
1431+ Constness :: NotConst ,
1432+ dummy_self,
1433+ & mut bounds,
1434+ ) ;
14221435 potential_assoc_types. extend ( cur_potential_assoc_types. into_iter ( ) . flatten ( ) ) ;
14231436 }
14241437
14251438 // Expand trait aliases recursively and check that only one regular (non-auto) trait
14261439 // is used and no 'maybe' bounds are used.
1427- let expanded_traits =
1428- traits:: expand_trait_aliases ( tcx, bounds. trait_bounds . iter ( ) . cloned ( ) ) ;
1440+ let expanded_traits = traits:: expand_trait_aliases (
1441+ tcx,
1442+ bounds. trait_bounds . iter ( ) . map ( |& ( a, b, _) | ( a. clone ( ) , b) ) ,
1443+ ) ;
14291444 let ( mut auto_traits, regular_traits) : ( Vec < _ > , Vec < _ > ) =
14301445 expanded_traits. partition ( |i| tcx. trait_is_auto ( i. trait_ref ( ) . def_id ( ) ) ) ;
14311446 if regular_traits. len ( ) > 1 {
@@ -1481,16 +1496,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14811496 let regular_traits_refs_spans = bounds
14821497 . trait_bounds
14831498 . into_iter ( )
1484- . filter ( |( trait_ref, _) | !tcx. trait_is_auto ( trait_ref. def_id ( ) ) ) ;
1499+ . filter ( |( trait_ref, _, _) | !tcx. trait_is_auto ( trait_ref. def_id ( ) ) ) ;
1500+
1501+ for ( base_trait_ref, span, constness) in regular_traits_refs_spans {
1502+ assert_eq ! ( constness, ast:: Constness :: NotConst ) ;
14851503
1486- for ( base_trait_ref, span) in regular_traits_refs_spans {
14871504 for trait_ref in traits:: elaborate_trait_ref ( tcx, base_trait_ref) {
14881505 debug ! (
14891506 "conv_object_ty_poly_trait_ref: observing object predicate `{:?}`" ,
14901507 trait_ref
14911508 ) ;
14921509 match trait_ref {
1493- ty:: Predicate :: Trait ( pred, constness ) => {
1510+ ty:: Predicate :: Trait ( pred, _ ) => {
14941511 associated_types. entry ( span) . or_default ( ) . extend (
14951512 tcx. associated_items ( pred. def_id ( ) )
14961513 . filter ( |item| item. kind == ty:: AssocKind :: Type )
@@ -2949,7 +2966,7 @@ pub struct Bounds<'tcx> {
29492966
29502967 /// A list of trait bounds. So if you had `T: Debug` this would be
29512968 /// `T: Debug`. Note that the self-type is explicit here.
2952- pub trait_bounds : Vec < ( ty:: PolyTraitRef < ' tcx > , Span ) > ,
2969+ pub trait_bounds : Vec < ( ty:: PolyTraitRef < ' tcx > , Span , Constness ) > ,
29532970
29542971 /// A list of projection equality bounds. So if you had `T:
29552972 /// Iterator<Item = u32>` this would include `<T as
@@ -2997,11 +3014,10 @@ impl<'tcx> Bounds<'tcx> {
29973014 let outlives = ty:: OutlivesPredicate ( param_ty, region_bound) ;
29983015 ( ty:: Binder :: bind ( outlives) . to_predicate ( ) , span)
29993016 } )
3000- . chain (
3001- self . trait_bounds
3002- . iter ( )
3003- . map ( |& ( bound_trait_ref, span) | ( bound_trait_ref. to_predicate ( ) , span) ) ,
3004- )
3017+ . chain ( self . trait_bounds . iter ( ) . map ( |& ( bound_trait_ref, span, constness) | {
3018+ let predicate = bound_trait_ref. with_constness ( constness) . to_predicate ( ) ;
3019+ ( predicate, span)
3020+ } ) )
30053021 . chain (
30063022 self . projection_bounds
30073023 . iter ( )
0 commit comments