Skip to content

Commit 2bd7f73

Browse files
committed
Refactor YieldKind so postfix yield must have an expression
1 parent 299e5d0 commit 2bd7f73

File tree

14 files changed

+65
-30
lines changed

14 files changed

+65
-30
lines changed

compiler/rustc_ast/src/ast.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ pub enum ExprKind {
16571657
Try(P<Expr>),
16581658

16591659
/// A `yield`, with an optional value to be yielded.
1660-
Yield(Option<P<Expr>>, YieldKind),
1660+
Yield(YieldKind),
16611661

16621662
/// A `do yeet` (aka `throw`/`fail`/`bail`/`raise`/whatever),
16631663
/// with an optional value to be returned.
@@ -1904,12 +1904,41 @@ pub enum MatchKind {
19041904
}
19051905

19061906
/// The kind of yield expression
1907-
#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq)]
1907+
#[derive(Clone, Encodable, Decodable, Debug)]
19081908
pub enum YieldKind {
19091909
/// yield expr { ... }
1910-
Prefix,
1910+
Prefix(Option<P<Expr>>),
19111911
/// expr.yield { ... }
1912-
Postfix,
1912+
Postfix(P<Expr>),
1913+
}
1914+
1915+
impl YieldKind {
1916+
/// Returns the expression inside the yield expression, if any.
1917+
///
1918+
/// For postfix yields, this is guaranteed to be `Some`.
1919+
pub const fn expr(&self) -> Option<&P<Expr>> {
1920+
match self {
1921+
YieldKind::Prefix(expr) => expr.as_ref(),
1922+
YieldKind::Postfix(expr) => Some(expr),
1923+
}
1924+
}
1925+
1926+
/// Returns a mutable reference to the expression being yielded, if any.
1927+
pub const fn expr_mut(&mut self) -> Option<&mut P<Expr>> {
1928+
match self {
1929+
YieldKind::Prefix(expr) => expr.as_mut(),
1930+
YieldKind::Postfix(expr) => Some(expr),
1931+
}
1932+
}
1933+
1934+
/// Returns true if both yields are prefix or both are postfix.
1935+
pub const fn same_kind(&self, other: &Self) -> bool {
1936+
match (self, other) {
1937+
(YieldKind::Prefix(_), YieldKind::Prefix(_)) => true,
1938+
(YieldKind::Postfix(_), YieldKind::Postfix(_)) => true,
1939+
_ => false,
1940+
}
1941+
}
19131942
}
19141943

19151944
/// A literal in a meta item.

compiler/rustc_ast/src/mut_visit.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1813,8 +1813,11 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
18131813
ExprKind::Paren(expr) => {
18141814
vis.visit_expr(expr);
18151815
}
1816-
ExprKind::Yield(expr, _) => {
1817-
visit_opt(expr, |expr| vis.visit_expr(expr));
1816+
ExprKind::Yield(kind) => {
1817+
let expr = kind.expr_mut();
1818+
if let Some(expr) = expr {
1819+
vis.visit_expr(expr);
1820+
}
18181821
}
18191822
ExprKind::Try(expr) => vis.visit_expr(expr),
18201823
ExprKind::TryBlock(body) => vis.visit_block(body),

compiler/rustc_ast/src/util/classify.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,14 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
182182
| Range(_, Some(e), _)
183183
| Ret(Some(e))
184184
| Unary(_, e)
185-
| Yield(Some(e), _)
186185
| Yeet(Some(e))
187186
| Become(e) => {
188187
expr = e;
189188
}
189+
Yield(kind) => match kind.expr() {
190+
Some(e) => expr = e,
191+
None => break None,
192+
},
190193
Closure(closure) => {
191194
expr = &closure.body;
192195
}
@@ -217,7 +220,6 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
217220
Break(_, None)
218221
| Range(_, None, _)
219222
| Ret(None)
220-
| Yield(None, _)
221223
| Array(_)
222224
| Call(_, _)
223225
| MethodCall(_)
@@ -237,7 +239,9 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
237239
| Yeet(None)
238240
| UnsafeBinderCast(..)
239241
| Err(_)
240-
| Dummy => break None,
242+
| Dummy => {
243+
break None;
244+
}
241245
}
242246
}
243247
}

