Skip to content

Commit 5df3ee8

Browse files
bors[bot]Veykril
andauthored
Merge #7816
7816: Lift Ty::Fn into a struct r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 72457d0 + 407196b commit 5df3ee8

File tree

9 files changed

+98
-86
lines changed

9 files changed

+98
-86
lines changed

crates/hir/src/code_model.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ use hir_ty::{
3131
display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter},
3232
method_resolution,
3333
traits::{FnTrait, Solution, SolutionVariables},
34-
BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, InEnvironment,
35-
Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, Ty, TyDefId,
36-
TyKind,
34+
BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate,
35+
InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment,
36+
Ty, TyDefId, TyKind,
3737
};
3838
use rustc_hash::FxHashSet;
3939
use stdx::{format_to, impl_from};
@@ -1692,7 +1692,7 @@ impl Type {
16921692
}
16931693

16941694
pub fn is_fn(&self) -> bool {
1695-
matches!(&self.ty.value, Ty::FnDef(..) | Ty::FnPtr { .. })
1695+
matches!(&self.ty.value, Ty::FnDef(..) | Ty::Function { .. })
16961696
}
16971697

16981698
pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
@@ -1974,7 +1974,7 @@ impl HirDisplay for Type {
19741974
#[derive(Debug)]
19751975
pub struct Callable {
19761976
ty: Type,
1977-
sig: FnSig,
1977+
sig: CallableSig,
19781978
def: Option<CallableDefId>,
19791979
pub(crate) is_bound_method: bool,
19801980
}

crates/hir_ty/src/display.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use std::{borrow::Cow, fmt};
44

55
use crate::{
6-
db::HirDatabase, primitive, utils::generics, CallableDefId, FnSig, GenericPredicate, Lifetime,
7-
Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty,
6+
db::HirDatabase, primitive, utils::generics, CallableDefId, CallableSig, GenericPredicate,
7+
Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty,
88
};
99
use arrayvec::ArrayVec;
1010
use hir_def::{
@@ -341,8 +341,8 @@ impl HirDisplay for Ty {
341341
write!(f, ")")?;
342342
}
343343
}
344-
Ty::FnPtr { is_varargs, substs, .. } => {
345-
let sig = FnSig::from_fn_ptr_substs(&substs, *is_varargs);
344+
Ty::Function(fn_ptr) => {
345+
let sig = CallableSig::from_fn_ptr(fn_ptr);
346346
sig.hir_fmt(f)?;
347347
}
348348
Ty::FnDef(def, parameters) => {
@@ -494,7 +494,7 @@ impl HirDisplay for Ty {
494494
}
495495
}
496496
}
497-
Ty::Closure { substs, .. } => {
497+
Ty::Closure(.., substs) => {
498498
let sig = substs[0].callable_sig(f.db);
499499
if let Some(sig) = sig {
500500
if sig.params().is_empty() {
@@ -571,7 +571,7 @@ impl HirDisplay for Ty {
571571
}
572572
}
573573

574-
impl HirDisplay for FnSig {
574+
impl HirDisplay for CallableSig {
575575
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
576576
write!(f, "fn(")?;
577577
f.write_joined(self.params(), ", ")?;

crates/hir_ty/src/infer/coerce.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ impl<'a> InferenceContext<'a> {
8989
| (Ty::Ref(Mutability::Shared, ..), Ty::Ref(Mutability::Mut, ..)) => return false,
9090

9191
// `{function_type}` -> `fn()`
92-
(Ty::FnDef(..), Ty::FnPtr { .. }) => match from_ty.callable_sig(self.db) {
92+
(Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) {
9393
None => return false,
9494
Some(sig) => {
9595
from_ty = Ty::fn_ptr(sig);
9696
}
9797
},
9898

99-
(Ty::Closure { substs, .. }, Ty::FnPtr { .. }) => {
99+
(Ty::Closure(.., substs), Ty::Function { .. }) => {
100100
from_ty = substs[0].clone();
101101
}
102102

crates/hir_ty/src/infer/expr.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::{
1818
primitive::{self, UintTy},
1919
traits::{FnTrait, InEnvironment},
2020
utils::{generics, variant_data, Generics},
21-
Binders, CallableDefId, InferTy, Mutability, Obligation, OpaqueTyId, Rawness, Scalar, Substs,
22-
TraitRef, Ty,
21+
Binders, CallableDefId, FnPointer, FnSig, InferTy, Mutability, Obligation, OpaqueTyId, Rawness,
22+
Scalar, Substs, TraitRef, Ty,
2323
};
2424

2525
use super::{
@@ -247,13 +247,12 @@ impl<'a> InferenceContext<'a> {
247247
None => self.table.new_type_var(),
248248
};
249249
sig_tys.push(ret_ty.clone());
250-
let sig_ty = Ty::FnPtr {
251-
num_args: sig_tys.len() as u16 - 1,
252-
is_varargs: false,
250+
let sig_ty = Ty::Function(FnPointer {
251+
num_args: sig_tys.len() - 1,
252+
sig: FnSig { variadic: false },
253253
substs: Substs(sig_tys.clone().into()),
254-
};
255-
let closure_ty =
256-
Ty::Closure { def: self.owner, expr: tgt_expr, substs: Substs::single(sig_ty) };
254+
});
255+
let closure_ty = Ty::Closure(self.owner, tgt_expr, Substs::single(sig_ty));
257256

258257
// Eagerly try to relate the closure type with the expected
259258
// type, otherwise we often won't have enough information to

crates/hir_ty/src/lib.rs

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ impl TypeWalk for ProjectionTy {
9999
}
100100
}
101101

102+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
103+
pub struct FnSig {
104+
pub variadic: bool,
105+
}
106+
107+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
108+
pub struct FnPointer {
109+
pub num_args: usize,
110+
pub sig: FnSig,
111+
pub substs: Substs,
112+
}
113+
102114
/// A type.
103115
///
104116
/// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents
@@ -166,7 +178,7 @@ pub enum Ty {
166178
///
167179
/// The closure signature is stored in a `FnPtr` type in the first type
168180
/// parameter.
169-
Closure { def: DefWithBodyId, expr: ExprId, substs: Substs },
181+
Closure(DefWithBodyId, ExprId, Substs),
170182

171183
/// Represents a foreign type declared in external blocks.
172184
ForeignType(TypeAliasId),
@@ -179,8 +191,7 @@ pub enum Ty {
179191
/// fn foo() -> i32 { 1 }
180192
/// let bar: fn() -> i32 = foo;
181193
/// ```
182-
// FIXME make this a Ty variant like in Chalk
183-
FnPtr { num_args: u16, is_varargs: bool, substs: Substs },
194+
Function(FnPointer),
184195

185196
/// A "projection" type corresponds to an (unnormalized)
186197
/// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the
@@ -535,22 +546,29 @@ pub enum TyKind {
535546
/// A function signature as seen by type inference: Several parameter types and
536547
/// one return type.
537548
#[derive(Clone, PartialEq, Eq, Debug)]
538-
pub struct FnSig {
549+
pub struct CallableSig {
539550
params_and_return: Arc<[Ty]>,
540551
is_varargs: bool,
541552
}
542553

543554
/// A polymorphic function signature.
544-
pub type PolyFnSig = Binders<FnSig>;
555+
pub type PolyFnSig = Binders<CallableSig>;
545556

546-
impl FnSig {
547-
pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> FnSig {
557+
impl CallableSig {
558+
pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig {
548559
params.push(ret);
549-
FnSig { params_and_return: params.into(), is_varargs }
560+
CallableSig { params_and_return: params.into(), is_varargs }
561+
}
562+
563+
pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
564+
CallableSig {
565+
params_and_return: Arc::clone(&fn_ptr.substs.0),
566+
is_varargs: fn_ptr.sig.variadic,
567+
}
550568
}
551569

552-
pub fn from_fn_ptr_substs(substs: &Substs, is_varargs: bool) -> FnSig {
553-
FnSig { params_and_return: Arc::clone(&substs.0), is_varargs }
570+
pub fn from_substs(substs: &Substs) -> CallableSig {
571+
CallableSig { params_and_return: Arc::clone(&substs.0), is_varargs: false }
554572
}
555573

556574
pub fn params(&self) -> &[Ty] {
@@ -562,7 +580,7 @@ impl FnSig {
562580
}
563581
}
564582

565-
impl TypeWalk for FnSig {
583+
impl TypeWalk for CallableSig {
566584
fn walk(&self, f: &mut impl FnMut(&Ty)) {
567585
for t in self.params_and_return.iter() {
568586
t.walk(f);
@@ -585,12 +603,12 @@ impl Ty {
585603
Ty::Tuple(0, Substs::empty())
586604
}
587605

588-
pub fn fn_ptr(sig: FnSig) -> Self {
589-
Ty::FnPtr {
590-
num_args: sig.params().len() as u16,
591-
is_varargs: sig.is_varargs,
606+
pub fn fn_ptr(sig: CallableSig) -> Self {
607+
Ty::Function(FnPointer {
608+
num_args: sig.params().len(),
609+
sig: FnSig { variadic: sig.is_varargs },
592610
substs: Substs(sig.params_and_return),
593-
}
611+
})
594612
}
595613

596614
pub fn builtin(builtin: BuiltinType) -> Self {
@@ -673,17 +691,17 @@ impl Ty {
673691
(Ty::OpaqueType(ty_id, ..), Ty::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
674692
(Ty::AssociatedType(ty_id, ..), Ty::AssociatedType(ty_id2, ..))
675693
| (Ty::ForeignType(ty_id, ..), Ty::ForeignType(ty_id2, ..)) => ty_id == ty_id2,
676-
(Ty::Closure { def, expr, .. }, Ty::Closure { def: def2, expr: expr2, .. }) => {
694+
(Ty::Closure(def, expr, _), Ty::Closure(def2, expr2, _)) => {
677695
expr == expr2 && def == def2
678696
}
679697
(Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..))
680698
| (Ty::RawPtr(mutability, ..), Ty::RawPtr(mutability2, ..)) => {
681699
mutability == mutability2
682700
}
683701
(
684-
Ty::FnPtr { num_args, is_varargs, .. },
685-
Ty::FnPtr { num_args: num_args2, is_varargs: is_varargs2, .. },
686-
) => num_args == num_args2 && is_varargs == is_varargs2,
702+
Ty::Function(FnPointer { num_args, sig, .. }),
703+
Ty::Function(FnPointer { num_args: num_args2, sig: sig2, .. }),
704+
) => num_args == num_args2 && sig == sig2,
687705
(Ty::Tuple(cardinality, _), Ty::Tuple(cardinality2, _)) => cardinality == cardinality2,
688706
(Ty::Str, Ty::Str) | (Ty::Never, Ty::Never) => true,
689707
(Ty::Scalar(scalar), Ty::Scalar(scalar2)) => scalar == scalar2,
@@ -722,17 +740,15 @@ impl Ty {
722740
}
723741
}
724742

725-
pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> {
743+
pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> {
726744
match self {
727-
Ty::FnPtr { is_varargs, substs: parameters, .. } => {
728-
Some(FnSig::from_fn_ptr_substs(&parameters, *is_varargs))
729-
}
745+
Ty::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)),
730746
Ty::FnDef(def, parameters) => {
731747
let sig = db.callable_item_signature(*def);
732748
Some(sig.subst(&parameters))
733749
}
734-
Ty::Closure { substs: parameters, .. } => {
735-
let sig_param = &parameters[0];
750+
Ty::Closure(.., substs) => {
751+
let sig_param = &substs[0];
736752
sig_param.callable_sig(db)
737753
}
738754
_ => None,
@@ -751,11 +767,11 @@ impl Ty {
751767
| Ty::RawPtr(_, substs)
752768
| Ty::Ref(_, substs)
753769
| Ty::FnDef(_, substs)
754-
| Ty::FnPtr { substs, .. }
770+
| Ty::Function(FnPointer { substs, .. })
755771
| Ty::Tuple(_, substs)
756772
| Ty::OpaqueType(_, substs)
757773
| Ty::AssociatedType(_, substs)
758-
| Ty::Closure { substs, .. } => {
774+
| Ty::Closure(.., substs) => {
759775
assert_eq!(substs.len(), new_substs.len());
760776
*substs = new_substs;
761777
}
@@ -774,11 +790,11 @@ impl Ty {
774790
| Ty::RawPtr(_, substs)
775791
| Ty::Ref(_, substs)
776792
| Ty::FnDef(_, substs)
777-
| Ty::FnPtr { substs, .. }
793+
| Ty::Function(FnPointer { substs, .. })
778794
| Ty::Tuple(_, substs)
779795
| Ty::OpaqueType(_, substs)
780796
| Ty::AssociatedType(_, substs)
781-
| Ty::Closure { substs, .. } => Some(substs),
797+
| Ty::Closure(.., substs) => Some(substs),
782798
_ => None,
783799
}
784800
}
@@ -791,11 +807,11 @@ impl Ty {
791807
| Ty::RawPtr(_, substs)
792808
| Ty::Ref(_, substs)
793809
| Ty::FnDef(_, substs)
794-
| Ty::FnPtr { substs, .. }
810+
| Ty::Function(FnPointer { substs, .. })
795811
| Ty::Tuple(_, substs)
796812
| Ty::OpaqueType(_, substs)
797813
| Ty::AssociatedType(_, substs)
798-
| Ty::Closure { substs, .. } => Some(substs),
814+
| Ty::Closure(.., substs) => Some(substs),
799815
_ => None,
800816
}
801817
}

crates/hir_ty/src/lower.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ use crate::{
3131
all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
3232
make_mut_slice, variant_data,
3333
},
34-
Binders, BoundVar, DebruijnIndex, FnSig, GenericPredicate, OpaqueTy, OpaqueTyId, PolyFnSig,
35-
ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, ReturnTypeImplTraits, Substs,
36-
TraitEnvironment, TraitRef, Ty, TypeWalk,
34+
Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate, OpaqueTy,
35+
OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait,
36+
ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk,
3737
};
3838

3939
#[derive(Debug)]
@@ -173,8 +173,12 @@ impl Ty {
173173
}
174174
TypeRef::Placeholder => Ty::Unknown,
175175
TypeRef::Fn(params, is_varargs) => {
176-
let sig = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect());
177-
Ty::FnPtr { num_args: sig.len() as u16 - 1, is_varargs: *is_varargs, substs: sig }
176+
let substs = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect());
177+
Ty::Function(FnPointer {
178+
num_args: substs.len() - 1,
179+
sig: FnSig { variadic: *is_varargs },
180+
substs,
181+
})
178182
}
179183
TypeRef::DynTrait(bounds) => {
180184
let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0));
@@ -1010,7 +1014,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
10101014
let ret = Ty::from_hir(&ctx_ret, &data.ret_type);
10111015
let generics = generics(db.upcast(), def.into());
10121016
let num_binders = generics.len();
1013-
Binders::new(num_binders, FnSig::from_params_and_return(params, ret, data.is_varargs))
1017+
Binders::new(num_binders, CallableSig::from_params_and_return(params, ret, data.is_varargs))
10141018
}
10151019

10161020
/// Build the declared type of a function. This should not need to look at the
@@ -1050,7 +1054,7 @@ fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnS
10501054
let params =
10511055
fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>();
10521056
let ret = type_for_adt(db, def.into());
1053-
Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false))
1057+
Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false))
10541058
}
10551059

10561060
/// Build the type of a tuple struct constructor.
@@ -1074,7 +1078,7 @@ fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId)
10741078
let params =
10751079
fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>();
10761080
let ret = type_for_adt(db, def.parent.into());
1077-
Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false))
1081+
Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false))
10781082
}
10791083

10801084
/// Build the type of a tuple enum variant constructor.

crates/hir_ty/src/method_resolution.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::{
1818
db::HirDatabase,
1919
primitive::{self, FloatTy, IntTy, UintTy},
2020
utils::all_super_traits,
21-
Canonical, DebruijnIndex, InEnvironment, Scalar, Substs, TraitEnvironment, TraitRef, Ty,
22-
TyKind, TypeWalk,
21+
Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Scalar, Substs, TraitEnvironment,
22+
TraitRef, Ty, TyKind, TypeWalk,
2323
};
2424

2525
/// This is used as a key for indexing impls.
@@ -35,7 +35,7 @@ pub enum TyFingerprint {
3535
Dyn(TraitId),
3636
Tuple(usize),
3737
ForeignType(TypeAliasId),
38-
FnPtr { num_args: u16, is_varargs: bool },
38+
FnPtr(usize, FnSig),
3939
}
4040

4141
impl TyFingerprint {
@@ -53,9 +53,7 @@ impl TyFingerprint {
5353
&Ty::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality),
5454
&Ty::RawPtr(mutability, ..) => TyFingerprint::RawPtr(mutability),
5555
&Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id),
56-
&Ty::FnPtr { num_args, is_varargs, .. } => {
57-
TyFingerprint::FnPtr { num_args, is_varargs }
58-
}
56+
&Ty::Function(FnPointer { num_args, sig, .. }) => TyFingerprint::FnPtr(num_args, sig),
5957
Ty::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?,
6058
_ => return None,
6159
};

0 commit comments

Comments
 (0)