Skip to content

Commit 46b09f7

Browse files
Auto merge of #143503 - compiler-errors:deep-reject-self-self, r=<try>
[perf] Detect `?0: Tr<?0>` conflict in deep reject
2 parents 6dec76f + 40e9ee7 commit 46b09f7

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
340340
// before uses of `U`. This avoids false ambiguity errors
341341
// in trait checking. See `setup_constraining_predicates`
342342
// for details.
343-
if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
343+
if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node
344+
&& !tcx.next_trait_solver_globally()
345+
{
344346
let self_ty = tcx.type_of(def_id).instantiate_identity();
345347
let trait_ref = tcx.impl_trait_ref(def_id).map(ty::EarlyBinder::instantiate_identity);
346348
cgp::setup_constraining_predicates(

compiler/rustc_type_ir/src/fast_reject.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,27 @@ pub struct DeepRejectCtxt<
198198
const INSTANTIATE_RHS_WITH_INFER: bool,
199199
> {
200200
_interner: PhantomData<I>,
201+
self_param: Option<I::Ty>,
201202
}
202203

203204
impl<I: Interner> DeepRejectCtxt<I, false, false> {
204205
/// Treat parameters in both the lhs and the rhs as rigid.
205206
pub fn relate_rigid_rigid(_interner: I) -> DeepRejectCtxt<I, false, false> {
206-
DeepRejectCtxt { _interner: PhantomData }
207+
DeepRejectCtxt { _interner: PhantomData, self_param: None }
207208
}
208209
}
209210

210211
impl<I: Interner> DeepRejectCtxt<I, true, true> {
211212
/// Treat parameters in both the lhs and the rhs as infer vars.
212213
pub fn relate_infer_infer(_interner: I) -> DeepRejectCtxt<I, true, true> {
213-
DeepRejectCtxt { _interner: PhantomData }
214+
DeepRejectCtxt { _interner: PhantomData, self_param: None }
214215
}
215216
}
216217

217218
impl<I: Interner> DeepRejectCtxt<I, false, true> {
218219
/// Treat parameters in the lhs as rigid, and in rhs as infer vars.
219220
pub fn relate_rigid_infer(_interner: I) -> DeepRejectCtxt<I, false, true> {
220-
DeepRejectCtxt { _interner: PhantomData }
221+
DeepRejectCtxt { _interner: PhantomData, self_param: None }
221222
}
222223
}
223224

@@ -229,23 +230,28 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
229230
const STARTING_DEPTH: usize = 8;
230231

231232
pub fn args_may_unify(
232-
self,
233+
mut self,
233234
obligation_args: I::GenericArgs,
234235
impl_args: I::GenericArgs,
235236
) -> bool {
236237
self.args_may_unify_inner(obligation_args, impl_args, Self::STARTING_DEPTH)
237238
}
238239

239-
pub fn types_may_unify(self, lhs: I::Ty, rhs: I::Ty) -> bool {
240+
pub fn types_may_unify(mut self, lhs: I::Ty, rhs: I::Ty) -> bool {
240241
self.types_may_unify_inner(lhs, rhs, Self::STARTING_DEPTH)
241242
}
242243

243-
pub fn types_may_unify_with_depth(self, lhs: I::Ty, rhs: I::Ty, depth_limit: usize) -> bool {
244+
pub fn types_may_unify_with_depth(
245+
mut self,
246+
lhs: I::Ty,
247+
rhs: I::Ty,
248+
depth_limit: usize,
249+
) -> bool {
244250
self.types_may_unify_inner(lhs, rhs, depth_limit)
245251
}
246252

247253
fn args_may_unify_inner(
248-
self,
254+
&mut self,
249255
obligation_args: I::GenericArgs,
250256
impl_args: I::GenericArgs,
251257
depth: usize,
@@ -268,16 +274,27 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
268274
})
269275
}
270276

271-
fn types_may_unify_inner(self, lhs: I::Ty, rhs: I::Ty, depth: usize) -> bool {
277+
fn types_may_unify_inner(&mut self, lhs: I::Ty, rhs: I::Ty, depth: usize) -> bool {
272278
if lhs == rhs {
273279
return true;
274280
}
275281

276282
match rhs.kind() {
277283
// Start by checking whether the `rhs` type may unify with
278284
// pretty much everything. Just return `true` in that case.
279-
ty::Param(_) => {
285+
ty::Param(p) => {
280286
if INSTANTIATE_RHS_WITH_INFER {
287+
if !INSTANTIATE_LHS_WITH_INFER && p.index() == 0 {
288+
if let Some(self_param) = self.self_param {
289+
return DeepRejectCtxt::<I, false, false> {
290+
_interner: PhantomData,
291+
self_param: None,
292+
}
293+
.types_may_unify(self_param, lhs);
294+
} else {
295+
self.self_param = Some(lhs);
296+
}
297+
}
281298
return true;
282299
}
283300
}
@@ -479,7 +496,7 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
479496

480497
// Unlike `types_may_unify_inner`, this does not take a depth as
481498
// we never recurse from this function.
482-
fn consts_may_unify_inner(self, lhs: I::Const, rhs: I::Const) -> bool {
499+
fn consts_may_unify_inner(&mut self, lhs: I::Const, rhs: I::Const) -> bool {
483500
match rhs.kind() {
484501
ty::ConstKind::Param(_) => {
485502
if INSTANTIATE_RHS_WITH_INFER {
@@ -527,7 +544,7 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
527544
}
528545
}
529546

530-
fn var_and_ty_may_unify(self, var: ty::InferTy, ty: I::Ty) -> bool {
547+
fn var_and_ty_may_unify(&mut self, var: ty::InferTy, ty: I::Ty) -> bool {
531548
if !ty.is_known_rigid() {
532549
return true;
533550
}

0 commit comments

Comments
 (0)