Skip to content

Commit d3812ac

Browse files
LangItem-ify Coroutine trait in solvers
1 parent 921645c commit d3812ac

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

compiler/rustc_hir/src/lang_items.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ language_item_table! {
244244
AsyncIterator, sym::async_iterator, async_iterator_trait, Target::Trait, GenericRequirement::Exact(0);
245245

246246
CoroutineState, sym::coroutine_state, coroutine_state, Target::Enum, GenericRequirement::None;
247-
Coroutine, sym::coroutine, coroutine_trait, Target::Trait, GenericRequirement::Minimum(1);
247+
Coroutine, sym::coroutine, coroutine_trait, Target::Trait, GenericRequirement::Exact(1);
248+
CoroutineReturn, sym::coroutine_return, coroutine_return, Target::AssocTy, GenericRequirement::Exact(1);
249+
CoroutineYield, sym::coroutine_yield, coroutine_yield, Target::AssocTy, GenericRequirement::Exact(1);
248250
CoroutineResume, sym::coroutine_resume, coroutine_resume, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
249251

250252
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,9 @@ symbols! {
635635
coroutine,
636636
coroutine_clone,
637637
coroutine_resume,
638+
coroutine_return,
638639
coroutine_state,
640+
coroutine_yield,
639641
coroutines,
640642
cosf128,
641643
cosf16,

compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::ty::NormalizesTo;
1717
use rustc_middle::ty::{self, Ty, TyCtxt};
1818
use rustc_middle::ty::{TypeVisitableExt, Upcast};
1919
use rustc_middle::{bug, span_bug};
20-
use rustc_span::{sym, ErrorGuaranteed, DUMMY_SP};
20+
use rustc_span::{ErrorGuaranteed, DUMMY_SP};
2121

2222
mod anon_const;
2323
mod inherent;
@@ -719,13 +719,16 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
719719

720720
let coroutine = args.as_coroutine();
721721

722-
let name = tcx.associated_item(goal.predicate.def_id()).name;
723-
let term = if name == sym::Return {
722+
let lang_items = tcx.lang_items();
723+
let term = if Some(goal.predicate.def_id()) == lang_items.coroutine_return() {
724724
coroutine.return_ty().into()
725-
} else if name == sym::Yield {
725+
} else if Some(goal.predicate.def_id()) == lang_items.coroutine_yield() {
726726
coroutine.yield_ty().into()
727727
} else {
728-
bug!("unexpected associated item `<{self_ty} as Coroutine>::{name}`")
728+
bug!(
729+
"unexpected associated item `<{self_ty} as Coroutine>::{}`",
730+
tcx.item_name(goal.predicate.def_id())
731+
)
729732
};
730733

731734
Self::probe_and_consider_implied_clause(

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,15 +1373,16 @@ fn confirm_coroutine_candidate<'cx, 'tcx>(
13731373
coroutine_sig,
13741374
);
13751375

1376-
let name = tcx.associated_item(obligation.predicate.def_id).name;
1377-
let ty = if name == sym::Return {
1376+
let lang_items = tcx.lang_items();
1377+
let ty = if Some(obligation.predicate.def_id) == lang_items.coroutine_return() {
13781378
return_ty
1379-
} else if name == sym::Yield {
1379+
} else if Some(obligation.predicate.def_id) == lang_items.coroutine_yield() {
13801380
yield_ty
13811381
} else {
13821382
span_bug!(
13831383
tcx.def_span(obligation.predicate.def_id),
1384-
"unexpected associated type: `Coroutine::{name}`"
1384+
"unexpected associated type: `Coroutine::{}`",
1385+
tcx.item_name(obligation.predicate.def_id),
13851386
);
13861387
};
13871388

library/core/src/ops/coroutine.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub trait Coroutine<R = ()> {
7676
/// values which are allowed to be returned each time a coroutine yields.
7777
/// For example an iterator-as-a-coroutine would likely have this type as
7878
/// `T`, the type being iterated over.
79+
#[cfg_attr(not(bootstrap), lang = "coroutine_yield")]
7980
type Yield;
8081

8182
/// The type of value this coroutine returns.
@@ -84,6 +85,7 @@ pub trait Coroutine<R = ()> {
8485
/// `return` statement or implicitly as the last expression of a coroutine
8586
/// literal. For example futures would use this as `Result<T, E>` as it
8687
/// represents a completed future.
88+
#[cfg_attr(not(bootstrap), lang = "coroutine_return")]
8789
type Return;
8890

8991
/// Resumes the execution of this coroutine.

0 commit comments

Comments
 (0)