Skip to content

Commit ad20f00

Browse files
committed
Use VariableKinds in Binders
1 parent 69714d3 commit ad20f00

File tree

7 files changed

+107
-84
lines changed

7 files changed

+107
-84
lines changed

crates/hir_ty/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl TyBuilder<TypeAliasId> {
194194

195195
impl<T: TypeWalk + HasInterner<Interner = Interner>> TyBuilder<Binders<T>> {
196196
fn subst_binders(b: Binders<T>) -> Self {
197-
let param_count = b.num_binders;
197+
let param_count = b.binders.len(&Interner);
198198
TyBuilder::new(b, param_count)
199199
}
200200

crates/hir_ty/src/infer/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ impl<'a> InferenceContext<'a> {
849849
self.write_method_resolution(tgt_expr, func);
850850
(ty, self.db.value_ty(func.into()), Some(generics(self.db.upcast(), func.into())))
851851
}
852-
None => (receiver_ty, Binders::new(0, self.err_ty()), None),
852+
None => (receiver_ty, Binders::empty(&Interner, self.err_ty()), None),
853853
};
854854
let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty);
855855
let method_ty = method_ty.subst(&substs);
@@ -951,7 +951,7 @@ impl<'a> InferenceContext<'a> {
951951
for predicate in generic_predicates.iter() {
952952
let (predicate, binders) =
953953
predicate.clone().subst(parameters).into_value_and_skipped_binders();
954-
always!(binders == 0); // quantified where clauses not yet handled
954+
always!(binders.len(&Interner) == 0); // quantified where clauses not yet handled
955955
self.push_obligation(predicate.cast(&Interner));
956956
}
957957
// add obligation for trait implementation, if this is a trait method

crates/hir_ty/src/lib.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub type ClosureId = chalk_ir::ClosureId<Interner>;
6666
pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
6767
pub type PlaceholderIndex = chalk_ir::PlaceholderIndex;
6868

69+
pub type VariableKind = chalk_ir::VariableKind<Interner>;
70+
pub type VariableKinds = chalk_ir::VariableKinds<Interner>;
6971
pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>;
7072

7173
pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
@@ -126,22 +128,26 @@ impl<T> Binders<T> {
126128
}
127129
}
128130

129-
impl<T: Clone> Binders<&T> {
130-
pub fn cloned(&self) -> Binders<T> {
131-
let (value, binders) = self.into_value_and_skipped_binders();
132-
Binders::new(binders, value.clone())
133-
}
134-
}
135-
136131
impl<T: TypeWalk> Binders<T> {
137132
/// Substitutes all variables.
138133
pub fn subst(self, subst: &Substitution) -> T {
139134
let (value, binders) = self.into_value_and_skipped_binders();
140-
assert_eq!(subst.len(&Interner), binders);
135+
assert_eq!(subst.len(&Interner), binders.len(&Interner));
141136
value.subst_bound_vars(subst)
142137
}
143138
}
144139

140+
pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> {
141+
Binders::new(
142+
VariableKinds::from_iter(
143+
&Interner,
144+
std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
145+
.take(num_vars),
146+
),
147+
value,
148+
)
149+
}
150+
145151
impl TraitRef {
146152
pub fn self_type_parameter(&self, interner: &Interner) -> &Ty {
147153
&self.substitution.at(interner, 0).assert_ty_ref(interner)
@@ -407,8 +413,8 @@ impl Ty {
407413
// This is only used by type walking.
408414
// Parameters will be walked outside, and projection predicate is not used.
409415
// So just provide the Future trait.
410-
let impl_bound = Binders::new(
411-
0,
416+
let impl_bound = Binders::empty(
417+
&Interner,
412418
WhereClause::Implemented(TraitRef {
413419
trait_id: to_chalk_trait_id(future_trait),
414420
substitution: Substitution::empty(&Interner),

crates/hir_ty/src/lower.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::{
3131
traits::chalk::{Interner, ToChalk},
3232
utils::{
3333
all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
34-
variant_data,
34+
variant_data, Generics,
3535
},
3636
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
3737
ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses,
@@ -196,7 +196,7 @@ impl<'a> TyLoweringContext<'a> {
196196
bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)),
197197
)
198198
});
199-
let bounds = Binders::new(1, bounds);
199+
let bounds = crate::make_only_type_binders(1, bounds);
200200
TyKind::Dyn(DynTy { bounds }).intern(&Interner)
201201
}
202202
TypeRef::ImplTrait(bounds) => {
@@ -209,9 +209,9 @@ impl<'a> TyLoweringContext<'a> {
209209
// this dance is to make sure the data is in the right
210210
// place even if we encounter more opaque types while
211211
// 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+
});
215215
// We don't want to lower the bounds inside the binders
216216
// we're currently in, because they don't end up inside
217217
// those binders. E.g. when we have `impl Trait<impl
@@ -380,7 +380,7 @@ impl<'a> TyLoweringContext<'a> {
380380
TyKind::Error.intern(&Interner)
381381
} else {
382382
let dyn_ty = DynTy {
383-
bounds: Binders::new(
383+
bounds: crate::make_only_type_binders(
384384
1,
385385
QuantifiedWhereClauses::from_iter(
386386
&Interner,
@@ -787,7 +787,7 @@ impl<'a> TyLoweringContext<'a> {
787787
let predicates = self.with_shifted_in(DebruijnIndex::ONE, |ctx| {
788788
bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)).collect()
789789
});
790-
ReturnTypeImplTrait { bounds: Binders::new(1, predicates) }
790+
ReturnTypeImplTrait { bounds: crate::make_only_type_binders(1, predicates) }
791791
}
792792
}
793793

@@ -884,7 +884,7 @@ pub(crate) fn field_types_query(
884884
let ctx =
885885
TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
886886
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)))
888888
}
889889
Arc::new(res)
890890
}
@@ -918,9 +918,7 @@ pub(crate) fn generic_predicates_for_param_query(
918918
},
919919
WherePredicate::Lifetime { .. } => false,
920920
})
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)))
924922
.collect()
925923
}
926924

