Skip to content

Commit b5f893b

Browse files
Implement Generator and Future
1 parent a274046 commit b5f893b

File tree

8 files changed

+259
-1
lines changed

8 files changed

+259
-1
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

+14
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy + Eq {
133133
ecx: &mut EvalCtxt<'_, 'tcx>,
134134
goal: Goal<'tcx, Self>,
135135
) -> QueryResult<'tcx>;
136+
137+
fn consider_builtin_future_candidate(
138+
ecx: &mut EvalCtxt<'_, 'tcx>,
139+
goal: Goal<'tcx, Self>,
140+
) -> QueryResult<'tcx>;
141+
142+
fn consider_builtin_generator_candidate(
143+
ecx: &mut EvalCtxt<'_, 'tcx>,
144+
goal: Goal<'tcx, Self>,
145+
) -> QueryResult<'tcx>;
136146
}
137147

138148
impl<'tcx> EvalCtxt<'_, 'tcx> {
@@ -259,6 +269,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
259269
G::consider_builtin_fn_trait_candidates(self, goal, kind)
260270
} else if lang_items.tuple_trait() == Some(trait_def_id) {
261271
G::consider_builtin_tuple_candidate(self, goal)
272+
} else if lang_items.future_trait() == Some(trait_def_id) {
273+
G::consider_builtin_future_candidate(self, goal)
274+
} else if lang_items.gen_trait() == Some(trait_def_id) {
275+
G::consider_builtin_generator_candidate(self, goal)
262276
} else {
263277
Err(NoSolution)
264278
};

compiler/rustc_trait_selection/src/solve/project_goals.rs

+68-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
1515
use rustc_middle::ty::{self, Ty, TyCtxt};
1616
use rustc_middle::ty::{ProjectionPredicate, TypeSuperVisitable, TypeVisitor};
1717
use rustc_middle::ty::{ToPredicate, TypeVisitable};
18-
use rustc_span::DUMMY_SP;
18+
use rustc_span::{sym, DUMMY_SP};
1919
use std::iter;
2020
use std::ops::ControlFlow;
2121

@@ -391,6 +391,73 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
391391
) -> QueryResult<'tcx> {
392392
bug!("`Tuple` does not have an associated type: {:?}", goal);
393393
}
394+
395+
fn consider_builtin_future_candidate(
396+
ecx: &mut EvalCtxt<'_, 'tcx>,
397+
goal: Goal<'tcx, Self>,
398+
) -> QueryResult<'tcx> {
399+
let self_ty = goal.predicate.self_ty();
400+
let ty::Generator(def_id, substs, _) = *self_ty.kind() else {
401+
return Err(NoSolution);
402+
};
403+
404+
// Generators are not futures unless they come from `async` desugaring
405+
let tcx = ecx.tcx();
406+
if !tcx.generator_is_async(def_id) {
407+
return Err(NoSolution);
408+
}
409+
410+
let term = substs.as_generator().return_ty().into();
411+
412+
Self::consider_assumption(
413+
ecx,
414+
goal,
415+
ty::Binder::dummy(ty::ProjectionPredicate {
416+
projection_ty: ecx.tcx().mk_alias_ty(goal.predicate.def_id(), [self_ty]),
417+
term,
418+
})
419+
.to_predicate(tcx),
420+
)
421+
}
422+
423+
fn consider_builtin_generator_candidate(
424+
ecx: &mut EvalCtxt<'_, 'tcx>,
425+
goal: Goal<'tcx, Self>,
426+
) -> QueryResult<'tcx> {
427+
let self_ty = goal.predicate.self_ty();
428+
let ty::Generator(def_id, substs, _) = *self_ty.kind() else {
429+
return Err(NoSolution);
430+
};
431+
432+
// `async`-desugared generators do not implement the generator trait
433+
let tcx = ecx.tcx();
434+
if tcx.generator_is_async(def_id) {
435+
return Err(NoSolution);
436+
}
437+
438+
let generator = substs.as_generator();
439+
440+
let name = tcx.associated_item(goal.predicate.def_id()).name;
441+
let term = if name == sym::Return {
442+
generator.return_ty().into()
443+
} else if name == sym::Yield {
444+
generator.yield_ty().into()
445+
} else {
446+
bug!("unexpected associated item `<{self_ty} as Generator>::{name}`")
447+
};
448+
449+
Self::consider_assumption(
450+
ecx,
451+
goal,
452+
ty::Binder::dummy(ty::ProjectionPredicate {
453+
projection_ty: ecx
454+
.tcx()
455+
.mk_alias_ty(goal.predicate.def_id(), [self_ty, generator.resume_ty()]),
456+
term,
457+
})
458+
.to_predicate(tcx),
459+
)
460+
}
394461
}
395462

