Skip to content

Commit a1dbb44

Browse files
committed
Auto merge of #145262 - compiler-errors:prefer-only-param, r=lcnr
Make sure to treat only param where clauses as inherent See the description in the test file. This PR fixes a bug introduced by #141333, where we considered non-`Param` where clauses to be "inherent" for the purpose of method probing, which leads to both changes in method ambiguity (see test) and also import usage linting (and thus fixes #145185). r? `@lcnr`
2 parents 809200e + c2b9a8a commit a1dbb44

8 files changed

+223
-0
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
909909
debug_assert_matches!(param_ty.kind(), ty::Param(_));
910910

911911
let tcx = self.tcx;
912+
913+
// We use `DeepRejectCtxt` here which may return false positive on where clauses
914+
// with alias self types. We need to later on reject these as inherent candidates
915+
// in `consider_probe`.
912916
let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
913917
let bound_predicate = predicate.kind();
914918
match bound_predicate.skip_binder() {
@@ -1945,6 +1949,29 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
19451949
);
19461950
(xform_self_ty, xform_ret_ty) =
19471951
self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
1952+
1953+
if matches!(probe.kind, WhereClauseCandidate(_)) {
1954+
// `WhereClauseCandidate` requires that the self type is a param,
1955+
// because it has special behavior with candidate preference as an
1956+
// inherent pick.
1957+
match ocx.structurally_normalize_ty(
1958+
cause,
1959+
self.param_env,
1960+
trait_ref.self_ty(),
1961+
) {
1962+
Ok(ty) => {
1963+
if !matches!(ty.kind(), ty::Param(_)) {
1964+
debug!("--> not a param ty: {xform_self_ty:?}");
1965+
return ProbeResult::NoMatch;
1966+
}
1967+
}
1968+
Err(errors) => {
1969+
debug!("--> cannot relate self-types {:?}", errors);
1970+
return ProbeResult::NoMatch;
1971+
}
1972+
}
1973+
}
1974+
19481975
xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
19491976
match ocx.relate(cause, self.param_env, self.variance(), self_ty, xform_self_ty)
19501977
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Regression test for <github.com/rust-lang/rust/issues/145185>.
2+
3+
mod module {
4+
pub trait Trait {
5+
fn method(&self);
6+
}
7+
}
8+
9+
// Note that we do not import Trait
10+
use std::ops::Deref;
11+
12+
fn foo(x: impl Deref<Target: module::Trait>) {
13+
x.method();
14+
//~^ ERROR no method named `method` found for type parameter
15+
}
16+
17+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0599]: no method named `method` found for type parameter `impl Deref<Target : module::Trait>` in the current scope
2+
--> $DIR/rigid-alias-bound-is-not-inherent-2.rs:13:7
3+
|
4+
LL | fn foo(x: impl Deref<Target: module::Trait>) {
5+
| --------------------------------- method `method` not found for this type parameter
6+
LL | x.method();
7+
| ^^^^^^ method not found in `impl Deref<Target : module::Trait>`
8+
|
9+
= help: items from traits can only be used if the trait is in scope
10+
help: trait `Trait` which provides `method` is implemented but not in scope; perhaps you want to import it
11+
|
12+
LL + use module::Trait;
13+
|
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0599`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::ops::Deref;
2+
3+
trait Trait1 {
4+
fn call_me(&self) {}
5+
}
6+
7+
impl<T> Trait1 for T {}
8+
9+
trait Trait2 {
10+
fn call_me(&self) {}
11+
}
12+
13+
impl<T> Trait2 for T {}
14+
15+
pub fn foo<T, U>(x: T)
16+
where
17+
T: Deref<Target = U>,
18+
U: Trait1,
19+
{
20+
// This should be ambiguous. The fact that there's an inherent where-bound
21+
// candidate for `U` should not impact the candidates for `T`
22+
x.call_me();
23+
//~^ ERROR multiple applicable items in scope
24+
}
25+
26+
fn main() {}
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-3.rs:22:7
3+
|
4+
LL | x.call_me();
5+
| ^^^^^^^ multiple `call_me` found
6+
|
7+
note: candidate #1 is defined in an impl of the trait `Trait1` for the type `T`
8+
--> $DIR/rigid-alias-bound-is-not-inherent-3.rs:4:5
9+
|
10+
LL | fn call_me(&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-3.rs:10:5
14+
|
15+
LL | fn call_me(&self) {}
16+
| ^^^^^^^^^^^^^^^^^
17+
help: disambiguate the method for candidate #1
18+
|
19+
LL - x.call_me();
20+
LL + Trait1::call_me(&x);
21+
|
22+
help: disambiguate the method for candidate #2
23+
|
24+
LL - x.call_me();
25+
LL + Trait2::call_me(&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 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() {}

0 commit comments

Comments
 (0)