Skip to content

Commit 77dceb9

Browse files
committedNov 28, 2024
Use depth for drcx
1 parent 6b6a867 commit 77dceb9

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed
 

‎compiler/rustc_type_ir/src/fast_reject.rs

+46-17
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,42 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
219219
obligation_args: I::GenericArgs,
220220
impl_args: I::GenericArgs,
221221
) -> 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+
222239
iter::zip(obligation_args.iter(), impl_args.iter()).all(|(obl, imp)| {
223240
match (obl.kind(), imp.kind()) {
224241
// We don't fast reject based on regions.
225242
(ty::GenericArgKind::Lifetime(_), ty::GenericArgKind::Lifetime(_)) => true,
226243
(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)
228245
}
229246
(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)
231248
}
232249
_ => panic!("kind mismatch: {obl:?} {imp:?}"),
233250
}
234251
})
235252
}
236253

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+
}
238258
match rhs.kind() {
239259
// Start by checking whether the `rhs` type may unify with
240260
// 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_
277297
match lhs.kind() {
278298
ty::Ref(_, lhs_ty, lhs_mutbl) => match rhs.kind() {
279299
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)
281301
}
282302
_ => false,
283303
},
284304

285305
ty::Adt(lhs_def, lhs_args) => match rhs.kind() {
286306
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)
288308
}
289309
_ => false,
290310
},
@@ -326,27 +346,28 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
326346
ty::Tuple(rhs) => {
327347
lhs.len() == rhs.len()
328348
&& 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))
330350
}
331351
_ => false,
332352
},
333353

334354
ty::Array(lhs_ty, lhs_len) => match rhs.kind() {
335355
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)
337358
}
338359
_ => false,
339360
},
340361

341362
ty::RawPtr(lhs_ty, lhs_mutbl) => match rhs.kind() {
342363
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)
344365
}
345366
_ => false,
346367
},
347368

348369
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))
350371
}
351372

352373
ty::Dynamic(lhs_preds, ..) => {
@@ -366,7 +387,7 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
366387
lhs_hdr == rhs_hdr
367388
&& lhs_sig_tys.len() == rhs_sig_tys.len()
368389
&& 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))
370391
}
371392
_ => false,
372393
},
@@ -375,49 +396,57 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
375396

376397
ty::FnDef(lhs_def_id, lhs_args) => match rhs.kind() {
377398
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)
379401
}
380402
_ => false,
381403
},
382404

383405
ty::Closure(lhs_def_id, lhs_args) => match rhs.kind() {
384406
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)
386409
}
387410
_ => false,
388411
},
389412

390413
ty::CoroutineClosure(lhs_def_id, lhs_args) => match rhs.kind() {
391414
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)
393417
}
394418
_ => false,
395419
},
396420

397421
ty::Coroutine(lhs_def_id, lhs_args) => match rhs.kind() {
398422
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)
400425
}
401426
_ => false,
402427
},
403428

404429
ty::CoroutineWitness(lhs_def_id, lhs_args) => match rhs.kind() {
405430
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)
407433
}
408434
_ => false,
409435
},
410436

411437
ty::Pat(lhs_ty, _) => {
412438
// 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))
414440
}
415441

416442
ty::Error(..) => true,
417443
}
418444
}
419445

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+
}
421450
match rhs.kind() {
422451
ty::ConstKind::Param(_) => {
423452
if INSTANTIATE_RHS_WITH_INFER {

0 commit comments

Comments
 (0)