@@ -991,9 +989,7 @@ pub(crate) fn generic_predicates_query(
991989
let generics = generics(db.upcast(), def);
992990
resolver
993991
.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)))
997993
.collect()
998994
}
999995

@@ -1030,7 +1026,7 @@ pub(crate) fn generic_defaults_query(
10301026
DebruijnIndex::INNERMOST,
10311027
);
10321028

1033-
Binders::new(idx, ty)
1029+
crate::make_only_type_binders(idx, ty)
10341030
})
10351031
.collect();
10361032

@@ -1043,23 +1039,22 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
10431039
let ctx_params = TyLoweringContext::new(db, &resolver)
10441040
.with_impl_trait_mode(ImplTraitLoweringMode::Variable)
10451041
.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<_>>();
10471043
let ctx_ret = TyLoweringContext::new(db, &resolver)
10481044
.with_impl_trait_mode(ImplTraitLoweringMode::Opaque)
10491045
.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);
10511047
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()))
10541049
}
10551050

10561051
/// Build the declared type of a function. This should not need to look at the
10571052
/// function body.
10581053
fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
10591054
let generics = generics(db.upcast(), def.into());
10601055
let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1061-
Binders::new(
1062-
substs.len(&Interner),
1056+
make_binders(
1057+
&generics,
10631058
TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner),
10641059
)
10651060
}
@@ -1072,7 +1067,7 @@ fn type_for_const(db: &dyn HirDatabase, def: ConstId) -> Binders<Ty> {
10721067
let ctx =
10731068
TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
10741069

1075-
Binders::new(generics.len(), ctx.lower_ty(&data.type_ref))
1070+
make_binders(&generics, ctx.lower_ty(&data.type_ref))
10761071
}
10771072

10781073
/// Build the declared type of a static.
@@ -1081,7 +1076,7 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
10811076
let resolver = def.resolver(db.upcast());
10821077
let ctx = TyLoweringContext::new(db, &resolver);
10831078

1084-
Binders::new(0, ctx.lower_ty(&data.type_ref))
1079+
Binders::empty(&Interner, ctx.lower_ty(&data.type_ref))
10851080
}
10861081

10871082
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
11031098
}
11041099
let generics = generics(db.upcast(), def.into());
11051100
let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1106-
Binders::new(
1107-
substs.len(&Interner),
1101+
make_binders(
1102+
&generics,
11081103
TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner),
11091104
)
11101105
}
@@ -1130,17 +1125,17 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
11301125
}
11311126
let generics = generics(db.upcast(), def.parent.into());
11321127
let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1133-
Binders::new(
1134-
substs.len(&Interner),
1128+
make_binders(
1129+
&generics,
11351130
TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner),
11361131
)
11371132
}
11381133

11391134
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
1135+
let generics = generics(db.upcast(), adt.into());
11401136
let b = TyBuilder::adt(db, adt);
1141-
let num_binders = b.remaining();
11421137
let ty = b.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0).build();
1143-
Binders::new(num_binders, ty)
1138+
make_binders(&generics, ty)
11441139
}
11451140

