@@ -31,7 +31,7 @@ use crate::{
31
31
traits:: chalk:: { Interner , ToChalk } ,
32
32
utils:: {
33
33
all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
34
- variant_data,
34
+ variant_data, Generics ,
35
35
} ,
36
36
AliasEq , AliasTy , Binders , BoundVar , CallableSig , DebruijnIndex , DynTy , FnPointer , FnSig ,
37
37
ImplTraitId , OpaqueTy , PolyFnSig , ProjectionTy , QuantifiedWhereClause , QuantifiedWhereClauses ,
@@ -196,7 +196,7 @@ impl<'a> TyLoweringContext<'a> {
196
196
bounds. iter ( ) . flat_map ( |b| ctx. lower_type_bound ( b, self_ty. clone ( ) , false ) ) ,
197
197
)
198
198
} ) ;
199
- let bounds = Binders :: new ( 1 , bounds) ;
199
+ let bounds = crate :: make_only_type_binders ( 1 , bounds) ;
200
200
TyKind :: Dyn ( DynTy { bounds } ) . intern ( & Interner )
201
201
}
202
202
TypeRef :: ImplTrait ( bounds) => {
@@ -209,9 +209,9 @@ impl<'a> TyLoweringContext<'a> {
209
209
// this dance is to make sure the data is in the right
210
210
// place even if we encounter more opaque types while
211
211
// lowering the bounds
212
- self . opaque_type_data
213
- . borrow_mut ( )
214
- . push ( ReturnTypeImplTrait { bounds : Binders :: new ( 1 , Vec :: new ( ) ) } ) ;
212
+ self . opaque_type_data . borrow_mut ( ) . push ( ReturnTypeImplTrait {
213
+ bounds : crate :: make_only_type_binders ( 1 , Vec :: new ( ) ) ,
214
+ } ) ;
215
215
// We don't want to lower the bounds inside the binders
216
216
// we're currently in, because they don't end up inside
217
217
// those binders. E.g. when we have `impl Trait<impl
@@ -380,7 +380,7 @@ impl<'a> TyLoweringContext<'a> {
380
380
TyKind :: Error . intern ( & Interner )
381
381
} else {
382
382
let dyn_ty = DynTy {
383
- bounds : Binders :: new (
383
+ bounds : crate :: make_only_type_binders (
384
384
1 ,
385
385
QuantifiedWhereClauses :: from_iter (
386
386
& Interner ,
@@ -787,7 +787,7 @@ impl<'a> TyLoweringContext<'a> {
787
787
let predicates = self . with_shifted_in ( DebruijnIndex :: ONE , |ctx| {
788
788
bounds. iter ( ) . flat_map ( |b| ctx. lower_type_bound ( b, self_ty. clone ( ) , false ) ) . collect ( )
789
789
} ) ;
790
- ReturnTypeImplTrait { bounds : Binders :: new ( 1 , predicates) }
790
+ ReturnTypeImplTrait { bounds : crate :: make_only_type_binders ( 1 , predicates) }
791
791
}
792
792
}
793
793
@@ -884,7 +884,7 @@ pub(crate) fn field_types_query(
884
884
let ctx =
885
885
TyLoweringContext :: new ( db, & resolver) . with_type_param_mode ( TypeParamLoweringMode :: Variable ) ;
886
886
for ( field_id, field_data) in var_data. fields ( ) . iter ( ) {
887
- res. insert ( field_id, Binders :: new ( generics. len ( ) , ctx. lower_ty ( & field_data. type_ref ) ) )
887
+ res. insert ( field_id, make_binders ( & generics, ctx. lower_ty ( & field_data. type_ref ) ) )
888
888
}
889
889
Arc :: new ( res)
890
890
}
@@ -918,9 +918,7 @@ pub(crate) fn generic_predicates_for_param_query(
918
918
} ,
919
919
WherePredicate :: Lifetime { .. } => false ,
920
920
} )
921
- . flat_map ( |pred| {
922
- ctx. lower_where_predicate ( pred, true ) . map ( |p| Binders :: new ( generics. len ( ) , p) )
923
- } )
921
+ . flat_map ( |pred| ctx. lower_where_predicate ( pred, true ) . map ( |p| make_binders ( & generics, p) ) )
924
922
. collect ( )
925
923
}
926
924
@@ -991,9 +989,7 @@ pub(crate) fn generic_predicates_query(
991
989
let generics = generics ( db. upcast ( ) , def) ;
992
990
resolver
993
991
. where_predicates_in_scope ( )
994
- . flat_map ( |pred| {
995
- ctx. lower_where_predicate ( pred, false ) . map ( |p| Binders :: new ( generics. len ( ) , p) )
996
- } )
992
+ . flat_map ( |pred| ctx. lower_where_predicate ( pred, false ) . map ( |p| make_binders ( & generics, p) ) )
997
993
. collect ( )
998
994
}
999
995
@@ -1030,7 +1026,7 @@ pub(crate) fn generic_defaults_query(
1030
1026
DebruijnIndex :: INNERMOST ,
1031
1027
) ;
1032
1028
1033
- Binders :: new ( idx, ty)
1029
+ crate :: make_only_type_binders ( idx, ty)
1034
1030
} )
1035
1031
. collect ( ) ;
1036
1032
@@ -1043,23 +1039,22 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
1043
1039
let ctx_params = TyLoweringContext :: new ( db, & resolver)
1044
1040
. with_impl_trait_mode ( ImplTraitLoweringMode :: Variable )
1045
1041
. with_type_param_mode ( TypeParamLoweringMode :: Variable ) ;
1046
- let params = data. params . iter ( ) . map ( |tr| ( & ctx_params) . lower_ty ( tr) ) . collect :: < Vec < _ > > ( ) ;
1042
+ let params = data. params . iter ( ) . map ( |tr| ctx_params. lower_ty ( tr) ) . collect :: < Vec < _ > > ( ) ;
1047
1043
let ctx_ret = TyLoweringContext :: new ( db, & resolver)
1048
1044
. with_impl_trait_mode ( ImplTraitLoweringMode :: Opaque )
1049
1045
. with_type_param_mode ( TypeParamLoweringMode :: Variable ) ;
1050
- let ret = ( & ctx_ret) . lower_ty ( & data. ret_type ) ;
1046
+ let ret = ctx_ret. lower_ty ( & data. ret_type ) ;
1051
1047
let generics = generics ( db. upcast ( ) , def. into ( ) ) ;
1052
- let num_binders = generics. len ( ) ;
1053
- Binders :: new ( num_binders, CallableSig :: from_params_and_return ( params, ret, data. is_varargs ( ) ) )
1048
+ make_binders ( & generics, CallableSig :: from_params_and_return ( params, ret, data. is_varargs ( ) ) )
1054
1049
}
1055
1050
1056
1051
/// Build the declared type of a function. This should not need to look at the
1057
1052
/// function body.
1058
1053
fn type_for_fn ( db : & dyn HirDatabase , def : FunctionId ) -> Binders < Ty > {
1059
1054
let generics = generics ( db. upcast ( ) , def. into ( ) ) ;
1060
1055
let substs = generics. bound_vars_subst ( DebruijnIndex :: INNERMOST ) ;
1061
- Binders :: new (
1062
- substs . len ( & Interner ) ,
1056
+ make_binders (
1057
+ & generics ,
1063
1058
TyKind :: FnDef ( CallableDefId :: FunctionId ( def) . to_chalk ( db) , substs) . intern ( & Interner ) ,
1064
1059
)
1065
1060
}
@@ -1072,7 +1067,7 @@ fn type_for_const(db: &dyn HirDatabase, def: ConstId) -> Binders<Ty> {
1072
1067
let ctx =
1073
1068
TyLoweringContext :: new ( db, & resolver) . with_type_param_mode ( TypeParamLoweringMode :: Variable ) ;
1074
1069
1075
- Binders :: new ( generics. len ( ) , ctx. lower_ty ( & data. type_ref ) )
1070
+ make_binders ( & generics, ctx. lower_ty ( & data. type_ref ) )
1076
1071
}
1077
1072
1078
1073
/// Build the declared type of a static.
@@ -1081,7 +1076,7 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
1081
1076
let resolver = def. resolver ( db. upcast ( ) ) ;
1082
1077
let ctx = TyLoweringContext :: new ( db, & resolver) ;
1083
1078
1084
- Binders :: new ( 0 , ctx. lower_ty ( & data. type_ref ) )
1079
+ Binders :: empty ( & Interner , ctx. lower_ty ( & data. type_ref ) )
1085
1080
}
1086
1081
1087
1082
fn fn_sig_for_struct_constructor ( db : & dyn HirDatabase , def : StructId ) -> PolyFnSig {
@@ -1103,8 +1098,8 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
1103
1098
}
1104
1099
let generics = generics ( db. upcast ( ) , def. into ( ) ) ;
1105
1100
let substs = generics. bound_vars_subst ( DebruijnIndex :: INNERMOST ) ;
1106
- Binders :: new (
1107
- substs . len ( & Interner ) ,
1101
+ make_binders (
1102
+ & generics ,
1108
1103
TyKind :: FnDef ( CallableDefId :: StructId ( def) . to_chalk ( db) , substs) . intern ( & Interner ) ,
1109
1104
)
1110
1105
}
@@ -1130,17 +1125,17 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
1130
1125
}
1131
1126
let generics = generics ( db. upcast ( ) , def. parent . into ( ) ) ;
1132
1127
let substs = generics. bound_vars_subst ( DebruijnIndex :: INNERMOST ) ;
1133
- Binders :: new (
1134
- substs . len ( & Interner ) ,
1128
+ make_binders (
1129
+ & generics ,
1135
1130
TyKind :: FnDef ( CallableDefId :: EnumVariantId ( def) . to_chalk ( db) , substs) . intern ( & Interner ) ,
1136
1131
)
1137
1132
}
1138
1133
1139
1134
fn type_for_adt ( db : & dyn HirDatabase , adt : AdtId ) -> Binders < Ty > {
1135
+ let generics = generics ( db. upcast ( ) , adt. into ( ) ) ;
1140
1136
let b = TyBuilder :: adt ( db, adt) ;
1141
- let num_binders = b. remaining ( ) ;
1142
1137
let ty = b. fill_with_bound_vars ( DebruijnIndex :: INNERMOST , 0 ) . build ( ) ;
1143
- Binders :: new ( num_binders , ty)
1138
+ make_binders ( & generics , ty)
1144
1139
}
1145
1140
1146
1141
fn type_for_type_alias ( db : & dyn HirDatabase , t : TypeAliasId ) -> Binders < Ty > {
@@ -1149,11 +1144,11 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
1149
1144
let ctx =
1150
1145
TyLoweringContext :: new ( db, & resolver) . with_type_param_mode ( TypeParamLoweringMode :: Variable ) ;
1151
1146
if db. type_alias_data ( t) . is_extern {
1152
- Binders :: new ( 0 , TyKind :: Foreign ( crate :: to_foreign_def_id ( t) ) . intern ( & Interner ) )
1147
+ Binders :: empty ( & Interner , TyKind :: Foreign ( crate :: to_foreign_def_id ( t) ) . intern ( & Interner ) )
1153
1148
} else {
1154
1149
let type_ref = & db. type_alias_data ( t) . type_ref ;
1155
1150
let inner = ctx. lower_ty ( type_ref. as_deref ( ) . unwrap_or ( & TypeRef :: Error ) ) ;
1156
- Binders :: new ( generics. len ( ) , inner)
1151
+ make_binders ( & generics, inner)
1157
1152
}
1158
1153
}
1159
1154
@@ -1212,19 +1207,21 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V
1212
1207
/// namespace.
1213
1208
pub ( crate ) fn ty_query ( db : & dyn HirDatabase , def : TyDefId ) -> Binders < Ty > {
1214
1209
match def {
1215
- TyDefId :: BuiltinType ( it) => Binders :: new ( 0 , TyBuilder :: builtin ( it) ) ,
1210
+ TyDefId :: BuiltinType ( it) => Binders :: empty ( & Interner , TyBuilder :: builtin ( it) ) ,
1216
1211
TyDefId :: AdtId ( it) => type_for_adt ( db, it) ,
1217
1212
TyDefId :: TypeAliasId ( it) => type_for_type_alias ( db, it) ,
1218
1213
}
1219
1214
}
1220
1215
1221
1216
pub ( crate ) fn ty_recover ( db : & dyn HirDatabase , _cycle : & [ String ] , def : & TyDefId ) -> Binders < Ty > {
1222
- let num_binders = match * def {
1223
- TyDefId :: BuiltinType ( _) => 0 ,
1224
- TyDefId :: AdtId ( it) => generics ( db. upcast ( ) , it. into ( ) ) . len ( ) ,
1225
- TyDefId :: TypeAliasId ( it) => generics ( db. upcast ( ) , it. into ( ) ) . len ( ) ,
1217
+ let generics = match * def {
1218
+ TyDefId :: BuiltinType ( _) => {
1219
+ return Binders :: empty ( & Interner , TyKind :: Error . intern ( & Interner ) )
1220
+ }
1221
+ TyDefId :: AdtId ( it) => generics ( db. upcast ( ) , it. into ( ) ) ,
1222
+ TyDefId :: TypeAliasId ( it) => generics ( db. upcast ( ) , it. into ( ) ) ,
1226
1223
} ;
1227
- Binders :: new ( num_binders , TyKind :: Error . intern ( & Interner ) )
1224
+ make_binders ( & generics , TyKind :: Error . intern ( & Interner ) )
1228
1225
}
1229
1226
1230
1227
pub ( crate ) fn value_ty_query ( db : & dyn HirDatabase , def : ValueTyDefId ) -> Binders < Ty > {
@@ -1244,7 +1241,7 @@ pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binde
1244
1241
let generics = generics ( db. upcast ( ) , impl_id. into ( ) ) ;
1245
1242
let ctx =
1246
1243
TyLoweringContext :: new ( db, & resolver) . with_type_param_mode ( TypeParamLoweringMode :: Variable ) ;
1247
- Binders :: new ( generics. len ( ) , ctx. lower_ty ( & impl_data. self_ty ) )
1244
+ make_binders ( & generics, ctx. lower_ty ( & impl_data. self_ty ) )
1248
1245
}
1249
1246
1250
1247
pub ( crate ) fn const_param_ty_query ( db : & dyn HirDatabase , def : ConstParamId ) -> Ty {
@@ -1262,7 +1259,7 @@ pub(crate) fn impl_self_ty_recover(
1262
1259
impl_id : & ImplId ,
1263
1260
) -> Binders < Ty > {
1264
1261
let generics = generics ( db. upcast ( ) , ( * impl_id) . into ( ) ) ;
1265
- Binders :: new ( generics. len ( ) , TyKind :: Error . intern ( & Interner ) )
1262
+ make_binders ( & generics, TyKind :: Error . intern ( & Interner ) )
1266
1263
}
1267
1264
1268
1265
pub ( crate ) fn impl_trait_query ( db : & dyn HirDatabase , impl_id : ImplId ) -> Option < Binders < TraitRef > > {
@@ -1287,13 +1284,12 @@ pub(crate) fn return_type_impl_traits(
1287
1284
. with_type_param_mode ( TypeParamLoweringMode :: Variable ) ;
1288
1285
let _ret = ( & ctx_ret) . lower_ty ( & data. ret_type ) ;
1289
1286
let generics = generics ( db. upcast ( ) , def. into ( ) ) ;
1290
- let num_binders = generics. len ( ) ;
1291
1287
let return_type_impl_traits =
1292
1288
ReturnTypeImplTraits { impl_traits : ctx_ret. opaque_type_data . into_inner ( ) } ;
1293
1289
if return_type_impl_traits. impl_traits . is_empty ( ) {
1294
1290
None
1295
1291
} else {
1296
- Some ( Arc :: new ( Binders :: new ( num_binders , return_type_impl_traits) ) )
1292
+ Some ( Arc :: new ( make_binders ( & generics , return_type_impl_traits) ) )
1297
1293
}
1298
1294
}
1299
1295
@@ -1303,3 +1299,7 @@ pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mut
1303
1299
hir_def:: type_ref:: Mutability :: Mut => Mutability :: Mut ,
1304
1300
}
1305
1301
}
1302
+
1303
+ fn make_binders < T > ( generics : & Generics , value : T ) -> Binders < T > {
1304
+ crate :: make_only_type_binders ( generics. len ( ) , value)
1305
+ }
0 commit comments