Skip to content

Commit 9cf60ee

Browse files
committed
Method resolution constrains hidden types instead of rejecting method candidates
1 parent c75f728 commit 9cf60ee

17 files changed

+58
-213
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14181418
let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).instantiate(tcx, args));
14191419
let self_ty = self.normalize(span, self_ty);
14201420
match self.at(&self.misc(span), self.param_env).eq(
1421-
DefineOpaqueTypes::No,
1421+
DefineOpaqueTypes::Yes,
14221422
impl_ty,
14231423
self_ty,
14241424
) {

compiler/rustc_hir_typeck/src/method/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
497497
args,
498498
})),
499499
);
500-
match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::No, method_self_ty, self_ty) {
500+
match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::Yes, method_self_ty, self_ty) {
501501
Ok(InferOk { obligations, value: () }) => {
502502
self.register_predicates(obligations);
503503
}

compiler/rustc_hir_typeck/src/method/probe.rs

+29-36
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
634634
}
635635
}
636636

637+
#[instrument(level = "debug", skip(self))]
637638
fn assemble_probe(&mut self, self_ty: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>) {
638-
debug!("assemble_probe: self_ty={:?}", self_ty);
639639
let raw_self_ty = self_ty.value.value;
640640
match *raw_self_ty.kind() {
641641
ty::Dynamic(data, ..) if let Some(p) = data.principal() => {
@@ -713,13 +713,12 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
713713
}
714714
}
715715

716+
#[instrument(level = "debug", skip(self))]
716717
fn assemble_inherent_impl_probe(&mut self, impl_def_id: DefId) {
717718
if !self.impl_dups.insert(impl_def_id) {
718719
return; // already visited
719720
}
720721

721-
debug!("assemble_inherent_impl_probe {:?}", impl_def_id);
722-
723722
for item in self.impl_or_trait_item(impl_def_id) {
724723
if !self.has_applicable_self(&item) {
725724
// No receiver declared. Not a candidate.
@@ -737,9 +736,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
737736
}
738737
}
739738

739+
#[instrument(level = "debug", skip(self))]
740740
fn assemble_inherent_candidates_from_object(&mut self, self_ty: Ty<'tcx>) {
741-
debug!("assemble_inherent_candidates_from_object(self_ty={:?})", self_ty);
742-
743741
let principal = match self_ty.kind() {
744742
ty::Dynamic(ref data, ..) => Some(data),
745743
_ => None,
@@ -768,9 +766,9 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
768766
});
769767
}
770768

769+
#[instrument(level = "debug", skip(self))]
771770
fn assemble_inherent_candidates_from_param(&mut self, param_ty: ty::ParamTy) {
772771
// FIXME: do we want to commit to this behavior for param bounds?
773-
debug!("assemble_inherent_candidates_from_param(param_ty={:?})", param_ty);
774772

775773
let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
776774
let bound_predicate = predicate.kind();
@@ -826,6 +824,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
826824
}
827825
}
828826

827+
#[instrument(level = "debug", skip(self))]
829828
fn assemble_extension_candidates_for_traits_in_scope(&mut self) {
830829
let mut duplicates = FxHashSet::default();
831830
let opt_applicable_traits = self.tcx.in_scope_traits(self.scope_expr_id);
@@ -842,6 +841,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
842841
}
843842
}
844843

844+
#[instrument(level = "debug", skip(self))]
845845
fn assemble_extension_candidates_for_all_traits(&mut self) {
846846
let mut duplicates = FxHashSet::default();
847847
for trait_info in suggest::all_traits(self.tcx) {
@@ -863,12 +863,12 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
863863
}
864864
}
865865

866+
#[instrument(level = "debug", skip(self))]
866867
fn assemble_extension_candidates_for_trait(
867868
&mut self,
868869
import_ids: &SmallVec<[LocalDefId; 1]>,
869870
trait_def_id: DefId,
870871
) {
871-
debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id);
872872
let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
873873
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, trait_args);
874874

@@ -958,6 +958,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
958958
///////////////////////////////////////////////////////////////////////////
959959
// THE ACTUAL SEARCH
960960

961+
#[instrument(level = "debug", skip(self))]
961962
fn pick(mut self) -> PickResult<'tcx> {
962963
assert!(self.method_name.is_some());
963964

@@ -1386,6 +1387,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13861387
}
13871388
}
13881389