11461141
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> {
11491144
let ctx =
11501145
TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
11511146
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))
11531148
} else {
11541149
let type_ref = &db.type_alias_data(t).type_ref;
11551150
let inner = ctx.lower_ty(type_ref.as_deref().unwrap_or(&TypeRef::Error));
1156-
Binders::new(generics.len(), inner)
1151+
make_binders(&generics, inner)
11571152
}
11581153
}
11591154

@@ -1212,19 +1207,21 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V
12121207
/// namespace.
12131208
pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> {
12141209
match def {
1215-
TyDefId::BuiltinType(it) => Binders::new(0, TyBuilder::builtin(it)),
1210+
TyDefId::BuiltinType(it) => Binders::empty(&Interner, TyBuilder::builtin(it)),
12161211
TyDefId::AdtId(it) => type_for_adt(db, it),
12171212
TyDefId::TypeAliasId(it) => type_for_type_alias(db, it),
12181213
}
12191214
}
12201215

12211216
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()),
12261223
};
1227-
Binders::new(num_binders, TyKind::Error.intern(&Interner))
1224+
make_binders(&generics, TyKind::Error.intern(&Interner))
12281225
}
12291226

12301227
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
12441241
let generics = generics(db.upcast(), impl_id.into());
12451242
let ctx =
12461243
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))
12481245
}
12491246

12501247
pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty {
@@ -1262,7 +1259,7 @@ pub(crate) fn impl_self_ty_recover(
12621259
impl_id: &ImplId,
12631260
) -> Binders<Ty> {
12641261
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))
12661263
}
12671264

12681265
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(
12871284
.with_type_param_mode(TypeParamLoweringMode::Variable);
12881285
let _ret = (&ctx_ret).lower_ty(&data.ret_type);
12891286
let generics = generics(db.upcast(), def.into());
1290-
let num_binders = generics.len();
12911287
let return_type_impl_traits =
12921288
ReturnTypeImplTraits { impl_traits: ctx_ret.opaque_type_data.into_inner() };
12931289
if return_type_impl_traits.impl_traits.is_empty() {
12941290
None
12951291
} else {
1296-
Some(Arc::new(Binders::new(num_binders, return_type_impl_traits)))
1292+
Some(Arc::new(make_binders(&generics, return_type_impl_traits)))
12971293
}
12981294
}
12991295

@@ -1303,3 +1299,7 @@ pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mut
13031299
hir_def::type_ref::Mutability::Mut => Mutability::Mut,
13041300
}
13051301
}
1302+
1303+
fn make_binders<T>(generics: &Generics, value: T) -> Binders<T> {
1304+
crate::make_only_type_binders(generics.len(), value)
1305+
}

crates/hir_ty/src/traits/chalk.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
184184
.db
185185
.return_type_impl_traits(func)
186186
.expect("impl trait id without impl traits");
187-
let data = &datas.skip_binders().impl_traits[idx as usize];
187+
let (datas, binders) = (*datas).as_ref().into_value_and_skipped_binders();
188+
let data = &datas.impl_traits[idx as usize];
188189
let bound = OpaqueTyDatumBound {
189190
bounds: make_binders(
190191
data.bounds
@@ -197,8 +198,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
197198
),
198199
where_clauses: make_binders(vec![], 0),
199200
};
200-
let num_vars = datas.num_binders;
201-
make_binders(bound, num_vars)
201+
chalk_ir::Binders::new(binders, bound)
202202
}
203203
crate::ImplTraitId::AsyncBlockTypeImplTrait(..) => {
204204
if let Some((future_trait, future_output)) = self
@@ -626,7 +626,7 @@ fn type_alias_associated_ty_value(
626626
let value = rust_ir::AssociatedTyValue {
627627
impl_id: impl_id.to_chalk(db),
628628
associated_ty_id: to_assoc_type_id(assoc_ty),
629-
value: make_binders(value_bound, binders),
629+
value: chalk_ir::Binders::new(binders, value_bound),
630630
};
631631
Arc::new(value)
632632
}
@@ -656,7 +656,7 @@ pub(crate) fn fn_def_datum_query(
656656
let datum = FnDefDatum {
657657
id: fn_def_id,
658658
sig: chalk_ir::FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: sig.is_varargs },
659-
binders: make_binders(bound, binders),
659+
binders: chalk_ir::Binders::new(binders, bound),
660660
};
661661
Arc::new(datum)
662662
}

0 commit comments

Comments
 (0)