Skip to content

Commit 47b72a3

Browse files
committed
Auto merge of #145396 - compiler-errors:revert-method-pref, r=lcnr
[BETA] Revert "Use DeepRejectCtxt in assemble_inherent_candidates_from_param" Fixes #145185. Backporting this to stable and beta in favor of #145262 (comment). r? lcnr
2 parents 630a5f1 + b0ec0ec commit 47b72a3

File tree

7 files changed

+118
-58
lines changed

7 files changed

+118
-58
lines changed

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// tidy-alphabetical-start
22
#![allow(rustc::diagnostic_outside_of_impl)]
33
#![allow(rustc::untranslatable_diagnostic)]
4-
#![feature(assert_matches)]
54
#![feature(box_patterns)]
65
#![feature(if_let_guard)]
76
#![feature(iter_intersperse)]

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::assert_matches::debug_assert_matches;
21
use std::cell::{Cell, RefCell};
32
use std::cmp::max;
43
use std::ops::Deref;
@@ -16,7 +15,7 @@ use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk,
1615
use rustc_infer::traits::ObligationCauseCode;
1716
use rustc_middle::middle::stability;
1817
use rustc_middle::ty::elaborate::supertrait_def_ids;
19-
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
18+
use rustc_middle::ty::fast_reject::{TreatParams, simplify_type};
2019
use rustc_middle::ty::{
2120
self, AssocItem, AssocItemContainer, GenericArgs, GenericArgsRef, GenericParamDefKind,
2221
ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt, Upcast,
@@ -803,8 +802,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
803802
);
804803
}
805804
}
806-
ty::Param(_) => {
807-
self.assemble_inherent_candidates_from_param(raw_self_ty);
805+
ty::Param(p) => {
806+
self.assemble_inherent_candidates_from_param(p);
808807
}
809808
ty::Bool
810809
| ty::Char
@@ -905,16 +904,18 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
905904
}
906905

907906
#[instrument(level = "debug", skip(self))]
908-
fn assemble_inherent_candidates_from_param(&mut self, param_ty: Ty<'tcx>) {
909-
debug_assert_matches!(param_ty.kind(), ty::Param(_));
910-
911-
let tcx = self.tcx;
907+
fn assemble_inherent_candidates_from_param(&mut self, param_ty: ty::ParamTy) {
912908
let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
913909
let bound_predicate = predicate.kind();
914910
match bound_predicate.skip_binder() {
915-
ty::ClauseKind::Trait(trait_predicate) => DeepRejectCtxt::relate_rigid_rigid(tcx)
916-
.types_may_unify(param_ty, trait_predicate.trait_ref.self_ty())
917-
.then(|| bound_predicate.rebind(trait_predicate.trait_ref)),
911+
ty::ClauseKind::Trait(trait_predicate) => {
912+
match *trait_predicate.trait_ref.self_ty().kind() {
913+
ty::Param(p) if p == param_ty => {
914+
Some(bound_predicate.rebind(trait_predicate.trait_ref))
915+
}
916+
_ => None,
917+
}
918+
}
918919
ty::ClauseKind::RegionOutlives(_)
919920
| ty::ClauseKind::TypeOutlives(_)
920921
| ty::ClauseKind::Projection(_)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0034]: multiple applicable items in scope
2+
--> $DIR/rigid-alias-bound-is-not-inherent.rs:42:7
3+
|
4+
LL | x.method();
5+
| ^^^^^^ multiple `method` found
6+
|
7+
note: candidate #1 is defined in the trait `Trait1`
8+
--> $DIR/rigid-alias-bound-is-not-inherent.rs:21:5
9+
|
10+
LL | fn method(&self) {
11+
| ^^^^^^^^^^^^^^^^
12+
note: candidate #2 is defined in an impl of the trait `Trait2` for the type `T`
13+
--> $DIR/rigid-alias-bound-is-not-inherent.rs:27:5
14+
|
15+
LL | fn method(&self) {
16+
| ^^^^^^^^^^^^^^^^
17+
help: disambiguate the method for candidate #1
18+
|
19+
LL - x.method();
20+
LL + Trait1::method(&x);
21+
|
22+
help: disambiguate the method for candidate #2
23+
|
24+
LL - x.method();
25+
LL + Trait2::method(&x);
26+
|
27+
28+
error: aborting due to 1 previous error
29+
30+
For more information about this error, try `rustc --explain E0034`.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0034]: multiple applicable items in scope
2+
--> $DIR/rigid-alias-bound-is-not-inherent.rs:42:7
3+
|
4+
LL | x.method();
5+
| ^^^^^^ multiple `method` found
6+
|
7+
note: candidate #1 is defined in the trait `Trait1`
8+
--> $DIR/rigid-alias-bound-is-not-inherent.rs:21:5
9+
|
10+
LL | fn method(&self) {
11+
| ^^^^^^^^^^^^^^^^
12+
note: candidate #2 is defined in the trait `Trait2`
13+
--> $DIR/rigid-alias-bound-is-not-inherent.rs:27:5
14+
|
15+
LL | fn method(&self) {
16+
| ^^^^^^^^^^^^^^^^
17+
help: disambiguate the method for candidate #1
18+
|
19+
LL - x.method();
20+
LL + Trait1::method(&x);
21+
|
22+
help: disambiguate the method for candidate #2
23+
|
24+
LL - x.method();
25+
LL + Trait2::method(&x);
26+
|
27+
28+
error: aborting due to 1 previous error
29+
30+
For more information about this error, try `rustc --explain E0034`.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
4+
5+
// See the code below.
6+
//
7+
// We were using `DeepRejectCtxt` to ensure that `assemble_inherent_candidates_from_param`
8+
// did not rely on the param-env being eagerly normalized. Since aliases unify with all
9+
// types, this meant that a rigid param-env candidate like `<T as Deref>::Target: Trait1`
10+
// would be registered as a "WhereClauseCandidate", which is treated as inherent. Since
11+
// we evaluate these candidates for all self types in the deref chain, this candidate
12+
// would be satisfied for `<T as Deref>::Target`, meaning that it would be preferred over
13+
// an "extension" candidate like `<T as Deref>::Target: Trait2` even though it holds.
14+
// This is problematic, since it causes ambiguities to be broken somewhat arbitrarily.
15+
// And as a side-effect, it also caused our computation of "used" traits to be miscalculated
16+
// since inherent candidates don't count as an import usage.
17+
18+
use std::ops::Deref;
19+
20+
trait Trait1 {
21+
fn method(&self) {
22+
println!("1");
23+
}
24+
}
25+
26+
trait Trait2 {
27+
fn method(&self) {
28+
println!("2");
29+
}
30+
}
31+
impl<T: Other + ?Sized> Trait2 for T {}
32+
33+
trait Other {}
34+
35+
fn foo<T>(x: T)
36+
where
37+
T: Deref,
38+
<T as Deref>::Target: Trait1 + Other,
39+
{
40+
// Make sure that we don't prefer methods from where clauses for rigid aliases,
41+
// just for params. We could revisit this behavior, but it would be a lang change.
42+
x.method();
43+
//~^ ERROR multiple applicable items in scope
44+
}
45+
46+
fn main() {}

tests/ui/traits/next-solver/method/param-method-from-unnormalized-param-env-2.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/ui/traits/next-solver/method/param-method-from-unnormalized-param-env.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)