Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,6 @@ impl Expr {

ExprKind::Break(_ /*label*/, value)
| ExprKind::Ret(value)
| ExprKind::Yield(YieldKind::Prefix(value))
| ExprKind::Yeet(value) => match value {
Some(_) => ExprPrecedence::Jump,
None => prefix_attrs_precedence(&self.attrs),
Expand Down Expand Up @@ -2090,8 +2089,6 @@ pub enum MatchKind {
/// The kind of yield expression
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub enum YieldKind {
/// yield expr { ... }
Prefix(Option<Box<Expr>>),
/// expr.yield { ... }
Postfix(Box<Expr>),
}
Expand All @@ -2102,25 +2099,21 @@ impl YieldKind {
/// For postfix yields, this is guaranteed to be `Some`.
pub const fn expr(&self) -> Option<&Box<Expr>> {
match self {
YieldKind::Prefix(expr) => expr.as_ref(),
YieldKind::Postfix(expr) => Some(expr),
}
}

/// Returns a mutable reference to the expression being yielded, if any.
pub const fn expr_mut(&mut self) -> Option<&mut Box<Expr>> {
match self {
YieldKind::Prefix(expr) => expr.as_mut(),
YieldKind::Postfix(expr) => Some(expr),
}
}

/// Returns true if both yields are prefix or both are postfix.
pub const fn same_kind(&self, other: &Self) -> bool {
match (self, other) {
(YieldKind::Prefix(_), YieldKind::Prefix(_)) => true,
(YieldKind::Postfix(_), YieldKind::Postfix(_)) => true,
_ => false,
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
/// ::std::task::Poll::Ready(result) => break result,
/// ::std::task::Poll::Pending => {}
/// }
/// task_context = yield ();
/// task_context = ().yield;
/// }
/// }
/// ```
Expand Down Expand Up @@ -962,8 +962,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
};

// Depending on `async` of `async gen`:
// async - task_context = yield ();
// async gen - task_context = yield ASYNC_GEN_PENDING;
// async - task_context = ().yield;
// async gen - task_context = ASYNC_GEN_PENDING.yield;
let yield_stmt = {
let yielded = if is_async_gen {
self.arena.alloc(self.expr_lang_item_path(span, hir::LangItem::AsyncGenPending))
Expand Down Expand Up @@ -1713,7 +1713,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};

if is_async_gen {
// `yield $expr` is transformed into `task_context = yield async_gen_ready($expr)`.
// `$expr.yield` is transformed into `task_context = async_gen_ready($expr).yield`.
// This ensures that we store our resumed `ResumeContext` correctly, and also that
// the apparent value of the `yield` expression is `()`.
let desugar_span = self.mark_span_with_reason(
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,14 +794,6 @@ impl<'a> State<'a> {
self.print_expr(e, FixupContext::default());
self.pclose();
}
ast::ExprKind::Yield(YieldKind::Prefix(e)) => {
self.word("yield");

if let Some(expr) = e {
self.space();
self.print_expr(expr, fixup.rightmost_subexpression());
}
}
ast::ExprKind::Yield(YieldKind::Postfix(e)) => {
self.print_expr_cond_paren(
e,
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_ast_pretty/src/pprust/state/fixup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_ast::util::classify;
use rustc_ast::util::parser::{self, ExprPrecedence};
use rustc_ast::{Expr, ExprKind, YieldKind};
use rustc_ast::{Expr, ExprKind};

// The default amount of fixing is minimal fixing, so all fixups are set to `false` by `Default`.
// Fixups should be turned on in a targeted fashion where needed.
Expand Down Expand Up @@ -239,11 +239,7 @@ impl FixupContext {
// Decrease precedence of value-less jumps when followed by an
// operator that would otherwise get interpreted as beginning a
// value for the jump.
if let ExprKind::Break(..)
| ExprKind::Ret(..)
| ExprKind::Yeet(..)
| ExprKind::Yield(YieldKind::Prefix(..)) = expr.kind
{
if let ExprKind::Break(..) | ExprKind::Ret(..) | ExprKind::Yeet(..) = expr.kind {
return ExprPrecedence::Jump;
}
}
Expand All @@ -255,7 +251,6 @@ impl FixupContext {
| ExprKind::Closure(..)
| ExprKind::Ret(..)
| ExprKind::Yeet(..)
| ExprKind::Yield(YieldKind::Prefix(..))
| ExprKind::Range(None, ..) = expr.kind
{
return ExprPrecedence::Prefix;
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_cranelift/example/gen_block_iterate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@

fn foo() -> impl Iterator<Item = u32> {
gen {
yield 42;
42.yield;
for x in 3..6 {
yield x
x.yield;
}
}
}

fn moved() -> impl Iterator<Item = u32> {
let mut x = "foo".to_string();
gen move {
yield 42;
42.yield;
if x == "foo" {
return;
}
x.clear();
for x in 3..6 {
yield x
x.yield;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/example/std_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn main() {
Box::pin(
#[coroutine]
move |mut _task_context| {
yield ();
().yield;
},
)
.as_mut()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/example/std_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn main() {
}

Box::pin(#[coroutine] move |mut _task_context| {
yield ();
().yield;
}).as_mut().resume(0);

println!("End");
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_error_codes/src/error_codes/E0626.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Erroneous code example:
# use std::pin::Pin;
let mut b = #[coroutine] || {
let a = &String::new(); // <-- This borrow...
yield (); // ...is still in scope here, when the yield occurs.
().yield; // ...is still in scope here, when the yield occurs.
println!("{}", a);
};
Pin::new(&mut b).resume(());
Expand All @@ -26,7 +26,7 @@ scope. To resolve this error, the coroutine may be marked `static`:
# use std::pin::Pin;
let mut b = #[coroutine] static || { // <-- note the static keyword
let a = &String::from("hello, world");
yield ();
().yield;
println!("{}", a);
};
let mut b = std::pin::pin!(b);
Expand All @@ -44,7 +44,7 @@ type by value:
# use std::pin::Pin;
let mut b = #[coroutine] || {
let a = String::from("hello, world");
yield ();
().yield;
println!("{}", a);
};
Pin::new(&mut b).resume(());
Expand All @@ -63,7 +63,7 @@ This error also frequently arises with iteration:
let mut b = #[coroutine] || {
let v = vec![1,2,3];
for &x in &v { // <-- borrow of `v` is still in scope...
yield x; // ...when this yield occurs.
x.yield; // ...when this yield occurs.
}
};
Pin::new(&mut b).resume(());
Expand All @@ -79,7 +79,7 @@ Such cases can sometimes be resolved by iterating "by value" (or using
let mut b = #[coroutine] || {
let v = vec![1,2,3];
for x in v { // <-- Take ownership of the values instead!
yield x; // <-- Now yield is OK.
x.yield; // <-- Now yield is OK.
}
};
Pin::new(&mut b).resume(());
Expand All @@ -96,7 +96,7 @@ let mut b = #[coroutine] || {
let len = v.len(); // (*)
for i in 0..len {
let x = v[i]; // (*)
yield x; // <-- Now yield is OK.
x.yield; // <-- Now yield is OK.
}
};
Pin::new(&mut b).resume(());
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0627.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Erroneous code example:
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]

fn fake_coroutine() -> &'static str {
yield 1;
1.yield;
return "foo"
}

Expand All @@ -23,7 +23,7 @@ literal. This can be fixed by constructing the coroutine correctly.

fn main() {
let mut coroutine = #[coroutine] || {
yield 1;
1.yield;
return "foo"
};
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0628.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
let coroutine = #[coroutine] |a: i32, b: i32| {
// error: too many parameters for a coroutine
// Allowed only 0 or 1 parameter
yield a;
a.yield;
};
}
```
Expand All @@ -24,7 +24,7 @@ the previous example by passing only one parameter.

fn main() {
let coroutine = #[coroutine] |a: i32| {
yield a;
a.yield;
};
}
```
4 changes: 2 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0727.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Erroneous code example:
fn main() {
let coroutine = #[coroutine] || {
async {
yield;
().yield;
}
};
}
Expand All @@ -24,7 +24,7 @@ To fix this error, you have to move `yield` out of the `async` block:

fn main() {
let coroutine = #[coroutine] || {
yield;
().yield;
};
}
```
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ pub enum ClosureKind {
Closure,
/// This is a coroutine expression -- i.e. a closure expression in which
/// we've found a `yield`. These can arise either from "plain" coroutine
/// usage (e.g. `let x = || { yield (); }`) or from a desugared expression
/// usage (e.g. `let x = || { ().yield; }`) or from a desugared expression
/// (e.g. `async` and `gen` blocks).
Coroutine(CoroutineKind),
/// This is a coroutine-closure, which is a special sugared closure that
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2195,7 +2195,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
{
// Yield the trait's def id. Supertraits will be
// elaborated from that.
yield item.owner_id.def_id.to_def_id();
item.owner_id.def_id.to_def_id().yield;
} else if let Some(generics) = node.generics() {
for pred in generics.predicates {
let hir::WherePredicateKind::BoundPredicate(pred) = pred.kind else {
Expand All @@ -2216,7 +2216,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
if let Some(def_id) =
poly_trait_ref.trait_ref.trait_def_id()
{
yield def_id;
def_id.yield;
}
}
hir::GenericBound::Outlives(_)
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,20 +1288,20 @@ impl<'a> CrateMetadataRef<'a> {
// the view of this crate as a proc macro crate.
if id == CRATE_DEF_INDEX {
for child_index in data.macros.decode(self) {
yield self.get_mod_child(child_index, sess);
self.get_mod_child(child_index, sess).yield;
}
}
} else {
// Iterate over all children.
let non_reexports = self.root.tables.module_children_non_reexports.get(self, id);
for child_index in non_reexports.unwrap().decode(self) {
yield self.get_mod_child(child_index, sess);
self.get_mod_child(child_index, sess).yield;
}

let reexports = self.root.tables.module_children_reexports.get(self, id);
if !reexports.is_default() {
for reexport in reexports.decode((self, sess)) {
yield reexport;
reexport.yield;
}
}
}
Expand Down Expand Up @@ -2016,7 +2016,7 @@ impl CrateMetadata {
.decode(CrateMetadataRef { cdata: self, cstore })
.map(move |index| DefId { index, krate })
}) {
yield def_id;
def_id.yield;
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_middle/src/ty/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,8 @@ pub fn analyze_coroutine_closure_captures<'a, 'tcx: 'a, T>(
child capture ({child_capture:#?})"
);

yield for_each(
(parent_field_idx, parent_capture),
(child_field_idx, child_capture),
);
for_each((parent_field_idx, parent_capture), (child_field_idx, child_capture))
.yield;

field_used_at_least_once = true;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ impl<'tcx> TyCtxt<'tcx> {
// new ones.
while i < { definitions.read().num_definitions() } {
let local_def_index = rustc_span::def_id::DefIndex::from_usize(i);
yield LocalDefId { local_def_index };
LocalDefId { local_def_index }.yield;
i += 1;
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//! 1 - Coroutine has returned / is completed
//! 2 - Coroutine has been poisoned
//!
//! It also rewrites `return x` and `yield y` as setting a new coroutine state and returning
//! It also rewrites `return x` and `y.yield` as setting a new coroutine state and returning
//! `CoroutineState::Complete(x)` and `CoroutineState::Yielded(y)`,
//! or `Poll::Ready(x)` and `Poll::Pending` respectively.
//! MIR locals which are live across a suspension point are moved to the coroutine struct
Expand Down Expand Up @@ -1589,7 +1589,7 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {

// Run the transformation which converts Places from Local to coroutine struct
// accesses for locals in `remap`.
// It also rewrites `return x` and `yield y` as writing a new coroutine state and returning
// It also rewrites `return x` and `y.yield` as writing a new coroutine state and returning
// either `CoroutineState::Complete(x)` and `CoroutineState::Yielded(y)`,
// or `Poll::Ready(x)` and `Poll::Pending` respectively depending on the coroutine kind.
let mut transform = TransformVisitor {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/coverage/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ExpnTree {
) -> impl Iterator<Item = &ExpnNode> {
gen move {
let Some(root_node) = self.get(root_expn_id) else { return };
yield root_node;
root_node.yield;

// Stack of child-node-ID iterators that drives the depth-first traversal.
let mut iter_stack = vec![root_node.child_expn_ids.iter()];
Expand All @@ -40,7 +40,7 @@ impl ExpnTree {

// Yield this node.
let Some(node) = self.get(curr_id) else { continue };
yield node;
node.yield;

// Push the node's children, to be traversed next.
if !node.child_expn_ids.is_empty() {
Expand Down
Loading
Loading