Skip to content

Commit 7ce0e9c

Browse files
Merge #8358
8358: Align FnPointer with Chalk r=flodiebold a=flodiebold CC #8313 Co-authored-by: Florian Diebold <flodiebold@gmail.com>
2 parents f25c1e7 + edc59d8 commit 7ce0e9c

File tree

9 files changed

+86
-40
lines changed

9 files changed

+86
-40
lines changed

crates/hir_ty/src/builder.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use smallvec::SmallVec;
1212

1313
use crate::{
1414
db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders,
15-
CallableSig, FnPointer, FnSig, GenericArg, Interner, ProjectionTy, Substitution, TraitRef, Ty,
16-
TyDefId, TyKind, TypeWalk, ValueTyDefId,
15+
CallableSig, FnPointer, FnSig, FnSubst, GenericArg, Interner, ProjectionTy, Substitution,
16+
TraitRef, Ty, TyDefId, TyKind, TypeWalk, ValueTyDefId,
1717
};
1818

1919
/// This is a builder for `Ty` or anything that needs a `Substitution`.
@@ -78,9 +78,12 @@ impl TyBuilder<()> {
7878

7979
pub fn fn_ptr(sig: CallableSig) -> Ty {
8080
TyKind::Function(FnPointer {
81-
num_args: sig.params().len(),
81+
num_binders: 0,
8282
sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs },
83-
substs: Substitution::from_iter(&Interner, sig.params_and_return.iter().cloned()),
83+
substitution: FnSubst(Substitution::from_iter(
84+
&Interner,
85+
sig.params_and_return.iter().cloned(),
86+
)),
8487
})
8588
.intern(&Interner)
8689
}

crates/hir_ty/src/infer/expr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::{
2222
to_chalk_trait_id,
2323
traits::{chalk::from_chalk, FnTrait},
2424
utils::{generics, variant_data, Generics},
25-
AdtId, Binders, CallableDefId, FnPointer, FnSig, InEnvironment, Interner, ProjectionTyExt,
26-
Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind,
25+
AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner,
26+
ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind,
2727
};
2828

2929
use super::{
@@ -260,9 +260,9 @@ impl<'a> InferenceContext<'a> {
260260
};
261261
sig_tys.push(ret_ty.clone());
262262
let sig_ty = TyKind::Function(FnPointer {
263-
num_args: sig_tys.len() - 1,
263+
num_binders: 0,
264264
sig: FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: false },
265-
substs: Substitution::from_iter(&Interner, sig_tys.clone()),
265+
substitution: FnSubst(Substitution::from_iter(&Interner, sig_tys.clone())),
266266
})
267267
.intern(&Interner);
268268
let closure_id = self.db.intern_closure((self.owner, tgt_expr)).into();

crates/hir_ty/src/infer/unify.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};
77

88
use super::{DomainGoal, InferenceContext};
99
use crate::{
10-
AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer,
10+
AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst,
1111
InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause,
1212
};
1313