396463
/// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code.

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+44
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,50 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
185185
Err(NoSolution)
186186
}
187187
}
188+
189+
fn consider_builtin_future_candidate(
190+
ecx: &mut EvalCtxt<'_, 'tcx>,
191+
goal: Goal<'tcx, Self>,
192+
) -> QueryResult<'tcx> {
193+
let ty::Generator(def_id, _, _) = *goal.predicate.self_ty().kind() else {
194+
return Err(NoSolution);
195+
};
196+
197+
// Generators are not futures unless they come from `async` desugaring
198+
let tcx = ecx.tcx();
199+
if !tcx.generator_is_async(def_id) {
200+
return Err(NoSolution);
201+
}
202+
203+
// Async generator unconditionally implement `Future`
204+
ecx.make_canonical_response(Certainty::Yes)
205+
}
206+
207+
fn consider_builtin_generator_candidate(
208+
ecx: &mut EvalCtxt<'_, 'tcx>,
209+
goal: Goal<'tcx, Self>,
210+
) -> QueryResult<'tcx> {
211+
let self_ty = goal.predicate.self_ty();
212+
let ty::Generator(def_id, substs, _) = *self_ty.kind() else {
213+
return Err(NoSolution);
214+
};
215+
216+
// `async`-desugared generators do not implement the generator trait
217+
let tcx = ecx.tcx();
218+
if tcx.generator_is_async(def_id) {
219+
return Err(NoSolution);
220+
}
221+
222+
let generator = substs.as_generator();
223+
Self::consider_assumption(
224+
ecx,
225+
goal,
226+
ty::Binder::dummy(
227+
tcx.mk_trait_ref(goal.predicate.def_id(), [self_ty, generator.resume_ty()]),
228+
)
229+
.to_predicate(tcx),
230+
)
231+
}
188232
}
189233

190234
impl<'tcx> EvalCtxt<'_, 'tcx> {

compiler/rustc_trait_selection/src/solve/trait_goals/structural_traits.rs

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ pub(super) fn instantiate_constituent_tys_for_copy_clone_trait<'tcx>(
173173
}
174174
}
175175

