Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1c5bfb1

Browse files
committed
Don't bind hidden types when searching for matching impls
1 parent f42a679 commit 1c5bfb1

File tree

7 files changed

+74
-39
lines changed

7 files changed

+74
-39
lines changed

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
21632163
let InferOk { obligations, .. } = self
21642164
.infcx
21652165
.at(&cause, obligation.param_env)
2166+
.define_opaque_types(false)
21662167
.eq(placeholder_obligation_trait_ref, impl_trait_ref)
21672168
.map_err(|e| debug!("match_impl: failed eq_trait_refs due to `{}`", e))?;
21682169
nested_obligations.extend(obligations);

src/test/ui/impl-trait/nested_impl_trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn fine(x: impl Into<u32>) -> impl Into<u32> { x }
44

55
fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
66
//~^ ERROR nested `impl Trait` is not allowed
7-
//~| ERROR `impl Into<u32>` doesn't implement `Debug`
7+
//~| ERROR the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
88

99
fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
1010
//~^ ERROR nested `impl Trait` is not allowed
@@ -17,7 +17,7 @@ struct X;
1717
impl X {
1818
fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
1919
//~^ ERROR nested `impl Trait` is not allowed
20-
//~| ERROR `impl Into<u32>` doesn't implement `Debug`
20+
//~| ERROR the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
2121
}
2222

2323
fn allowed_in_assoc_type() -> impl Iterator<Item=impl Fn()> {

src/test/ui/impl-trait/nested_impl_trait.stderr

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,21 @@ error[E0562]: `impl Trait` only allowed in function and inherent method return t
4646
LL | fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {
4747
| ^^^^^^^^^^^^^^
4848

49-
error[E0277]: `impl Into<u32>` doesn't implement `Debug`
49+
error[E0277]: the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
5050
--> $DIR/nested_impl_trait.rs:5:70
5151
|
5252
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
53-
| ^ `impl Into<u32>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
53+
| ^ the trait `From<impl Into<u32>>` is not implemented for `impl Debug`
5454
|
55-
help: consider further restricting this bound
56-
|
57-
LL | fn bad_in_ret_position(x: impl Into<u32> + std::fmt::Debug) -> impl Into<impl Debug> { x }
58-
| +++++++++++++++++
55+
= note: required because of the requirements on the impl of `Into<impl Debug>` for `impl Into<u32>`
5956

60-
error[E0277]: `impl Into<u32>` doesn't implement `Debug`
57+
error[E0277]: the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
6158
--> $DIR/nested_impl_trait.rs:18:58
6259
|
6360
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
64-
| ^ `impl Into<u32>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
65-
|
66-
help: consider further restricting this bound
61+
| ^ the trait `From<impl Into<u32>>` is not implemented for `impl Debug`
6762
|
68-
LL | fn bad(x: impl Into<u32> + std::fmt::Debug) -> impl Into<impl Debug> { x }
69-
| +++++++++++++++++
63+
= note: required because of the requirements on the impl of `Into<impl Debug>` for `impl Into<u32>`
7064

7165
error: aborting due to 8 previous errors
7266

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
type Foo = impl PartialEq<(Foo, i32)>;
4+
5+
struct Bar;
6+
7+
impl PartialEq<(Foo, i32)> for Bar {
8+
fn eq(&self, _other: &(Foo, i32)) -> bool {
9+
true
10+
}
11+
}
12+
13+
fn foo() -> Foo {
14+
Bar //~ ERROR can't compare `Bar` with `(Bar, i32)`
15+
}
16+
17+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: can't compare `Bar` with `(Bar, i32)`
2+
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:14:5
3+
|
4+
LL | Bar
5+
| ^^^ no implementation for `Bar == (Bar, i32)`
6+
|
7+
= help: the trait `PartialEq<(Bar, i32)>` is not implemented for `Bar`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/impl-trait/recursive-type-alias-impl-trait-declaration.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,18 @@
22

33
#![feature(type_alias_impl_trait)]
44

5-
mod direct {
6-
type Foo = impl PartialEq<(Foo, i32)>;
5+
type Foo = impl PartialEq<(Foo, i32)>;
76

8-
struct Bar;
7+
struct Bar;
98

10-
impl PartialEq<(Foo, i32)> for Bar {
11-
fn eq(&self, _other: &(Foo, i32)) -> bool {
12-
true
13-
}
14-
}
15-
16-
fn foo() -> Foo {
17-
Bar
9+
impl PartialEq<(Bar, i32)> for Bar {
10+
fn eq(&self, _other: &(Bar, i32)) -> bool {
11+
true
1812
}
1913
}
2014

21-
mod indirect {
22-
type Foo = impl PartialEq<(Foo, i32)>;
23-
24-
struct Bar;
25-
26-
impl PartialEq<(Bar, i32)> for Bar {
27-
fn eq(&self, _other: &(Bar, i32)) -> bool {
28-
true
29-
}
30-
}
31-
32-
fn foo() -> Foo {
33-
Bar
34-
}
15+
fn foo() -> Foo {
16+
Bar
3517
}
3618

3719
fn main() {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// check-pass
2+
3+
use std::fmt::Debug;
4+
5+
pub struct EventStream<S> {
6+
stream: S,
7+
}
8+
9+
impl<S: Debug> EventStream<S> {
10+
fn into_stream(self) -> impl Debug {
11+
unimplemented!()
12+
}
13+
14+
pub fn into_reader(self) -> impl Debug {
15+
ReaderStream::from(self.into_stream())
16+
}
17+
}
18+
19+
#[derive(Debug)]
20+
pub struct ReaderStream<S> {
21+
stream: S,
22+
}
23+
24+
impl<S> From<S> for ReaderStream<S> {
25+
fn from(stream: S) -> Self {
26+
ReaderStream { stream }
27+
}
28+
}
29+
30+
fn main() {}

0 commit comments

Comments
 (0)