@@ -219,22 +219,42 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
219
219
obligation_args : I :: GenericArgs ,
220
220
impl_args : I :: GenericArgs ,
221
221
) -> bool {
222
+ self . args_may_unify_inner ( obligation_args, impl_args, 10 )
223
+ }
224
+
225
+ pub fn types_may_unify ( self , lhs : I :: Ty , rhs : I :: Ty ) -> bool {
226
+ self . types_may_unify_inner ( lhs, rhs, 10 )
227
+ }
228
+
229
+ fn args_may_unify_inner (
230
+ self ,
231
+ obligation_args : I :: GenericArgs ,
232
+ impl_args : I :: GenericArgs ,
233
+ depth : usize ,
234
+ ) -> bool {
235
+ if depth == 0 {
236
+ return true ;
237
+ }
238
+
222
239
iter:: zip ( obligation_args. iter ( ) , impl_args. iter ( ) ) . all ( |( obl, imp) | {
223
240
match ( obl. kind ( ) , imp. kind ( ) ) {
224
241
// We don't fast reject based on regions.
225
242
( ty:: GenericArgKind :: Lifetime ( _) , ty:: GenericArgKind :: Lifetime ( _) ) => true ,
226
243
( ty:: GenericArgKind :: Type ( obl) , ty:: GenericArgKind :: Type ( imp) ) => {
227
- self . types_may_unify ( obl, imp)
244
+ self . types_may_unify_inner ( obl, imp, depth - 1 )
228
245
}
229
246
( ty:: GenericArgKind :: Const ( obl) , ty:: GenericArgKind :: Const ( imp) ) => {
230
- self . consts_may_unify ( obl, imp)
247
+ self . consts_may_unify_inner ( obl, imp, depth - 1 )
231
248
}
232
249
_ => panic ! ( "kind mismatch: {obl:?} {imp:?}" ) ,
233
250
}
234
251
} )
235
252
}
236
253
237
- pub fn types_may_unify ( self , lhs : I :: Ty , rhs : I :: Ty ) -> bool {
254
+ fn types_may_unify_inner ( self , lhs : I :: Ty , rhs : I :: Ty , depth : usize ) -> bool {
255
+ if depth == 0 {
256
+ return true ;
257
+ }
238
258
match rhs. kind ( ) {
239
259
// Start by checking whether the `rhs` type may unify with
240
260
// pretty much everything. Just return `true` in that case.
@@ -277,14 +297,14 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
277
297
match lhs. kind ( ) {
278
298
ty:: Ref ( _, lhs_ty, lhs_mutbl) => match rhs. kind ( ) {
279
299
ty:: Ref ( _, rhs_ty, rhs_mutbl) => {
280
- lhs_mutbl == rhs_mutbl && self . types_may_unify ( lhs_ty, rhs_ty)
300
+ lhs_mutbl == rhs_mutbl && self . types_may_unify_inner ( lhs_ty, rhs_ty, depth - 1 )
281
301
}
282
302
_ => false ,
283
303
} ,
284
304
285
305
ty:: Adt ( lhs_def, lhs_args) => match rhs. kind ( ) {
286
306
ty:: Adt ( rhs_def, rhs_args) => {
287
- lhs_def == rhs_def && self . args_may_unify ( lhs_args, rhs_args)
307
+ lhs_def == rhs_def && self . args_may_unify_inner ( lhs_args, rhs_args, depth - 1 )
288
308
}
289
309
_ => false ,
290
310
} ,
@@ -326,27 +346,28 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
326
346
ty:: Tuple ( rhs) => {
327
347
lhs. len ( ) == rhs. len ( )
328
348
&& iter:: zip ( lhs. iter ( ) , rhs. iter ( ) )
329
- . all ( |( lhs, rhs) | self . types_may_unify ( lhs, rhs) )
349
+ . all ( |( lhs, rhs) | self . types_may_unify_inner ( lhs, rhs, depth - 1 ) )
330
350
}
331
351
_ => false ,
332
352
} ,
333
353
334
354
ty:: Array ( lhs_ty, lhs_len) => match rhs. kind ( ) {
335
355
ty:: Array ( rhs_ty, rhs_len) => {
336
- self . types_may_unify ( lhs_ty, rhs_ty) && self . consts_may_unify ( lhs_len, rhs_len)
356
+ self . types_may_unify_inner ( lhs_ty, rhs_ty, depth - 1 )
357
+ && self . consts_may_unify_inner ( lhs_len, rhs_len, depth - 1 )
337
358
}
338
359
_ => false ,
339
360
} ,
340
361
341
362
ty:: RawPtr ( lhs_ty, lhs_mutbl) => match rhs. kind ( ) {
342
363
ty:: RawPtr ( rhs_ty, rhs_mutbl) => {
343
- lhs_mutbl == rhs_mutbl && self . types_may_unify ( lhs_ty, rhs_ty)
364
+ lhs_mutbl == rhs_mutbl && self . types_may_unify_inner ( lhs_ty, rhs_ty, depth - 1 )
344
365
}
345
366
_ => false ,
346
367
} ,
347
368
348
369
ty:: Slice ( lhs_ty) => {
349
- matches ! ( rhs. kind( ) , ty:: Slice ( rhs_ty) if self . types_may_unify ( lhs_ty, rhs_ty) )
370
+ matches ! ( rhs. kind( ) , ty:: Slice ( rhs_ty) if self . types_may_unify_inner ( lhs_ty, rhs_ty, depth - 1 ) )
350
371
}
351
372
352
373
ty:: Dynamic ( lhs_preds, ..) => {
@@ -366,7 +387,7 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
366
387
lhs_hdr == rhs_hdr
367
388
&& lhs_sig_tys. len ( ) == rhs_sig_tys. len ( )
368
389
&& iter:: zip ( lhs_sig_tys. iter ( ) , rhs_sig_tys. iter ( ) )
369
- . all ( |( lhs, rhs) | self . types_may_unify ( lhs, rhs) )
390
+ . all ( |( lhs, rhs) | self . types_may_unify_inner ( lhs, rhs, depth - 1 ) )
370
391
}
371
392
_ => false ,
372
393
} ,
@@ -375,49 +396,57 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
375
396
376
397
ty:: FnDef ( lhs_def_id, lhs_args) => match rhs. kind ( ) {
377
398
ty:: FnDef ( rhs_def_id, rhs_args) => {
378
- lhs_def_id == rhs_def_id && self . args_may_unify ( lhs_args, rhs_args)
399
+ lhs_def_id == rhs_def_id
400
+ && self . args_may_unify_inner ( lhs_args, rhs_args, depth - 1 )
379
401
}
380
402
_ => false ,
381
403
} ,
382
404
383
405
ty:: Closure ( lhs_def_id, lhs_args) => match rhs. kind ( ) {
384
406
ty:: Closure ( rhs_def_id, rhs_args) => {
385
- lhs_def_id == rhs_def_id && self . args_may_unify ( lhs_args, rhs_args)
407
+ lhs_def_id == rhs_def_id
408
+ && self . args_may_unify_inner ( lhs_args, rhs_args, depth - 1 )
386
409
}
387
410
_ => false ,
388
411
} ,
389
412
390
413
ty:: CoroutineClosure ( lhs_def_id, lhs_args) => match rhs. kind ( ) {
391
414
ty:: CoroutineClosure ( rhs_def_id, rhs_args) => {
392
- lhs_def_id == rhs_def_id && self . args_may_unify ( lhs_args, rhs_args)
415
+ lhs_def_id == rhs_def_id
416
+ && self . args_may_unify_inner ( lhs_args, rhs_args, depth - 1 )
393
417
}
394
418
_ => false ,
395
419
} ,
396
420
397
421
ty:: Coroutine ( lhs_def_id, lhs_args) => match rhs. kind ( ) {
398
422
ty:: Coroutine ( rhs_def_id, rhs_args) => {
399
- lhs_def_id == rhs_def_id && self . args_may_unify ( lhs_args, rhs_args)
423
+ lhs_def_id == rhs_def_id
424
+ && self . args_may_unify_inner ( lhs_args, rhs_args, depth - 1 )
400
425
}
401
426
_ => false ,
402
427
} ,
403
428
404
429
ty:: CoroutineWitness ( lhs_def_id, lhs_args) => match rhs. kind ( ) {
405
430
ty:: CoroutineWitness ( rhs_def_id, rhs_args) => {
406
- lhs_def_id == rhs_def_id && self . args_may_unify ( lhs_args, rhs_args)
431
+ lhs_def_id == rhs_def_id
432
+ && self . args_may_unify_inner ( lhs_args, rhs_args, depth - 1 )
407
433
}
408
434
_ => false ,
409
435
} ,
410
436
411
437
ty:: Pat ( lhs_ty, _) => {
412
438
// FIXME(pattern_types): take pattern into account
413
- matches ! ( rhs. kind( ) , ty:: Pat ( rhs_ty, _) if self . types_may_unify ( lhs_ty, rhs_ty) )
439
+ matches ! ( rhs. kind( ) , ty:: Pat ( rhs_ty, _) if self . types_may_unify_inner ( lhs_ty, rhs_ty, depth - 1 ) )
414
440
}
415
441
416
442
ty:: Error ( ..) => true ,
417
443
}
418
444
}
419
445
420
- pub fn consts_may_unify ( self , lhs : I :: Const , rhs : I :: Const ) -> bool {
446
+ fn consts_may_unify_inner ( self , lhs : I :: Const , rhs : I :: Const , depth : usize ) -> bool {
447
+ if depth == 0 {
448
+ return true ;
449
+ }
421
450
match rhs. kind ( ) {
422
451
ty:: ConstKind :: Param ( _) => {
423
452
if INSTANTIATE_RHS_WITH_INFER {
0 commit comments