1390+
#[instrument(level = "trace", skip(self, possibly_unsatisfied_predicates), ret)]
13891391
fn consider_probe(
13901392
&self,
13911393
self_ty: Ty<'tcx>,
@@ -1415,15 +1417,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14151417
(xform_self_ty, xform_ret_ty) =
14161418
self.xform_self_ty(probe.item, impl_ty, impl_args);
14171419
xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1418-
// FIXME: Make this `ocx.sup` once we define opaques more eagerly.
1419-
match self.at(cause, self.param_env).sup(
1420-
DefineOpaqueTypes::No,
1421-
xform_self_ty,
1422-
self_ty,
1423-
) {
1424-
Ok(infer_ok) => {
1425-
ocx.register_infer_ok_obligations(infer_ok);
1426-
}
1420+
match ocx.sup(cause, self.param_env, xform_self_ty, self_ty) {
1421+
Ok(()) => {}
14271422
Err(err) => {
14281423
debug!("--> cannot relate self-types {:?}", err);
14291424
return ProbeResult::NoMatch;
@@ -1484,19 +1479,23 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14841479
(xform_self_ty, xform_ret_ty) =
14851480
self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
14861481
xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1487-
// FIXME: Make this `ocx.sup` once we define opaques more eagerly.
1488-
match self.at(cause, self.param_env).sup(
1489-
DefineOpaqueTypes::No,
1490-
xform_self_ty,
1491-
self_ty,
1492-
) {
1493-
Ok(infer_ok) => {
1494-
ocx.register_infer_ok_obligations(infer_ok);
1495-
}
1496-
Err(err) => {
1497-
debug!("--> cannot relate self-types {:?}", err);
1482+
match self_ty.kind() {
1483+
// HACK: opaque types will match anything for which their bounds hold.
1484+
// Thus we need to prevent them from trying to match the `&_` autoref
1485+
// candidates that get created for `&self` trait methods.
1486+
ty::Alias(ty::Opaque, alias_ty)
1487+
if self.infcx.can_define_opaque_ty(alias_ty.def_id)
1488+
&& !xform_self_ty.is_ty_var() =>
1489+
{
14981490
return ProbeResult::NoMatch;
14991491
}
1492+
_ => match ocx.sup(cause, self.param_env, xform_self_ty, self_ty) {
1493+
Ok(()) => {}
1494+
Err(err) => {
1495+
debug!("--> cannot relate self-types {:?}", err);
1496+
return ProbeResult::NoMatch;
1497+
}
1498+
},
15001499
}
15011500
let obligation = traits::Obligation::new(
15021501
self.tcx,
@@ -1536,15 +1535,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
15361535
(xform_self_ty, xform_ret_ty) =
15371536
self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
15381537
xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1539-
// FIXME: Make this `ocx.sup` once we define opaques more eagerly.
1540-
match self.at(cause, self.param_env).sup(
1541-
DefineOpaqueTypes::No,
1542-
xform_self_ty,
1543-
self_ty,
1544-
) {
1545-
Ok(infer_ok) => {
1546-
ocx.register_infer_ok_obligations(infer_ok);
1547-
}
1538+
match ocx.sup(cause, self.param_env, xform_self_ty, self_ty) {
1539+
Ok(()) => {}
15481540
Err(err) => {
15491541
debug!("--> cannot relate self-types {:?}", err);
15501542
return ProbeResult::NoMatch;
@@ -1665,6 +1657,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16651657
/// Similarly to `probe_for_return_type`, this method attempts to find the best matching
16661658
/// candidate method where the method name may have been misspelled. Similarly to other
16671659
/// edit distance based suggestions, we provide at most one such suggestion.
1660+
#[instrument(level = "debug", skip(self))]
16681661
pub(crate) fn probe_for_similar_candidate(
16691662
&mut self,
16701663
) -> Result<Option<ty::AssocItem>, MethodError<'tcx>> {

compiler/rustc_middle/src/traits/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub struct CandidateStep<'tcx> {
156156

157157
#[derive(Copy, Clone, Debug, HashStable)]
158158
pub struct MethodAutoderefStepsResult<'tcx> {
159-
/// The valid autoderef steps that could be find.
159+
/// The valid autoderef steps that could be found.
160160
pub steps: &'tcx [CandidateStep<'tcx>],
161161
/// If Some(T), a type autoderef reported an error on.
162162
pub opt_bad_ty: Option<&'tcx MethodAutoderefBadTy<'tcx>>,

tests/ui/impl-trait/method-resolution.current.stderr

-36
This file was deleted.

tests/ui/impl-trait/method-resolution.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
//! Check that we do not constrain hidden types during method resolution.
2-
//! Otherwise we'd pick up that calling `bar` can be satisfied iff `u32`
3-
//! is the hidden type of the RPIT.
1+
//! Since there is only one possible `bar` method, we invoke it and subsequently
2+
//! constrain `foo`'s RPIT to `u32`.
43
54
//@ revisions: current next
65
//@[next] compile-flags: -Znext-solver
7-
//@[next] check-pass
6+
//@ check-pass
87

98
trait Trait {}
109

@@ -17,11 +16,9 @@ impl Bar<u32> {
1716
}
1817

1918
fn foo(x: bool) -> Bar<impl Sized> {
20-
//[current]~^ ERROR: cycle detected
2119
if x {
2220
let x = foo(false);
2321
x.bar();
24-
//[current]~^ ERROR: no method named `bar` found
2522
}
2623
todo!()
2724
}

tests/ui/impl-trait/method-resolution2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Check that the method call does not constrain the RPIT to `i32`, even though
2-
//! `i32` is the only trait that satisfies the RPIT's trait bounds.
2+
//! `i32` is the only type that satisfies the RPIT's trait bounds.
33
44
//@ revisions: current next
55
//@[next] compile-flags: -Znext-solver
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
1-
error[E0599]: no method named `bar` found for struct `Bar<impl Sized>` in the current scope
2-
--> $DIR/method-resolution3.rs:22:11
1+
error[E0034]: multiple applicable items in scope
2+
--> $DIR/method-resolution3.rs:21:11
33
|
4-
LL | struct Bar<T>(T);
5-
| ------------- method `bar` not found for this struct
6-
...
74
LL | x.bar();
8-
| ^^^ method not found in `Bar<impl Sized>`
5+
| ^^^ multiple `bar` found
96
|
10-
= note: the method was found for
11-
- `Bar<i32>`
12-
- `Bar<u32>`
13-
14-
error[E0391]: cycle detected when computing type of opaque `foo::{opaque#0}`
15-
--> $DIR/method-resolution3.rs:18:24
16-
|
17-
LL | fn foo(x: bool) -> Bar<impl Sized> {
18-
| ^^^^^^^^^^
7+
note: candidate #1 is defined in an impl for the type `Bar<i32>`
8+
--> $DIR/method-resolution3.rs:15:5
199
|
20-
note: ...which requires type-checking `foo`...
21-
--> $DIR/method-resolution3.rs:22:9
22-
|
23-
LL | x.bar();
24-
| ^
25-
= note: ...which requires evaluating trait selection obligation `Bar<foo::{opaque#0}>: core::marker::Unpin`...
26-
= note: ...which again requires computing type of opaque `foo::{opaque#0}`, completing the cycle
27-
note: cycle used when computing type of `foo::{opaque#0}`
28-
--> $DIR/method-resolution3.rs:18:24
10+
LL | fn bar(self) {}
11+
| ^^^^^^^^^^^^
12+
note: candidate #2 is defined in an impl for the type `Bar<u32>`
13+
--> $DIR/method-resolution3.rs:11:5
2914
|
30-
LL | fn foo(x: bool) -> Bar<impl Sized> {
31-
| ^^^^^^^^^^
32-
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
15+
LL | fn bar(self) {}
16+
| ^^^^^^^^^^^^
3317

34-
error: aborting due to 2 previous errors
18+
error: aborting due to 1 previous error
3519

36-
Some errors have detailed explanations: E0391, E0599.
37-
For more information about an error, try `rustc --explain E0391`.
20+
For more information about this error, try `rustc --explain E0034`.

tests/ui/impl-trait/method-resolution3.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0034]: multiple applicable items in scope
2-
--> $DIR/method-resolution3.rs:22:11
2+
--> $DIR/method-resolution3.rs:21:11
33
|
44
LL | x.bar();
55
| ^^^ multiple `bar` found

tests/ui/impl-trait/method-resolution3.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ impl Bar<i32> {
1616
}
1717

1818
fn foo(x: bool) -> Bar<impl Sized> {
19-
//[current]~^ ERROR: cycle
2019
if x {
2120
let x = foo(false);
2221
x.bar();
23-
//[current]~^ ERROR: no method named `bar`
24-
//[next]~^^ ERROR: multiple applicable items in scope
22+
//~^ ERROR: multiple applicable items in scope
2523
}
2624
todo!()
2725
}

tests/ui/impl-trait/method-resolution4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
//! variable, but get a type mismatch when comparing `&mut _` with
55
//! `std::iter::Empty`.
66
7-
//@[current] check-pass
87
//@ revisions: current next
98
//@[next] compile-flags: -Znext-solver
9+
//@[current] check-pass
1010

1111
fn foo(b: bool) -> impl Iterator<Item = ()> {
1212
if b {

tests/ui/methods/opaque_param_in_ufc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#![feature(type_alias_impl_trait)]
2+
3+
//@ check-pass
4+
25
struct Foo<T>(T);
36

47
impl Foo<u32> {
@@ -15,14 +18,11 @@ fn bar() -> Bar {
1518
impl Foo<Bar> {
1619
fn foo() -> Bar {
1720
Self::method();
18-
//~^ ERROR: no function or associated item named `method` found for struct `Foo<Bar>`
1921
Foo::<Bar>::method();
20-
//~^ ERROR: no function or associated item named `method` found for struct `Foo<Bar>`
2122
let x = Foo(bar());
2223
Foo::method2(x);
2324
let x = Self(bar());
2425
Self::method2(x);
25-
//~^ ERROR: no function or associated item named `method2` found for struct `Foo<Bar>`
2626
todo!()
2727
}
2828
}

0 commit comments

Comments
 (0)