Skip to content

Commit 2538276

Browse files
committed
Method resolution constrains hidden types instead of rejecting method candidates
1 parent ed1f464 commit 2538276

13 files changed

+34
-164
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14481448
let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).instantiate(tcx, args));
14491449
let self_ty = self.normalize(span, self_ty);
14501450
match self.at(&self.misc(span), self.param_env).eq(
1451-
DefineOpaqueTypes::No,
1451+
DefineOpaqueTypes::Yes,
14521452
impl_ty,
14531453
self_ty,
14541454
) {

compiler/rustc_hir_typeck/src/method/confirm.rs

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

compiler/rustc_hir_typeck/src/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14881488
self.probe(|_| {
14891489
// First check that the self type can be related.
14901490
let sub_obligations = match self.at(&ObligationCause::dummy(), self.param_env).sup(
1491-
DefineOpaqueTypes::No,
1491+
DefineOpaqueTypes::Yes,
14921492
probe.xform_self_ty,
14931493
self_ty,
14941494
) {

tests/ui/impl-trait/issues/issue-70877.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ fn ham() -> Foo {
2727

2828
fn oof(_: Foo) -> impl std::fmt::Debug {
2929
let mut bar = ham();
30-
let func = bar.next().unwrap();
31-
return func(&"oof"); //~ ERROR opaque type's hidden type cannot be another opaque type
30+
let func = bar.next().unwrap(); //~ ERROR: type annotations needed
31+
return func(&"oof");
3232
}
3333

3434
fn main() {
+9-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
error: opaque type's hidden type cannot be another opaque type from the same scope
2-
--> $DIR/issue-70877.rs:31:12
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-70877.rs:30:9
33
|
4+
LL | let func = bar.next().unwrap();
5+
| ^^^^
46
LL | return func(&"oof");
5-
| ^^^^^^^^^^^^ one of the two opaque types used here has to be outside its defining scope
7+
| ------------ type must be known at this point
68
|
7-
note: opaque type whose hidden type is being assigned
8-
--> $DIR/issue-70877.rs:28:19
9+
help: consider giving `func` an explicit type
910
|
10-
LL | fn oof(_: Foo) -> impl std::fmt::Debug {
11-
| ^^^^^^^^^^^^^^^^^^^^
12-
note: opaque type being used as hidden type
13-
--> $DIR/issue-70877.rs:4:15
14-
|
15-
LL | type FooRet = impl std::fmt::Debug;
16-
| ^^^^^^^^^^^^^^^^^^^^
11+
LL | let func: /* Type */ = bar.next().unwrap();
12+
| ++++++++++++
1713

1814
error: aborting due to 1 previous error
1915

16+
For more information about this error, try `rustc --explain E0282`.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ check-pass
2+
13
trait Trait {}
24

35
impl Trait for u32 {}
@@ -9,11 +11,9 @@ impl Bar<u32> {
911
}
1012

1113
fn foo(x: bool) -> Bar<impl Sized> {
12-
//~^ ERROR: cycle detected
1314
if x {
1415
let x = foo(false);
1516
x.foo();
16-
//~^ ERROR: no method named `foo` found
1717
}
1818
todo!()
1919
}

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

-36
This file was deleted.

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ impl Bar<i32> {
1313
}
1414

1515
fn foo(x: bool) -> Bar<impl Sized> {
16-
//~^ ERROR: cycle
1716
if x {
1817
let x = foo(false);
1918
x.foo();
20-
//~^ ERROR: no method named `foo`
19+
//~^ ERROR: multiple applicable items in scope
2120
}
2221
todo!()
2322
}
+13-30
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
1-
error[E0599]: no method named `foo` found for struct `Bar<impl Sized>` in the current scope
2-
--> $DIR/method-resolution3.rs:19:11
1+
error[E0034]: multiple applicable items in scope
2+
--> $DIR/method-resolution3.rs:18:11
33
|
4-
LL | struct Bar<T>(T);
5-
| ------------- method `foo` not found for this struct
6-
...
74
LL | x.foo();
8-
| ^^^ method not found in `Bar<impl Sized>`
5+
| ^^^ multiple `foo` 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:15: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:12:5
199
|
20-
note: ...which requires type-checking `foo`...
21-
--> $DIR/method-resolution3.rs:19:9
22-
|
23-
LL | x.foo();
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:15:24
10+
LL | fn foo(self) {}
11+
| ^^^^^^^^^^^^
12+
note: candidate #2 is defined in an impl for the type `Bar<u32>`
13+
--> $DIR/method-resolution3.rs:8: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 foo(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/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
}

tests/ui/methods/opaque_param_in_ufc.stderr

-36
This file was deleted.

tests/ui/type-alias-impl-trait/method_resolution2.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#![feature(type_alias_impl_trait)]
2+
//@ check-pass
23

34
type Foo = impl Sized;
4-
//~^ ERROR: cycle
55

66
struct Bar<T>(T);
77

88
impl Bar<Foo> {
99
fn bar(self) {
1010
self.foo()
11-
//~^ ERROR: no method named `foo`
1211
}
1312
}
1413

tests/ui/type-alias-impl-trait/method_resolution2.stderr

-36
This file was deleted.

0 commit comments

Comments
 (0)