176+
// Returns a binder of the tupled inputs types and output type from a builtin callable type.
176177
pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>(
177178
tcx: TyCtxt<'tcx>,
178179
self_ty: Ty<'tcx>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0271]: expected `[async block@$DIR/async.rs:12:17: 12:25]` to be a future that resolves to `i32`, but it resolves to `()`
2+
--> $DIR/async.rs:12:17
3+
|
4+
LL | needs_async(async {});
5+
| ----------- ^^^^^^^^ expected `i32`, found `()`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
note: required by a bound in `needs_async`
10+
--> $DIR/async.rs:8:31
11+
|
12+
LL | fn needs_async(_: impl Future<Output = i32>) {}
13+
| ^^^^^^^^^^^^ required by this bound in `needs_async`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0271`.

tests/ui/traits/new-solver/async.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-flags: -Ztrait-solver=next
2+
// edition: 2021
3+
// revisions: pass fail
4+
//[pass] check-pass
5+
6+
use std::future::Future;
7+
8+
fn needs_async(_: impl Future<Output = i32>) {}
9+
10+
#[cfg(fail)]
11+
fn main() {
12+
needs_async(async {});
13+
//[fail]~^ ERROR to be a future that resolves to `i32`, but it resolves to `()`
14+
}
15+
16+
#[cfg(pass)]
17+
fn main() {
18+
needs_async(async { 1i32 });
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
error[E0277]: the trait bound `[generator@$DIR/generator.rs:18:21: 18:23]: Generator<A>` is not satisfied
2+
--> $DIR/generator.rs:18:21
3+
|
4+
LL | needs_generator(|| {
5+
| _____---------------_^
6+
| | |
7+
| | required by a bound introduced by this call
8+
LL | |
9+
LL | |
10+
LL | |
11+
LL | | yield ();
12+
LL | | });
13+
| |_____^ the trait `Generator<A>` is not implemented for `[generator@$DIR/generator.rs:18:21: 18:23]`
14+
|
15+
note: required by a bound in `needs_generator`
16+
--> $DIR/generator.rs:14:28
17+
|
18+
LL | fn needs_generator(_: impl Generator<A, Yield = B, Return = C>) {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `needs_generator`
20+
21+
error[E0271]: type mismatch resolving `<[generator@$DIR/generator.rs:18:21: 18:23] as Generator<A>>::Yield == B`
22+
--> $DIR/generator.rs:18:21
23+
|
24+
LL | needs_generator(|| {
25+
| _____---------------_^
26+
| | |
27+
| | required by a bound introduced by this call
28+
LL | |
29+
LL | |
30+
LL | |
31+
LL | | yield ();
32+
LL | | });
33+
| |_____^ types differ
34+
|
35+
note: required by a bound in `needs_generator`
36+
--> $DIR/generator.rs:14:41
37+
|
38+
LL | fn needs_generator(_: impl Generator<A, Yield = B, Return = C>) {}
39+
| ^^^^^^^^^ required by this bound in `needs_generator`
40+
41+
error[E0271]: type mismatch resolving `<[generator@$DIR/generator.rs:18:21: 18:23] as Generator<A>>::Return == C`
42+
--> $DIR/generator.rs:18:21
43+
|
44+
LL | needs_generator(|| {
45+
| _____---------------_^
46+
| | |
47+
| | required by a bound introduced by this call
48+
LL | |
49+
LL | |
50+
LL | |
51+
LL | | yield ();
52+
LL | | });
53+
| |_____^ types differ
54+
|
55+
note: required by a bound in `needs_generator`
56+
--> $DIR/generator.rs:14:52
57+
|
58+
LL | fn needs_generator(_: impl Generator<A, Yield = B, Return = C>) {}
59+
| ^^^^^^^^^^ required by this bound in `needs_generator`
60+
61+
error: aborting due to 3 previous errors
62+
63+
Some errors have detailed explanations: E0271, E0277.
64+
For more information about an error, try `rustc --explain E0271`.
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// compile-flags: -Ztrait-solver=next
2+
// edition: 2021
3+
// revisions: pass fail
4+
//[pass] check-pass
5+
6+
#![feature(generator_trait, generators)]
7+
8+
use std::ops::Generator;
9+
10+
struct A;
11+
struct B;
12+
struct C;
13+
14+
fn needs_generator(_: impl Generator<A, Yield = B, Return = C>) {}
15+
16+
#[cfg(fail)]
17+
fn main() {
18+
needs_generator(|| {
19+
//[fail]~^ ERROR Generator<A>` is not satisfied
20+
//[fail]~| ERROR as Generator<A>>::Yield == B`
21+
//[fail]~| ERROR as Generator<A>>::Return == C`
22+
yield ();
23+
});
24+
}
25+
26+
#[cfg(pass)]
27+
fn main() {
28+
needs_generator(|_: A| {
29+
let _: A = yield B;
30+
C
31+
})
32+
}

0 commit comments

Comments
 (0)