@@ -308,8 +308,8 @@ impl InferenceTable {
308308
(TyKind::Adt(_, substs1), TyKind::Adt(_, substs2))
309309
| (TyKind::FnDef(_, substs1), TyKind::FnDef(_, substs2))
310310
| (
311-
TyKind::Function(FnPointer { substs: substs1, .. }),
312-
TyKind::Function(FnPointer { substs: substs2, .. }),
311+
TyKind::Function(FnPointer { substitution: FnSubst(substs1), .. }),
312+
TyKind::Function(FnPointer { substitution: FnSubst(substs2), .. }),
313313
)
314314
| (TyKind::Tuple(_, substs1), TyKind::Tuple(_, substs2))
315315
| (TyKind::OpaqueType(_, substs1), TyKind::OpaqueType(_, substs2))

crates/hir_ty/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ impl CallableSig {
142142
CallableSig {
143143
// FIXME: what to do about lifetime params? -> return PolyFnSig
144144
params_and_return: fn_ptr
145-
.substs
145+
.substitution
146146
.clone()
147147
.shifted_out_to(DebruijnIndex::ONE)
148148
.expect("unexpected lifetime vars in fn ptr")
149+
.0
149150
.interned()
150151
.iter()
151152
.map(|arg| arg.assert_ty_ref(&Interner).clone())
@@ -239,9 +240,9 @@ impl Ty {
239240
mutability == mutability2
240241
}
241242
(
242-
TyKind::Function(FnPointer { num_args, sig, .. }),
243-
TyKind::Function(FnPointer { num_args: num_args2, sig: sig2, .. }),
244-
) => num_args == num_args2 && sig == sig2,
243+
TyKind::Function(FnPointer { num_binders, sig, .. }),
244+
TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }),
245+
) => num_binders == num_binders2 && sig == sig2,
245246
(TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => {
246247
cardinality == cardinality2
247248
}
@@ -314,11 +315,11 @@ impl Ty {
314315
match self.kind(&Interner) {
315316
TyKind::Adt(_, substs)
316317
| TyKind::FnDef(_, substs)
317-
| TyKind::Function(FnPointer { substs, .. })
318318
| TyKind::Tuple(_, substs)
319319
| TyKind::OpaqueType(_, substs)
320320
| TyKind::AssociatedType(_, substs)
321321
| TyKind::Closure(.., substs) => Some(substs),
322+
TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&substs.0),
322323
_ => None,
323324
}
324325
}
@@ -327,11 +328,11 @@ impl Ty {
327328
match self.interned_mut() {
328329
TyKind::Adt(_, substs)
329330
| TyKind::FnDef(_, substs)
330-
| TyKind::Function(FnPointer { substs, .. })
331331
| TyKind::Tuple(_, substs)
332332
| TyKind::OpaqueType(_, substs)
333333
| TyKind::AssociatedType(_, substs)
334334
| TyKind::Closure(.., substs) => Some(substs),
335+
TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&mut substs.0),
335336
_ => None,
336337
}
337338
}

crates/hir_ty/src/lower.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ use crate::{
3434
variant_data, Generics,
3535
},
3636
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
37-
ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses,
38-
ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, Ty,
39-
TyBuilder, TyKind, TypeWalk, WhereClause,
37+
FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
38+
QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
39+
TraitEnvironment, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, WhereClause,
4040
};
4141

4242
#[derive(Debug)]
@@ -181,9 +181,9 @@ impl<'a> TyLoweringContext<'a> {
181181
let substs =
182182
Substitution::from_iter(&Interner, params.iter().map(|tr| self.lower_ty(tr)));
183183
TyKind::Function(FnPointer {
184-
num_args: substs.len(&Interner) - 1,
184+
num_binders: 0, // FIXME lower `for<'a> fn()` correctly
185185
sig: FnSig { abi: (), safety: Safety::Safe, variadic: *is_varargs },
186-
substs,
186+
substitution: FnSubst(substs),
187187
})
188188
.intern(&Interner)
189189
}

