@@ -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 }
0 commit comments