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 9df4a9d commit af980d5
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 132 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_data_structures/src/obligation_forest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ impl<O: ForestObligation> ObligationForest<O> {
Entry::Vacant(v) => {
let obligation_tree_id = match parent {
Some(parent_index) => self.nodes[parent_index].obligation_tree_id,
None => self.obligation_tree_id_generator.next().unwrap(),
// FIXME(type_alias_impl_trait): with `#[defines]` attributes required to define hidden
// types we can convert this back to a `next` method call, as this function shouldn't be
// defining a hidden type anyway.
None => Iterator::next(&mut self.obligation_tree_id_generator).unwrap(),
};

let already_failed = parent.is_some()
Expand Down
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: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-70877.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ fn ham() -> Foo {

fn oof(_: Foo) -> impl std::fmt::Debug {
let mut bar = ham();
let func = bar.next().unwrap();
// Need to UFC invoke `Iterator::next`,
// as otherwise the hidden type gets constrained to `&mut _`
let func = Iterator::next(&mut bar).unwrap();
return func(&"oof"); //~ ERROR opaque type's hidden type cannot be another opaque type
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/issues/issue-70877.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: opaque type's hidden type cannot be another opaque type from the same scope
--> $DIR/issue-70877.rs:31:12
--> $DIR/issue-70877.rs:33:12
|
LL | return func(&"oof");
| ^^^^^^^^^^^^ one of the two opaque types used here has to be outside its defining scope
Expand Down
19 changes: 8 additions & 11 deletions tests/ui/impl-trait/method-resolution.current.stderr
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
error[E0599]: no method named `bar` found for struct `Bar<impl Sized>` in the current scope
--> $DIR/method-resolution.rs:23:11
error[E0599]: no method named `foo` found for struct `Bar` in the current scope
--> $DIR/method-resolution.rs:22:11
|
LL | struct Bar<T>(T);
| ------------- method `bar` not found for this struct
| ------------- method `foo` not found for this struct
...
LL | x.bar();
LL | x.foo();
| ^^^ method not found in `Bar<impl Sized>`
|
= note: the method was found for
- `Bar<u32>`

error[E0391]: cycle detected when computing type of opaque `foo::{opaque#0}`
--> $DIR/method-resolution.rs:19:24
--> $DIR/method-resolution.rs:18:24
|
LL | fn foo(x: bool) -> Bar<impl Sized> {
| ^^^^^^^^^^
|
note: ...which requires type-checking `foo`...
--> $DIR/method-resolution.rs:23:9
--> $DIR/method-resolution.rs:22:9
|
LL | x.bar();
LL | x.foo();
| ^
= note: ...which requires evaluating trait selection obligation `Bar<foo::{opaque#0}>: core::marker::Unpin`...
= note: ...which again requires computing type of opaque `foo::{opaque#0}`, completing the cycle
note: cycle used when computing type of `foo::{opaque#0}`
--> $DIR/method-resolution.rs:19:24
--> $DIR/method-resolution.rs:18:24
|
LL | fn foo(x: bool) -> Bar<impl Sized> {
| ^^^^^^^^^^
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/impl-trait/method-resolution.next.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0599]: no method named `foo` found for struct `Bar` in the current scope
--> $DIR/method-resolution.rs:22:11
|
LL | struct Bar<T>(T);
| ------------- method `foo` not found for this struct
...
LL | x.foo();
| ^^^ method not found in `Bar<_>`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
7 changes: 3 additions & 4 deletions tests/ui/impl-trait/method-resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//@ revisions: current next
//@[next] compile-flags: -Znext-solver
//@[next] check-pass

trait Trait {}

Expand All @@ -17,11 +16,11 @@ impl Bar<u32> {
}

fn foo(x: bool) -> Bar<impl Sized> {
//[current]~^ ERROR: cycle detected
//[current]~^ ERROR: cycle
if x {
let x = foo(false);
x.bar();
//[current]~^ ERROR: no method named `bar` found
x.foo();
//~^ ERROR: no method named `foo` found
}
todo!()
}
Expand Down
12 changes: 4 additions & 8 deletions tests/ui/impl-trait/method-resolution3.current.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
error[E0599]: no method named `bar` found for struct `Bar<impl Sized>` in the current scope
error[E0599]: no method named `foo` found for struct `Bar` in the current scope
--> $DIR/method-resolution3.rs:22:11
|
LL | struct Bar<T>(T);
| ------------- method `bar` not found for this struct
| ------------- method `foo` not found for this struct
...
LL | x.bar();
LL | x.foo();
| ^^^ method not found in `Bar<impl Sized>`
|
= note: the method was found for
- `Bar<i32>`
- `Bar<u32>`

error[E0391]: cycle detected when computing type of opaque `foo::{opaque#0}`
--> $DIR/method-resolution3.rs:18:24
Expand All @@ -20,7 +16,7 @@ LL | fn foo(x: bool) -> Bar<impl Sized> {
note: ...which requires type-checking `foo`...
--> $DIR/method-resolution3.rs:22:9
|
LL | x.bar();
LL | x.foo();
| ^
= note: ...which requires evaluating trait selection obligation `Bar<foo::{opaque#0}>: core::marker::Unpin`...
= note: ...which again requires computing type of opaque `foo::{opaque#0}`, completing the cycle
Expand Down
22 changes: 7 additions & 15 deletions tests/ui/impl-trait/method-resolution3.next.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
error[E0034]: multiple applicable items in scope
error[E0599]: no method named `foo` found for struct `Bar` in the current scope
--> $DIR/method-resolution3.rs:22:11
|
LL | x.bar();
| ^^^ multiple `bar` found
|
note: candidate #1 is defined in an impl for the type `Bar<i32>`
--> $DIR/method-resolution3.rs:15:5
|
LL | fn bar(self) {}
| ^^^^^^^^^^^^
note: candidate #2 is defined in an impl for the type `Bar<u32>`
--> $DIR/method-resolution3.rs:11:5
|
LL | fn bar(self) {}
| ^^^^^^^^^^^^
LL | struct Bar<T>(T);
| ------------- method `foo` not found for this struct
...
LL | x.foo();
| ^^^ method not found in `Bar<_>`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0034`.
For more information about this error, try `rustc --explain E0599`.
5 changes: 2 additions & 3 deletions tests/ui/impl-trait/method-resolution3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ fn foo(x: bool) -> Bar<impl Sized> {
//[current]~^ ERROR: cycle
if x {
let x = foo(false);
x.bar();
//[current]~^ ERROR: no method named `bar`
//[next]~^^ ERROR: multiple applicable items in scope
x.foo();
//~^ ERROR: no method named `foo` found
}
todo!()
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/impl-trait/method-resolution4.current.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0308]: mismatched types
--> $DIR/method-resolution4.rs:15:5
|
LL | std::iter::empty()
| ^^^^^^^^^^^^^^^^^^ expected `&mut _`, found `Empty<_>`
|
= note: expected mutable reference `&mut _`
found struct `std::iter::Empty<_>`
help: consider mutably borrowing here
|
LL | &mut std::iter::empty()
| ++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/method-resolution4.next.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0282]: type annotations needed
--> $DIR/method-resolution4.rs:13:9
--> $DIR/method-resolution4.rs:12:9
|
LL | foo(false).next().unwrap();
| ^^^^^^^^^^ cannot infer type

error[E0308]: mismatched types
--> $DIR/method-resolution4.rs:16:5
--> $DIR/method-resolution4.rs:15:5
|
LL | fn foo(b: bool) -> impl Iterator<Item = ()> {
| ------------------------ the expected opaque type
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/impl-trait/method-resolution4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! variable, but get a type mismatch when comparing `&mut _` with
//! `std::iter::Empty`.
//@[current] check-pass
//@ revisions: current next
//@[next] compile-flags: -Znext-solver

Expand All @@ -14,7 +13,7 @@ fn foo(b: bool) -> impl Iterator<Item = ()> {
//[next]~^ type annotations needed
}
std::iter::empty()
//[next]~^ mismatched types
//~^ mismatched types
}

fn main() {}
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.

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

This file was deleted.

4 changes: 1 addition & 3 deletions tests/ui/type-alias-impl-trait/method_resolution2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
//@ revisions: current next
//@[next] compile-flags: -Znext-solver
//@[next] check-pass
//@ check-pass

#![feature(type_alias_impl_trait)]

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

struct Bar<T>(T);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0284]: type annotations needed
--> $DIR/method_resolution_trait_method_from_opaque.rs:25:18
|
LL | self.bar.next().unwrap();
| ^^^^
|
= note: cannot satisfy `<_ as Iterator>::Item == _`
help: try using a fully qualified path to specify the expected types
|
LL | <_ as Iterator>::next(self.bar).unwrap();
| ++++++++++++++++++++++ ~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0284`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0282]: type annotations needed
--> $DIR/method_resolution_trait_method_from_opaque.rs:26:9
--> $DIR/method_resolution_trait_method_from_opaque.rs:25:9
|
LL | self.bar.next().unwrap();
| ^^^^^^^^ cannot infer type
Expand Down
Loading

0 comments on commit af980d5

Please sign in to comment.