compiler/rustc_ast/src/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,8 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
12691269
try_visit!(visitor.visit_ty(container));
12701270
walk_list!(visitor, visit_ident, fields.iter());
12711271
}
1272-
ExprKind::Yield(optional_expression, _) => {
1273-
visit_opt!(visitor, visit_expr, optional_expression);
1272+
ExprKind::Yield(kind) => {
1273+
visit_opt!(visitor, visit_expr, kind.expr());
12741274
}
12751275
ExprKind::Try(subexpression) => try_visit!(visitor.visit_expr(subexpression)),
12761276
ExprKind::TryBlock(body) => try_visit!(visitor.visit_block(body)),

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
351351
rest,
352352
)
353353
}
354-
ExprKind::Yield(opt_expr, _) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
354+
ExprKind::Yield(kind) => self.lower_expr_yield(e.span, kind.expr().map(|x| &**x)),
355355
ExprKind::Err(guar) => hir::ExprKind::Err(*guar),
356356

357357
ExprKind::UnsafeBinderCast(kind, expr, ty) => hir::ExprKind::UnsafeBinderCast(

compiler/rustc_ast_lowering/src/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ fn may_contain_yield_point(e: &ast::Expr) -> bool {
642642
type Result = ControlFlow<()>;
643643

644644
fn visit_expr(&mut self, e: &ast::Expr) -> ControlFlow<()> {
645-
if let ast::ExprKind::Await(_, _) | ast::ExprKind::Yield(_, _) = e.kind {
645+
if let ast::ExprKind::Await(_, _) | ast::ExprKind::Yield(_) = e.kind {
646646
ControlFlow::Break(())
647647
} else {
648648
visit::walk_expr(self, e)

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ impl<'a> State<'a> {
761761
self.print_expr(e, FixupContext::default());
762762
self.pclose();
763763
}
764-
ast::ExprKind::Yield(e, YieldKind::Prefix) => {
764+
ast::ExprKind::Yield(YieldKind::Prefix(e)) => {
765765
self.word("yield");
766766

767767
if let Some(expr) = e {
@@ -773,9 +773,7 @@ impl<'a> State<'a> {
773773
);
774774
}
775775
}
776-
ast::ExprKind::Yield(e, YieldKind::Postfix) => {
777-
// It's not possible to have a postfix yield with no expression.
778-
let e = e.as_ref().unwrap();
776+
ast::ExprKind::Yield(YieldKind::Postfix(e)) => {
779777
self.print_expr_cond_paren(
780778
e,
781779
e.precedence() < ExprPrecedence::Unambiguous,

compiler/rustc_builtin_macros/src/assert/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
323323
| ExprKind::While(_, _, _)
324324
| ExprKind::Yeet(_)
325325
| ExprKind::Become(_)
326-
| ExprKind::Yield(_, _)
326+
| ExprKind::Yield(_)
327327
| ExprKind::UnsafeBinderCast(..) => {}
328328
}
329329
}

compiler/rustc_parse/src/parser/expr.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1315,8 +1315,9 @@ impl<'a> Parser<'a> {
13151315
if self.eat_keyword(exp!(Yield)) {
13161316
let yield_span = self.prev_token.span;
13171317
self.psess.gated_spans.gate(sym::yield_expr, yield_span);
1318-
return Ok(self
1319-
.mk_expr(lo.to(yield_span), ExprKind::Yield(Some(self_arg), YieldKind::Postfix)));
1318+
return Ok(
1319+
self.mk_expr(lo.to(yield_span), ExprKind::Yield(YieldKind::Postfix(self_arg)))
1320+
);
13201321
}
13211322

13221323
let fn_span_lo = self.token.span;
@@ -1893,7 +1894,7 @@ impl<'a> Parser<'a> {
18931894
/// Parse `"yield" expr?`.
18941895
fn parse_expr_yield(&mut self) -> PResult<'a, P<Expr>> {
18951896
let lo = self.prev_token.span;
1896-
let kind = ExprKind::Yield(self.parse_expr_opt()?, YieldKind::Prefix);
1897+
let kind = ExprKind::Yield(YieldKind::Prefix(self.parse_expr_opt()?));
18971898
let span = lo.to(self.prev_token.span);
18981899
self.psess.gated_spans.gate(sym::yield_expr, span);
18991900
let expr = self.mk_expr(span, kind);
@@ -4047,7 +4048,7 @@ impl MutVisitor for CondChecker<'_> {
40474048
| ExprKind::MacCall(_)
40484049
| ExprKind::Struct(_)
40494050
| ExprKind::Repeat(_, _)
4050-
| ExprKind::Yield(_, _)
4051+
| ExprKind::Yield(_)
40514052
| ExprKind::Yeet(_)
40524053
| ExprKind::Become(_)
40534054
| ExprKind::IncludedBytes(_)

src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ fn ident_difference_expr_with_base_location(
528528
&strip_non_ident_wrappers(left).kind,
529529
&strip_non_ident_wrappers(right).kind,
530530
) {
531-
(Yield(_, _), Yield(_, _))
531+
(Yield(_), Yield(_))
532532
| (Try(_), Try(_))
533533
| (Paren(_), Paren(_))
534534
| (Repeat(_, _), Repeat(_, _))

src/tools/clippy/clippy_utils/src/ast_utils/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
201201
(Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lt, rt),
202202
(Block(lb, ll), Block(rb, rl)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lb, rb),
203203
(TryBlock(l), TryBlock(r)) => eq_block(l, r),
204-
(Yield(l, lk), Yield(r, rk)) => eq_expr_opt(l.as_ref(), r.as_ref()) && lk == rk,
204+
(Yield(l), Yield(r)) => eq_expr_opt(l.expr(), r.expr()) && l.same_kind(r),
205205
(Ret(l), Ret(r)) => eq_expr_opt(l.as_ref(), r.as_ref()),
206206
(Break(ll, le), Break(rl, re)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_expr_opt(le.as_ref(), re.as_ref()),
207207
(Continue(ll), Continue(rl)) => eq_label(ll.as_ref(), rl.as_ref()),

src/tools/rustfmt/src/chains.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl ChainItemKind {
259259
let span = mk_sp(nested.span.hi(), expr.span.hi());
260260
(ChainItemKind::Await, span)
261261
}
262-
ast::ExprKind::Yield(Some(ref nested), ast::YieldKind::Postfix) => {
262+
ast::ExprKind::Yield(ast::YieldKind::Postfix(ref nested)) => {
263263
let span = mk_sp(nested.span.hi(), expr.span.hi());
264264
(ChainItemKind::Yield, span)
265265
}
@@ -516,7 +516,7 @@ impl Chain {
516516
ast::ExprKind::Field(ref subexpr, _)
517517
| ast::ExprKind::Try(ref subexpr)
518518
| ast::ExprKind::Await(ref subexpr, _)
519-
| ast::ExprKind::Yield(Some(ref subexpr), ast::YieldKind::Postfix) => Some(SubExpr {
519+
| ast::ExprKind::Yield(ast::YieldKind::Postfix(ref subexpr)) => Some(SubExpr {
520520
expr: Self::convert_try(subexpr, context),
521521
is_method_call_receiver: false,
522522
}),

src/tools/rustfmt/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub(crate) fn format_expr(
221221
Ok(format!("break{id_str}"))
222222
}
223223
}
224-
ast::ExprKind::Yield(ref opt_expr, ast::YieldKind::Prefix) => {
224+
ast::ExprKind::Yield(ast::YieldKind::Prefix(ref opt_expr)) => {
225225
if let Some(ref expr) = *opt_expr {
226226
rewrite_unary_prefix(context, "yield ", &**expr, shape)
227227
} else {
@@ -244,7 +244,7 @@ pub(crate) fn format_expr(
244244
| ast::ExprKind::Field(..)
245245
| ast::ExprKind::MethodCall(..)
246246
| ast::ExprKind::Await(_, _)
247-
| ast::ExprKind::Yield(_, ast::YieldKind::Postfix) => rewrite_chain(expr, context, shape),
247+
| ast::ExprKind::Yield(ast::YieldKind::Postfix(_)) => rewrite_chain(expr, context, shape),
248248
ast::ExprKind::MacCall(ref mac) => {
249249
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|_| {
250250
wrap_str(

src/tools/rustfmt/src/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
485485
| ast::ExprKind::Index(_, ref expr, _)
486486
| ast::ExprKind::Unary(_, ref expr)
487487
| ast::ExprKind::Try(ref expr)
488-
| ast::ExprKind::Yield(Some(ref expr), YieldKind::Prefix) => {
488+
| ast::ExprKind::Yield(YieldKind::Prefix(Some(ref expr))) => {
489489
is_block_expr(context, expr, repr)
490490
}
491491
ast::ExprKind::Closure(ref closure) => is_block_expr(context, &closure.body, repr),
@@ -517,7 +517,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
517517
| ast::ExprKind::Tup(..)
518518
| ast::ExprKind::Use(..)
519519
| ast::ExprKind::Type(..)
520-
| ast::ExprKind::Yield(_, _)
520+
| ast::ExprKind::Yield(..)
521521
| ast::ExprKind::Underscore => false,
522522
}
523523
}

0 commit comments

Comments
 (0)