Skip to content

Commit 769b1cf

Browse files
Normalize projections in opaque types
1 parent 088b987 commit 769b1cf

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed

src/librustc/infer/opaque_types/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,12 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
10371037
let predicates_of = tcx.predicates_of(def_id);
10381038
debug!("instantiate_opaque_types: predicates={:#?}", predicates_of,);
10391039
let bounds = predicates_of.instantiate(tcx, substs);
1040+
1041+
let param_env = tcx.param_env(def_id);
1042+
let InferOk { value: bounds, obligations } =
1043+
infcx.partially_normalize_associated_types_in(span, self.body_id, param_env, &bounds);
1044+
self.obligations.extend(obligations);
1045+
10401046
debug!("instantiate_opaque_types: bounds={:?}", bounds);
10411047

10421048
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,103 @@
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+
fn foo2_pass<'a, T: Trait<'a, Assoc=()> + 'a>() -> impl FooLike<Output=T::Assoc> + 'a {
47+
Foo(())
48+
}
49+
50+
/// Normalization to type containing bound region.
51+
fn foo2_pass2<'a, T: Trait<'a, Assoc=&'a ()> + 'a>() -> impl FooLike<Output=T::Assoc> + 'a {
52+
Foo(&())
53+
}
54+
}
55+
56+
/////////////////////////////////////////////
57+
// Reduction using `impl Trait` in bindings
58+
59+
mod impl_trait_in_bindings {
60+
struct Foo;
61+
62+
trait FooLike { type Output; }
63+
64+
impl FooLike for Foo {
65+
type Output = u32;
66+
}
67+
68+
trait Trait {
69+
type Assoc;
70+
}
71+
72+
fn foo<T: Trait<Assoc=u32>>() {
73+
let _: impl FooLike<Output=T::Assoc> = Foo;
74+
}
75+
}
76+
77+
/////////////////////////////////////////////
78+
// The same applied to `existential type`s
79+
80+
mod existential_types {
81+
trait Implemented {
82+
type Assoc;
83+
}
84+
impl<T> Implemented for T {
85+
type Assoc = u8;
86+
}
87+
88+
trait Trait {
89+
type Out;
90+
}
91+
92+
impl Trait for () {
93+
type Out = u8;
94+
}
95+
96+
existential type Ex: Trait<Out = <() as Implemented>::Assoc>;
97+
98+
fn define() -> Ex {
99+
()
100+
}
101+
}
102+
103+
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)