@@ -879,11 +879,22 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
879
879
ty
880
880
}
881
881
882
- fn unify_substs ( & mut self , substs1 : & Substs , substs2 : & Substs ) -> bool {
883
- substs1. 0 . iter ( ) . zip ( substs2. 0 . iter ( ) ) . all ( |( t1, t2) | self . unify ( t1, t2) )
882
+ fn unify_substs ( & mut self , substs1 : & Substs , substs2 : & Substs , depth : usize ) -> bool {
883
+ substs1. 0 . iter ( ) . zip ( substs2. 0 . iter ( ) ) . all ( |( t1, t2) | self . unify_inner ( t1, t2, depth ) )
884
884
}
885
885
886
886
fn unify ( & mut self , ty1 : & Ty , ty2 : & Ty ) -> bool {
887
+ self . unify_inner ( ty1, ty2, 0 )
888
+ }
889
+
890
+ fn unify_inner ( & mut self , ty1 : & Ty , ty2 : & Ty , depth : usize ) -> bool {
891
+ if depth > 1000 {
892
+ // prevent stackoverflows
893
+ panic ! ( "infinite recursion in unification" ) ;
894
+ }
895
+ if ty1 == ty2 {
896
+ return true ;
897
+ }
887
898
// try to resolve type vars first
888
899
let ty1 = self . resolve_ty_shallow ( ty1) ;
889
900
let ty2 = self . resolve_ty_shallow ( ty2) ;
@@ -904,13 +915,15 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
904
915
(
905
916
Ty :: Adt { def_id : def_id1, substs : substs1, .. } ,
906
917
Ty :: Adt { def_id : def_id2, substs : substs2, .. } ,
907
- ) if def_id1 == def_id2 => self . unify_substs ( substs1, substs2) ,
908
- ( Ty :: Slice ( t1) , Ty :: Slice ( t2) ) => self . unify ( t1, t2) ,
909
- ( Ty :: RawPtr ( t1, m1) , Ty :: RawPtr ( t2, m2) ) if m1 == m2 => self . unify ( t1, t2) ,
910
- ( Ty :: Ref ( t1, m1) , Ty :: Ref ( t2, m2) ) if m1 == m2 => self . unify ( t1, t2) ,
918
+ ) if def_id1 == def_id2 => self . unify_substs ( substs1, substs2, depth + 1 ) ,
919
+ ( Ty :: Slice ( t1) , Ty :: Slice ( t2) ) => self . unify_inner ( t1, t2, depth + 1 ) ,
920
+ ( Ty :: RawPtr ( t1, m1) , Ty :: RawPtr ( t2, m2) ) if m1 == m2 => {
921
+ self . unify_inner ( t1, t2, depth + 1 )
922
+ }
923
+ ( Ty :: Ref ( t1, m1) , Ty :: Ref ( t2, m2) ) if m1 == m2 => self . unify_inner ( t1, t2, depth + 1 ) ,
911
924
( Ty :: FnPtr ( sig1) , Ty :: FnPtr ( sig2) ) if sig1 == sig2 => true ,
912
925
( Ty :: Tuple ( ts1) , Ty :: Tuple ( ts2) ) if ts1. len ( ) == ts2. len ( ) => {
913
- ts1. iter ( ) . zip ( ts2. iter ( ) ) . all ( |( t1, t2) | self . unify ( t1, t2) )
926
+ ts1. iter ( ) . zip ( ts2. iter ( ) ) . all ( |( t1, t2) | self . unify_inner ( t1, t2, depth + 1 ) )
914
927
}
915
928
( Ty :: Infer ( InferTy :: TypeVar ( tv1) ) , Ty :: Infer ( InferTy :: TypeVar ( tv2) ) )
916
929
| ( Ty :: Infer ( InferTy :: IntVar ( tv1) ) , Ty :: Infer ( InferTy :: IntVar ( tv2) ) )
0 commit comments