Skip to content

Commit

Permalink
Method resolution constrains hidden types instead of rejecting method…
Browse files Browse the repository at this point in the history
… candidates
  • Loading branch information
oli-obk committed Apr 15, 2024
1 parent 31271ed commit a6c6b29
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 132 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).instantiate(tcx, args));
let self_ty = self.normalize(span, self_ty);
match self.at(&self.misc(span), self.param_env).eq(
DefineOpaqueTypes::No,
DefineOpaqueTypes::Yes,
impl_ty,
self_ty,
) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
args,
})),
);
match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::No, method_self_ty, self_ty) {
match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::Yes, method_self_ty, self_ty) {
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
self.probe(|_| {
// First check that the self type can be related.
let sub_obligations = match self.at(&ObligationCause::dummy(), self.param_env).sup(
DefineOpaqueTypes::No,
DefineOpaqueTypes::Yes,
probe.xform_self_ty,
self_ty,
) {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/issues/issue-70877.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fn ham() -> Foo {

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

fn main() {
Expand Down
21 changes: 9 additions & 12 deletions tests/ui/impl-trait/issues/issue-70877.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
error: opaque type's hidden type cannot be another opaque type from the same scope
--> $DIR/issue-70877.rs:31:12
error[E0282]: type annotations needed
--> $DIR/issue-70877.rs:30:9
|
LL | let func = bar.next().unwrap();
| ^^^^
LL | return func(&"oof");
| ^^^^^^^^^^^^ one of the two opaque types used here has to be outside its defining scope
| ------------ type must be known at this point
|
note: opaque type whose hidden type is being assigned
--> $DIR/issue-70877.rs:28:19
help: consider giving `func` an explicit type
|
LL | fn oof(_: Foo) -> impl std::fmt::Debug {
| ^^^^^^^^^^^^^^^^^^^^
note: opaque type being used as hidden type
--> $DIR/issue-70877.rs:4:15
|
LL | type FooRet = impl std::fmt::Debug;
| ^^^^^^^^^^^^^^^^^^^^
LL | let func: /* Type */ = bar.next().unwrap();
| ++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0282`.
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/method-resolution.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ check-pass

trait Trait {}

impl Trait for u32 {}
Expand All @@ -9,11 +11,9 @@ impl Bar<u32> {
}

fn foo(x: bool) -> Bar<impl Sized> {
//~^ ERROR: cycle detected
if x {
let x = foo(false);
x.foo();
//~^ ERROR: no method named `foo` found
}
todo!()
}
Expand Down
36 changes: 0 additions & 36 deletions tests/ui/impl-trait/method-resolution.stderr

This file was deleted.

6 changes: 3 additions & 3 deletions tests/ui/methods/opaque_param_in_ufc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#![feature(type_alias_impl_trait)]

//@ check-pass

struct Foo<T>(T);

impl Foo<u32> {
Expand All @@ -15,14 +18,11 @@ fn bar() -> Bar {
impl Foo<Bar> {
fn foo() -> Bar {
Self::method();
//~^ ERROR: no function or associated item named `method` found for struct `Foo<Bar>`
Foo::<Bar>::method();
//~^ ERROR: no function or associated item named `method` found for struct `Foo<Bar>`
let x = Foo(bar());
Foo::method2(x);
let x = Self(bar());
Self::method2(x);
//~^ ERROR: no function or associated item named `method2` found for struct `Foo<Bar>`
todo!()
}
}
Expand Down
36 changes: 0 additions & 36 deletions tests/ui/methods/opaque_param_in_ufc.stderr

This file was deleted.

3 changes: 1 addition & 2 deletions tests/ui/type-alias-impl-trait/method_resolution2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#![feature(type_alias_impl_trait)]
//@ check-pass

type Foo = impl Sized;
//~^ ERROR: cycle

struct Bar<T>(T);

impl Bar<Foo> {
fn bar(self) {
self.foo()
//~^ ERROR: no method named `foo`
}
}

Expand Down
36 changes: 0 additions & 36 deletions tests/ui/type-alias-impl-trait/method_resolution2.stderr

This file was deleted.

0 comments on commit a6c6b29

Please sign in to comment.