Syntatically accept become
expressions
This commit is contained in:
parent
8d1fa473dd
commit
d7713feb99
@ -1295,6 +1295,7 @@ impl Expr {
|
||||
ExprKind::Yield(..) => ExprPrecedence::Yield,
|
||||
ExprKind::Yeet(..) => ExprPrecedence::Yeet,
|
||||
ExprKind::FormatArgs(..) => ExprPrecedence::FormatArgs,
|
||||
ExprKind::Become(..) => ExprPrecedence::Become,
|
||||
ExprKind::Err => ExprPrecedence::Err,
|
||||
}
|
||||
}
|
||||
@ -1515,6 +1516,11 @@ pub enum ExprKind {
|
||||
/// with an optional value to be returned.
|
||||
Yeet(Option<P<Expr>>),
|
||||
|
||||
/// A tail call return, with the value to be returned.
|
||||
///
|
||||
/// While `.0` must be a function call, we check this later, after parsing.
|
||||
Become(P<Expr>),
|
||||
|
||||
/// Bytes included via `include_bytes!`
|
||||
/// Added for optimization purposes to avoid the need to escape
|
||||
/// large binary blobs - should always behave like [`ExprKind::Lit`]
|
||||
|
@ -1457,6 +1457,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
|
||||
ExprKind::Yeet(expr) => {
|
||||
visit_opt(expr, |expr| vis.visit_expr(expr));
|
||||
}
|
||||
ExprKind::Become(expr) => vis.visit_expr(expr),
|
||||
ExprKind::InlineAsm(asm) => vis.visit_inline_asm(asm),
|
||||
ExprKind::FormatArgs(fmt) => vis.visit_format_args(fmt),
|
||||
ExprKind::OffsetOf(container, fields) => {
|
||||
|
@ -245,6 +245,7 @@ pub enum ExprPrecedence {
|
||||
Ret,
|
||||
Yield,
|
||||
Yeet,
|
||||
Become,
|
||||
|
||||
Range,
|
||||
|
||||
@ -298,7 +299,8 @@ impl ExprPrecedence {
|
||||
| ExprPrecedence::Continue
|
||||
| ExprPrecedence::Ret
|
||||
| ExprPrecedence::Yield
|
||||
| ExprPrecedence::Yeet => PREC_JUMP,
|
||||
| ExprPrecedence::Yeet
|
||||
| ExprPrecedence::Become => PREC_JUMP,
|
||||
|
||||
// `Range` claims to have higher precedence than `Assign`, but `x .. x = x` fails to
|
||||
// parse, instead of parsing as `(x .. x) = x`. Giving `Range` a lower precedence
|
||||
|
@ -908,6 +908,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
|
||||
ExprKind::Yeet(optional_expression) => {
|
||||
walk_list!(visitor, visit_expr, optional_expression);
|
||||
}
|
||||
ExprKind::Become(expr) => visitor.visit_expr(expr),
|
||||
ExprKind::MacCall(mac) => visitor.visit_mac_call(mac),
|
||||
ExprKind::Paren(subexpression) => visitor.visit_expr(subexpression),
|
||||
ExprKind::InlineAsm(asm) => visitor.visit_inline_asm(asm),
|
||||
|
@ -275,6 +275,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
hir::ExprKind::Ret(e)
|
||||
}
|
||||
ExprKind::Yeet(sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref()),
|
||||
ExprKind::Become(sub_expr) => {
|
||||
let sub_expr = self.lower_expr(sub_expr);
|
||||
|
||||
// FIXME(explicit_tail_calls): Use `hir::ExprKind::Become` once we implemented it
|
||||
hir::ExprKind::Ret(Some(sub_expr))
|
||||
}
|
||||
ExprKind::InlineAsm(asm) => {
|
||||
hir::ExprKind::InlineAsm(self.lower_inline_asm(e.span, asm))
|
||||
}
|
||||
|
@ -555,6 +555,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
|
||||
gate_all!(dyn_star, "`dyn*` trait objects are experimental");
|
||||
gate_all!(const_closures, "const closures are experimental");
|
||||
gate_all!(builtin_syntax, "`builtin #` syntax is unstable");
|
||||
gate_all!(explicit_tail_calls, "`become` expression is experimental");
|
||||
|
||||
if !visitor.features.negative_bounds {
|
||||
for &span in spans.get(&sym::negative_bounds).iter().copied().flatten() {
|
||||
|
@ -537,6 +537,11 @@ impl<'a> State<'a> {
|
||||
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
|
||||
}
|
||||
}
|
||||
ast::ExprKind::Become(result) => {
|
||||
self.word("become");
|
||||
self.word(" ");
|
||||
self.print_expr_maybe_paren(result, parser::PREC_JUMP);
|
||||
}
|
||||
ast::ExprKind::InlineAsm(a) => {
|
||||
// FIXME: This should have its own syntax, distinct from a macro invocation.
|
||||
self.word("asm!");
|
||||
|
@ -320,6 +320,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
|
||||
| ExprKind::Underscore
|
||||
| ExprKind::While(_, _, _)
|
||||
| ExprKind::Yeet(_)
|
||||
| ExprKind::Become(_)
|
||||
| ExprKind::Yield(_) => {}
|
||||
}
|
||||
}
|
||||
|
@ -395,6 +395,8 @@ declare_features! (
|
||||
(active, exclusive_range_pattern, "1.11.0", Some(37854), None),
|
||||
/// Allows exhaustive pattern matching on types that contain uninhabited types.
|
||||
(active, exhaustive_patterns, "1.13.0", Some(51085), None),
|
||||
/// Allows explicit tail calls via `become` expression.
|
||||
(incomplete, explicit_tail_calls, "CURRENT_RUSTC_VERSION", Some(112788), None),
|
||||
/// Allows using `efiapi`, `sysv64` and `win64` as calling convention
|
||||
/// for functions with varargs.
|
||||
(active, extended_varargs_abi_support, "1.65.0", Some(100189), None),
|
||||
|
@ -1430,6 +1430,8 @@ impl<'a> Parser<'a> {
|
||||
self.parse_expr_yield()
|
||||
} else if self.is_do_yeet() {
|
||||
self.parse_expr_yeet()
|
||||
} else if self.eat_keyword(kw::Become) {
|
||||
self.parse_expr_become()
|
||||
} else if self.check_keyword(kw::Let) {
|
||||
self.parse_expr_let()
|
||||
} else if self.eat_keyword(kw::Underscore) {
|
||||
@ -1746,6 +1748,16 @@ impl<'a> Parser<'a> {
|
||||
self.maybe_recover_from_bad_qpath(expr)
|
||||
}
|
||||
|
||||
/// Parse `"become" expr`, with `"become"` token already eaten.
|
||||
fn parse_expr_become(&mut self) -> PResult<'a, P<Expr>> {
|
||||
let lo = self.prev_token.span;
|
||||
let kind = ExprKind::Become(self.parse_expr()?);
|
||||
let span = lo.to(self.prev_token.span);
|
||||
self.sess.gated_spans.gate(sym::explicit_tail_calls, span);
|
||||
let expr = self.mk_expr(span, kind);
|
||||
self.maybe_recover_from_bad_qpath(expr)
|
||||
}
|
||||
|
||||
/// Parse `"break" (('label (:? expr)?) | expr?)` with `"break"` token already eaten.
|
||||
/// If the label is followed immediately by a `:` token, the label and `:` are
|
||||
/// parsed as part of the expression (i.e. a labeled loop). The language team has
|
||||
|
@ -569,7 +569,8 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
|
||||
Array, ConstBlock, Call, MethodCall, Tup, Binary, Unary, Lit, Cast, Type, Let,
|
||||
If, While, ForLoop, Loop, Match, Closure, Block, Async, Await, TryBlock, Assign,
|
||||
AssignOp, Field, Index, Range, Underscore, Path, AddrOf, Break, Continue, Ret,
|
||||
InlineAsm, FormatArgs, OffsetOf, MacCall, Struct, Repeat, Paren, Try, Yield, Yeet, IncludedBytes, Err
|
||||
InlineAsm, FormatArgs, OffsetOf, MacCall, Struct, Repeat, Paren, Try, Yield, Yeet,
|
||||
Become, IncludedBytes, Err
|
||||
]
|
||||
);
|
||||
ast_visit::walk_expr(self, e)
|
||||
|
@ -686,6 +686,7 @@ symbols! {
|
||||
expf32,
|
||||
expf64,
|
||||
explicit_generic_args_with_impl_trait,
|
||||
explicit_tail_calls,
|
||||
export_name,
|
||||
expr,
|
||||
extended_key_value_attributes,
|
||||
|
@ -0,0 +1,9 @@
|
||||
pub fn you<T>() -> T {
|
||||
become bottom(); //~ error: `become` expression is experimental
|
||||
}
|
||||
|
||||
pub fn bottom<T>() -> T {
|
||||
become you(); //~ error: `become` expression is experimental
|
||||
}
|
||||
|
||||
fn main() {}
|
@ -0,0 +1,21 @@
|
||||
error[E0658]: `become` expression is experimental
|
||||
--> $DIR/feature-gate-explicit_tail_calls.rs:2:5
|
||||
|
|
||||
LL | become bottom();
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: see issue #112788 <https://github.com/rust-lang/rust/issues/112788> for more information
|
||||
= help: add `#![feature(explicit_tail_calls)]` to the crate attributes to enable
|
||||
|
||||
error[E0658]: `become` expression is experimental
|
||||
--> $DIR/feature-gate-explicit_tail_calls.rs:6:5
|
||||
|
|
||||
LL | become you();
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= note: see issue #112788 <https://github.com/rust-lang/rust/issues/112788> for more information
|
||||
= help: add `#![feature(explicit_tail_calls)]` to the crate attributes to enable
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0658`.
|
Loading…
x
Reference in New Issue
Block a user