Skip to content

Commit 0b680cf

Browse files
committed
Auto merge of #62221 - jonas-schievink:normalize-impl-trait, r=nikomatsakis
Normalize projections appearing in `impl Trait` Fixes #60414 This does not try to do the same for `existential type`s (which have the same bug), since that always seems to lead to cycle errors.
2 parents b8ec4c4 + 66e0266 commit 0b680cf

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

src/librustc/infer/opaque_types/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,12 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
11111111
let predicates_of = tcx.predicates_of(def_id);
11121112
debug!("instantiate_opaque_types: predicates={:#?}", predicates_of,);
11131113
let bounds = predicates_of.instantiate(tcx, substs);
1114+
1115+
let param_env = tcx.param_env(def_id);
1116+
let InferOk { value: bounds, obligations } =
1117+
infcx.partially_normalize_associated_types_in(span, self.body_id, param_env, &bounds);
1118+
self.obligations.extend(obligations);
1119+
11141120
debug!("instantiate_opaque_types: bounds={:?}", bounds);
11151121

11161122
let required_region_bounds = tcx.required_region_bounds(ty, bounds.predicates.clone());

src/librustc_typeck/check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ fn check_fn<'a, 'tcx>(
10651065
&declared_ret_ty,
10661066
decl.output.span(),
10671067
);
1068+
debug!("check_fn: declared_ret_ty: {}, revealed_ret_ty: {}", declared_ret_ty, revealed_ret_ty);
10681069
fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(revealed_ret_ty)));
10691070
fn_sig = fcx.tcx.mk_fn_sig(
10701071
fn_sig.inputs().iter().cloned(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
// edition:2018
3+
4+
#![feature(async_await)]
5+
6+
// See issue 60414
7+
8+
trait Trait {
9+
type Assoc;
10+
}
11+
12+
async fn foo<T: Trait<Assoc=()>>() -> T::Assoc {
13+
()
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// compile-fail
2+
// edition:2018
3+
4+
#![feature(async_await)]
5+
#![feature(existential_type)]
6+
#![feature(impl_trait_in_bindings)]
7+
//~^ WARNING the feature `impl_trait_in_bindings` is incomplete
8+
9+
// See issue 60414
10+
11+
/////////////////////////////////////////////
12+
// Reduction to `impl Trait`
13+
14+
struct Foo<T>(T);
15+
16+
trait FooLike { type Output; }
17+
18+
impl<T> FooLike for Foo<T> {
19+
type Output = T;
20+
}
21+
22+
mod impl_trait {
23+
use super::*;
24+
25+
trait Trait {
26+
type Assoc;
27+
}
28+
29+
/// `T::Assoc` can't be normalized any further here.
30+
fn foo_fail<T: Trait>() -> impl FooLike<Output=T::Assoc> {
31+
//~^ ERROR: type mismatch
32+
Foo(())
33+
}
34+
}
35+
36+
/////////////////////////////////////////////
37+
// Same with lifetimes in the trait
38+
39+
mod lifetimes {
40+
use super::*;
41+
42+
trait Trait<'a> {
43+
type Assoc;
44+
}
45+
46+
/// Missing bound constraining `Assoc`, `T::Assoc` can't be normalized further.
47+
fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike<Output=T::Assoc> {
48+
//~^ ERROR: type mismatch
49+
Foo(())
50+
}
51+
}
52+
53+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
warning: the feature `impl_trait_in_bindings` is incomplete and may cause the compiler to crash
2+
--> $DIR/bound-normalization-fail.rs:6:12
3+
|
4+
LL | #![feature(impl_trait_in_bindings)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as impl_trait::Trait>::Assoc`
8+
--> $DIR/bound-normalization-fail.rs:30:32
9+
|
10+
LL | fn foo_fail<T: Trait>() -> impl FooLike<Output=T::Assoc> {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found associated type
12+
|
13+
= note: expected type `()`
14+
found type `<T as impl_trait::Trait>::Assoc`
15+
= note: the return type of a function must have a statically known size
16+
17+
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as lifetimes::Trait<'static>>::Assoc`
18+
--> $DIR/bound-normalization-fail.rs:47:41
19+
|
20+
LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike<Output=T::Assoc> {
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found associated type
22+
|
23+
= note: expected type `()`
24+
found type `<T as lifetimes::Trait<'static>>::Assoc`
25+
= note: the return type of a function must have a statically known size
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0271`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// check-pass
2+
// edition:2018
3+
4+
#![feature(async_await)]
5+
#![feature(existential_type)]
6+
#![feature(impl_trait_in_bindings)]
7+
//~^ WARNING the feature `impl_trait_in_bindings` is incomplete
8+
9+
// See issue 60414
10+
11+
/////////////////////////////////////////////
12+
// Reduction to `impl Trait`
13+
14+
struct Foo<T>(T);
15+
16+
trait FooLike { type Output; }
17+
18+
impl<T> FooLike for Foo<T> {
19+
type Output = T;
20+
}
21+
22+
mod impl_trait {
23+
use super::*;
24+
25+
trait Trait {
26+
type Assoc;
27+
}
28+
29+
/// `T::Assoc` should be normalized to `()` here.
30+
fn foo_pass<T: Trait<Assoc=()>>() -> impl FooLike<Output=T::Assoc> {
31+
Foo(())
32+
}
33+
}
34+
35+
/////////////////////////////////////////////
36+
// Same with lifetimes in the trait
37+
38+
mod lifetimes {
39+
use super::*;
40+
41+
trait Trait<'a> {
42+
type Assoc;
43+
}
44+
45+
/// Like above.
46+
///
47+
/// FIXME(#51525) -- the shorter notation `T::Assoc` winds up referencing `'static` here
48+
fn foo2_pass<'a, T: Trait<'a, Assoc=()> + 'a>(
49+
) -> impl FooLike<Output=<T as Trait<'a>>::Assoc> + 'a {
50+
Foo(())
51+
}
52+
53+
/// Normalization to type containing bound region.
54+
///
55+
/// FIXME(#51525) -- the shorter notation `T::Assoc` winds up referencing `'static` here
56+
fn foo2_pass2<'a, T: Trait<'a, Assoc=&'a ()> + 'a>(
57+
) -> impl FooLike<Output=<T as Trait<'a>>::Assoc> + 'a {
58+
Foo(&())
59+
}
60+
}
61+
62+
/////////////////////////////////////////////
63+
// Reduction using `impl Trait` in bindings
64+
65+
mod impl_trait_in_bindings {
66+
struct Foo;
67+
68+
trait FooLike { type Output; }
69+
70+
impl FooLike for Foo {
71+
type Output = u32;
72+
}
73+
74+
trait Trait {
75+
type Assoc;
76+
}
77+
78+
fn foo<T: Trait<Assoc=u32>>() {
79+
let _: impl FooLike<Output=T::Assoc> = Foo;
80+
}
81+
}
82+
83+
/////////////////////////////////////////////
84+
// The same applied to `existential type`s
85+
86+
mod existential_types {
87+
trait Implemented {
88+
type Assoc;
89+
}
90+
impl<T> Implemented for T {
91+
type Assoc = u8;
92+
}
93+
94+
trait Trait {
95+
type Out;
96+
}
97+
98+
impl Trait for () {
99+
type Out = u8;
100+
}
101+
102+
existential type Ex: Trait<Out = <() as Implemented>::Assoc>;
103+
104+
fn define() -> Ex {
105+
()
106+
}
107+
}
108+
109+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
warning: the feature `impl_trait_in_bindings` is incomplete and may cause the compiler to crash
2+
--> $DIR/bound-normalization-pass.rs:6:12
3+
|
4+
LL | #![feature(impl_trait_in_bindings)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+

0 commit comments

Comments
 (0)