crates/hir_ty/src/method_resolution.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ impl TyFingerprint {
4646
/// have impls: if we have some `struct S`, we can have an `impl S`, but not
4747
/// `impl &S`. Hence, this will return `None` for reference types and such.
4848
pub fn for_impl(ty: &Ty) -> Option<TyFingerprint> {
49-
let fp = match *ty.kind(&Interner) {
49+
let fp = match ty.kind(&Interner) {
5050
TyKind::Str => TyFingerprint::Str,
5151
TyKind::Never => TyFingerprint::Never,
5252
TyKind::Slice(..) => TyFingerprint::Slice,
5353
TyKind::Array(..) => TyFingerprint::Array,
54-
TyKind::Scalar(scalar) => TyFingerprint::Scalar(scalar),
55-
TyKind::Adt(AdtId(adt), _) => TyFingerprint::Adt(adt),
56-
TyKind::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality),
57-
TyKind::Raw(mutability, ..) => TyFingerprint::RawPtr(mutability),
58-
TyKind::Foreign(alias_id, ..) => TyFingerprint::ForeignType(alias_id),
59-
TyKind::Function(FnPointer { num_args, sig, .. }) => {
60-
TyFingerprint::FnPtr(num_args, sig)
54+
TyKind::Scalar(scalar) => TyFingerprint::Scalar(*scalar),
55+
TyKind::Adt(AdtId(adt), _) => TyFingerprint::Adt(*adt),
56+
TyKind::Tuple(cardinality, _) => TyFingerprint::Tuple(*cardinality),
57+
TyKind::Raw(mutability, ..) => TyFingerprint::RawPtr(*mutability),
58+
TyKind::Foreign(alias_id, ..) => TyFingerprint::ForeignType(*alias_id),
59+
TyKind::Function(FnPointer { sig, substitution: substs, .. }) => {
60+
TyFingerprint::FnPtr(substs.0.len(&Interner) - 1, *sig)
6161
}
6262
TyKind::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?,
6363
_ => return None,

crates/hir_ty/src/traits/chalk/mapping.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ impl ToChalk for Ty {
2424
match self.into_inner() {
2525
TyKind::Ref(m, ty) => ref_to_chalk(db, m, ty),
2626
TyKind::Array(ty) => array_to_chalk(db, ty),
27-
TyKind::Function(FnPointer { sig, substs, .. }) => {
28-
let substitution = chalk_ir::FnSubst(substs.to_chalk(db).shifted_in(&Interner));
27+
TyKind::Function(FnPointer { sig, substitution: substs, .. }) => {
28+
let substitution = chalk_ir::FnSubst(substs.0.to_chalk(db).shifted_in(&Interner));
2929
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
3030
num_binders: 0,
3131
sig,
@@ -132,11 +132,11 @@ impl ToChalk for Ty {
132132
..
133133
}) => {
134134
assert_eq!(num_binders, 0);
135-
let substs: Substitution = from_chalk(
135+
let substs = crate::FnSubst(from_chalk(
136136
db,
137137
substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"),
138-
);
139-
TyKind::Function(FnPointer { num_args: (substs.len(&Interner) - 1), sig, substs })
138+
));
139+
TyKind::Function(FnPointer { num_binders, sig, substitution: substs })
140140
}
141141
chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx),
142142
chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error,

crates/hir_ty/src/types.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use smallvec::SmallVec;
1111

1212
use crate::{
1313
AssocTypeId, CanonicalVarKinds, ChalkTraitId, ClosureId, FnDefId, FnSig, ForeignDefId,
14-
InferenceVar, Interner, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKinds,
14+
InferenceVar, Interner, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKind, VariableKinds,
1515
};
1616

1717
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
@@ -43,9 +43,36 @@ pub struct DynTy {
4343

4444
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
4545
pub struct FnPointer {
46-
pub num_args: usize,
46+
pub num_binders: usize,
4747
pub sig: FnSig,
48-
pub substs: Substitution,
48+
pub substitution: FnSubst,
49+
}
50+
/// A wrapper for the substs on a Fn.
51+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
52+
pub struct FnSubst(pub Substitution);
53+
54+
impl FnPointer {
55+
/// Represent the current `Fn` as if it was wrapped in `Binders`
56+
pub fn into_binders(self, interner: &Interner) -> Binders<FnSubst> {
57+
Binders::new(
58+
VariableKinds::from_iter(
59+
interner,
60+
(0..self.num_binders).map(|_| VariableKind::Lifetime),
61+
),
62+
self.substitution,
63+
)
64+
}
65+
66+
/// Represent the current `Fn` as if it was wrapped in `Binders`
67+
pub fn as_binders(&self, interner: &Interner) -> Binders<&FnSubst> {
68+
Binders::new(
69+
VariableKinds::from_iter(
70+
interner,
71+
(0..self.num_binders).map(|_| VariableKind::Lifetime),
72+
),
73+
&self.substitution,
74+
)
75+
}
4976
}
5077

5178
#[derive(Clone, PartialEq, Eq, Debug, Hash)]

crates/hir_ty/src/walk.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use std::mem;
66
use chalk_ir::DebruijnIndex;
77

88
use crate::{
9-
utils::make_mut_slice, AliasEq, AliasTy, Binders, CallableSig, GenericArg, GenericArgData,
10-
Interner, OpaqueTy, ProjectionTy, Substitution, TraitRef, Ty, TyKind, WhereClause,
9+
utils::make_mut_slice, AliasEq, AliasTy, Binders, CallableSig, FnSubst, GenericArg,
10+
GenericArgData, Interner, OpaqueTy, ProjectionTy, Substitution, TraitRef, Ty, TyKind,
11+
WhereClause,
1112
};
1213

1314
/// This allows walking structures that contain types to do something with those
@@ -381,3 +382,17 @@ impl TypeWalk for AliasEq {
381382
}
382383
}
383384
}
385+
386+
impl TypeWalk for FnSubst {
387+
fn walk(&self, f: &mut impl FnMut(&Ty)) {
388+
self.0.walk(f)
389+
}
390+
391+
fn walk_mut_binders(
392+
&mut self,
393+
f: &mut impl FnMut(&mut Ty, DebruijnIndex),
394+
binders: DebruijnIndex,
395+
) {
396+
self.0.walk_mut_binders(f, binders)
397+
}
398+
}

0 commit comments

Comments
 (0)