mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
Auto merge of #15003 - WaffleLapkin:become_unuwuable, r=Veykril
feature: Add basic support for `become` expr/tail calls This follows https://github.com/rust-lang/rfcs/pull/3407 and my WIP implementation in the compiler. Notice that I haven't even *opened* a compiler PR (although I plan to soon), so this feature doesn't really exist outside of my WIP branches. I've used this to help me test my implementation; opening a PR before I forget. (feel free to ignore this for now, given all of the above)
This commit is contained in:
commit
dba59970bc
16 changed files with 144 additions and 6 deletions
|
@ -416,6 +416,11 @@ impl ExprCollector<'_> {
|
|||
let expr = e.expr().map(|e| self.collect_expr(e));
|
||||
self.alloc_expr(Expr::Return { expr }, syntax_ptr)
|
||||
}
|
||||
ast::Expr::BecomeExpr(e) => {
|
||||
let expr =
|
||||
e.expr().map(|e| self.collect_expr(e)).unwrap_or_else(|| self.missing_expr());
|
||||
self.alloc_expr(Expr::Become { expr }, syntax_ptr)
|
||||
}
|
||||
ast::Expr::YieldExpr(e) => {
|
||||
self.is_lowering_coroutine = true;
|
||||
let expr = e.expr().map(|e| self.collect_expr(e));
|
||||
|
|
|
@ -261,6 +261,11 @@ impl Printer<'_> {
|
|||
self.print_expr(*expr);
|
||||
}
|
||||
}
|
||||
Expr::Become { expr } => {
|
||||
w!(self, "become");
|
||||
self.whitespace();
|
||||
self.print_expr(*expr);
|
||||
}
|
||||
Expr::Yield { expr } => {
|
||||
w!(self, "yield");
|
||||
if let Some(expr) = expr {
|
||||
|
|
|
@ -216,6 +216,9 @@ pub enum Expr {
|
|||
Return {
|
||||
expr: Option<ExprId>,
|
||||
},
|
||||
Become {
|
||||
expr: ExprId,
|
||||
},
|
||||
Yield {
|
||||
expr: Option<ExprId>,
|
||||
},
|
||||
|
@ -410,6 +413,7 @@ impl Expr {
|
|||
f(expr);
|
||||
}
|
||||
}
|
||||
Expr::Become { expr } => f(*expr),
|
||||
Expr::RecordLit { fields, spread, .. } => {
|
||||
for field in fields.iter() {
|
||||
f(field.expr);
|
||||
|
|
|
@ -531,6 +531,9 @@ impl InferenceContext<'_> {
|
|||
self.consume_expr(expr);
|
||||
}
|
||||
}
|
||||
&Expr::Become { expr } => {
|
||||
self.consume_expr(expr);
|
||||
}
|
||||
Expr::RecordLit { fields, spread, .. } => {
|
||||
if let &Some(expr) = spread {
|
||||
self.consume_expr(expr);
|
||||
|
|
|
@ -502,6 +502,7 @@ impl InferenceContext<'_> {
|
|||
self.result.standard_types.never.clone()
|
||||
}
|
||||
&Expr::Return { expr } => self.infer_expr_return(tgt_expr, expr),
|
||||
&Expr::Become { expr } => self.infer_expr_become(expr),
|
||||
Expr::Yield { expr } => {
|
||||
if let Some((resume_ty, yield_ty)) = self.resume_yield_tys.clone() {
|
||||
if let Some(expr) = expr {
|
||||
|
@ -1084,6 +1085,27 @@ impl InferenceContext<'_> {
|
|||
self.result.standard_types.never.clone()
|
||||
}
|
||||
|
||||
fn infer_expr_become(&mut self, expr: ExprId) -> Ty {
|
||||
match &self.return_coercion {
|
||||
Some(return_coercion) => {
|
||||
let ret_ty = return_coercion.expected_ty();
|
||||
|
||||
let call_expr_ty =
|
||||
self.infer_expr_inner(expr, &Expectation::HasType(ret_ty.clone()));
|
||||
|
||||
// NB: this should *not* coerce.
|
||||
// tail calls don't support any coercions except lifetimes ones (like `&'static u8 -> &'a u8`).
|
||||
self.unify(&call_expr_ty, &ret_ty);
|
||||
}
|
||||
None => {
|
||||
// FIXME: diagnose `become` outside of functions
|
||||
self.infer_expr_no_expect(expr);
|
||||
}
|
||||
}
|
||||
|
||||
self.result.standard_types.never.clone()
|
||||
}
|
||||
|
||||
fn infer_expr_box(&mut self, inner_expr: ExprId, expected: &Expectation) -> Ty {
|
||||
if let Some(box_id) = self.resolve_boxed_box() {
|
||||
let table = &mut self.table;
|
||||
|
|
|
@ -93,6 +93,9 @@ impl InferenceContext<'_> {
|
|||
self.infer_mut_expr(expr, Mutability::Not);
|
||||
}
|
||||
}
|
||||
Expr::Become { expr } => {
|
||||
self.infer_mut_expr(*expr, Mutability::Not);
|
||||
}
|
||||
Expr::RecordLit { path: _, fields, spread, ellipsis: _, is_assignee_expr: _ } => {
|
||||
self.infer_mut_not_expr_iter(fields.iter().map(|it| it.expr).chain(*spread))
|
||||
}
|
||||
|
|
|
@ -775,6 +775,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
|
|||
self.set_terminator(current, TerminatorKind::Return, expr_id.into());
|
||||
Ok(None)
|
||||
}
|
||||
Expr::Become { .. } => not_supported!("tail-calls"),
|
||||
Expr::Yield { .. } => not_supported!("yield"),
|
||||
Expr::RecordLit { fields, path, spread, ellipsis: _, is_assignee_expr: _ } => {
|
||||
let spread_place = match spread {
|
||||
|
|
|
@ -329,6 +329,7 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
|
|||
| ast::Expr::RecordExpr(_)
|
||||
| ast::Expr::RefExpr(_)
|
||||
| ast::Expr::ReturnExpr(_)
|
||||
| ast::Expr::BecomeExpr(_)
|
||||
| ast::Expr::TryExpr(_)
|
||||
| ast::Expr::TupleExpr(_)
|
||||
| ast::Expr::LetExpr(_)
|
||||
|
|
|
@ -58,6 +58,7 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet =
|
|||
T![match],
|
||||
T![move],
|
||||
T![return],
|
||||
T![become],
|
||||
T![static],
|
||||
T![try],
|
||||
T![unsafe],
|
||||
|
@ -102,6 +103,7 @@ pub(super) fn atom_expr(
|
|||
T![try] => try_block_expr(p, None),
|
||||
T![match] => match_expr(p),
|
||||
T![return] => return_expr(p),
|
||||
T![become] => become_expr(p),
|
||||
T![yield] => yield_expr(p),
|
||||
T![do] if p.nth_at_contextual_kw(1, T![yeet]) => yeet_expr(p),
|
||||
T![continue] => continue_expr(p),
|
||||
|
@ -621,6 +623,18 @@ fn return_expr(p: &mut Parser<'_>) -> CompletedMarker {
|
|||
m.complete(p, RETURN_EXPR)
|
||||
}
|
||||
|
||||
// test become_expr
|
||||
// fn foo() {
|
||||
// become foo();
|
||||
// }
|
||||
fn become_expr(p: &mut Parser<'_>) -> CompletedMarker {
|
||||
assert!(p.at(T![become]));
|
||||
let m = p.start();
|
||||
p.bump(T![become]);
|
||||
expr(p);
|
||||
m.complete(p, BECOME_EXPR)
|
||||
}
|
||||
|
||||
// test yield_expr
|
||||
// fn foo() {
|
||||
// yield;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,31 @@
|
|||
SOURCE_FILE
|
||||
FN
|
||||
FN_KW "fn"
|
||||
WHITESPACE " "
|
||||
NAME
|
||||
IDENT "foo"
|
||||
PARAM_LIST
|
||||
L_PAREN "("
|
||||
R_PAREN ")"
|
||||
WHITESPACE " "
|
||||
BLOCK_EXPR
|
||||
STMT_LIST
|
||||
L_CURLY "{"
|
||||
WHITESPACE "\n "
|
||||
EXPR_STMT
|
||||
BECOME_EXPR
|
||||
BECOME_KW "become"
|
||||
WHITESPACE " "
|
||||
CALL_EXPR
|
||||
PATH_EXPR
|
||||
PATH
|
||||
PATH_SEGMENT
|
||||
NAME_REF
|
||||
IDENT "foo"
|
||||
ARG_LIST
|
||||
L_PAREN "("
|
||||
R_PAREN ")"
|
||||
SEMICOLON ";"
|
||||
WHITESPACE "\n"
|
||||
R_CURLY "}"
|
||||
WHITESPACE "\n"
|
|
@ -0,0 +1,3 @@
|
|||
fn foo() {
|
||||
become foo();
|
||||
}
|
|
@ -367,6 +367,7 @@ Expr =
|
|||
| RecordExpr
|
||||
| RefExpr
|
||||
| ReturnExpr
|
||||
| BecomeExpr
|
||||
| TryExpr
|
||||
| TupleExpr
|
||||
| WhileExpr
|
||||
|
@ -528,6 +529,9 @@ MatchGuard =
|
|||
ReturnExpr =
|
||||
Attr* 'return' Expr?
|
||||
|
||||
BecomeExpr =
|
||||
Attr* 'become' Expr
|
||||
|
||||
YieldExpr =
|
||||
Attr* 'yield' Expr?
|
||||
|
||||
|
|
|
@ -1095,6 +1095,16 @@ impl ReturnExpr {
|
|||
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct BecomeExpr {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
impl ast::HasAttrs for BecomeExpr {}
|
||||
impl BecomeExpr {
|
||||
pub fn become_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![become]) }
|
||||
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct TryExpr {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
|
@ -1633,6 +1643,7 @@ pub enum Expr {
|
|||
RecordExpr(RecordExpr),
|
||||
RefExpr(RefExpr),
|
||||
ReturnExpr(ReturnExpr),
|
||||
BecomeExpr(BecomeExpr),
|
||||
TryExpr(TryExpr),
|
||||
TupleExpr(TupleExpr),
|
||||
WhileExpr(WhileExpr),
|
||||
|
@ -2792,6 +2803,17 @@ impl AstNode for ReturnExpr {
|
|||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl AstNode for BecomeExpr {
|
||||
fn can_cast(kind: SyntaxKind) -> bool { kind == BECOME_EXPR }
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
if Self::can_cast(syntax.kind()) {
|
||||
Some(Self { syntax })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl AstNode for TryExpr {
|
||||
fn can_cast(kind: SyntaxKind) -> bool { kind == TRY_EXPR }
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
|
@ -3540,6 +3562,9 @@ impl From<RefExpr> for Expr {
|
|||
impl From<ReturnExpr> for Expr {
|
||||
fn from(node: ReturnExpr) -> Expr { Expr::ReturnExpr(node) }
|
||||
}
|
||||
impl From<BecomeExpr> for Expr {
|
||||
fn from(node: BecomeExpr) -> Expr { Expr::BecomeExpr(node) }
|
||||
}
|
||||
impl From<TryExpr> for Expr {
|
||||
fn from(node: TryExpr) -> Expr { Expr::TryExpr(node) }
|
||||
}
|
||||
|
@ -3593,6 +3618,7 @@ impl AstNode for Expr {
|
|||
| RECORD_EXPR
|
||||
| REF_EXPR
|
||||
| RETURN_EXPR
|
||||
| BECOME_EXPR
|
||||
| TRY_EXPR
|
||||
| TUPLE_EXPR
|
||||
| WHILE_EXPR
|
||||
|
@ -3632,6 +3658,7 @@ impl AstNode for Expr {
|
|||
RECORD_EXPR => Expr::RecordExpr(RecordExpr { syntax }),
|
||||
REF_EXPR => Expr::RefExpr(RefExpr { syntax }),
|
||||
RETURN_EXPR => Expr::ReturnExpr(ReturnExpr { syntax }),
|
||||
BECOME_EXPR => Expr::BecomeExpr(BecomeExpr { syntax }),
|
||||
TRY_EXPR => Expr::TryExpr(TryExpr { syntax }),
|
||||
TUPLE_EXPR => Expr::TupleExpr(TupleExpr { syntax }),
|
||||
WHILE_EXPR => Expr::WhileExpr(WhileExpr { syntax }),
|
||||
|
@ -3673,6 +3700,7 @@ impl AstNode for Expr {
|
|||
Expr::RecordExpr(it) => &it.syntax,
|
||||
Expr::RefExpr(it) => &it.syntax,
|
||||
Expr::ReturnExpr(it) => &it.syntax,
|
||||
Expr::BecomeExpr(it) => &it.syntax,
|
||||
Expr::TryExpr(it) => &it.syntax,
|
||||
Expr::TupleExpr(it) => &it.syntax,
|
||||
Expr::WhileExpr(it) => &it.syntax,
|
||||
|
@ -4150,6 +4178,7 @@ impl AstNode for AnyHasAttrs {
|
|||
| RANGE_EXPR
|
||||
| REF_EXPR
|
||||
| RETURN_EXPR
|
||||
| BECOME_EXPR
|
||||
| TRY_EXPR
|
||||
| TUPLE_EXPR
|
||||
| WHILE_EXPR
|
||||
|
@ -4851,6 +4880,11 @@ impl std::fmt::Display for ReturnExpr {
|
|||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
}
|
||||
}
|
||||
impl std::fmt::Display for BecomeExpr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
}
|
||||
}
|
||||
impl std::fmt::Display for TryExpr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
|
|
|
@ -130,8 +130,8 @@ impl Expr {
|
|||
//
|
||||
ContinueExpr(_) => (0, 0),
|
||||
|
||||
ClosureExpr(_) | ReturnExpr(_) | YieldExpr(_) | YeetExpr(_) | BreakExpr(_)
|
||||
| OffsetOfExpr(_) | FormatArgsExpr(_) | AsmExpr(_) => (0, 1),
|
||||
ClosureExpr(_) | ReturnExpr(_) | BecomeExpr(_) | YieldExpr(_) | YeetExpr(_)
|
||||
| BreakExpr(_) | OffsetOfExpr(_) | FormatArgsExpr(_) | AsmExpr(_) => (0, 1),
|
||||
|
||||
RangeExpr(_) => (5, 5),
|
||||
|
||||
|
@ -288,6 +288,7 @@ impl Expr {
|
|||
PrefixExpr(e) => e.op_token(),
|
||||
RefExpr(e) => e.amp_token(),
|
||||
ReturnExpr(e) => e.return_token(),
|
||||
BecomeExpr(e) => e.become_token(),
|
||||
TryExpr(e) => e.question_mark_token(),
|
||||
YieldExpr(e) => e.yield_token(),
|
||||
YeetExpr(e) => e.do_token(),
|
||||
|
@ -316,7 +317,8 @@ impl Expr {
|
|||
|
||||
// For BinExpr and RangeExpr this is technically wrong -- the child can be on the left...
|
||||
BinExpr(_) | RangeExpr(_) | BreakExpr(_) | ContinueExpr(_) | PrefixExpr(_)
|
||||
| RefExpr(_) | ReturnExpr(_) | YieldExpr(_) | YeetExpr(_) | LetExpr(_) => self
|
||||
| RefExpr(_) | ReturnExpr(_) | BecomeExpr(_) | YieldExpr(_) | YeetExpr(_)
|
||||
| LetExpr(_) => self
|
||||
.syntax()
|
||||
.parent()
|
||||
.and_then(Expr::cast)
|
||||
|
|
|
@ -67,8 +67,9 @@ pub(crate) const KINDS_SRC: KindsSrc<'_> = KindsSrc {
|
|||
keywords: &[
|
||||
"as", "async", "await", "box", "break", "const", "continue", "crate", "do", "dyn", "else",
|
||||
"enum", "extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "macro",
|
||||
"match", "mod", "move", "mut", "pub", "ref", "return", "self", "Self", "static", "struct",
|
||||
"super", "trait", "true", "try", "type", "unsafe", "use", "where", "while", "yield",
|
||||
"match", "mod", "move", "mut", "pub", "ref", "return", "become", "self", "Self", "static",
|
||||
"struct", "super", "trait", "true", "try", "type", "unsafe", "use", "where", "while",
|
||||
"yield",
|
||||
],
|
||||
contextual_keywords: &[
|
||||
"auto",
|
||||
|
@ -154,6 +155,7 @@ pub(crate) const KINDS_SRC: KindsSrc<'_> = KindsSrc {
|
|||
"BLOCK_EXPR",
|
||||
"STMT_LIST",
|
||||
"RETURN_EXPR",
|
||||
"BECOME_EXPR",
|
||||
"YIELD_EXPR",
|
||||
"YEET_EXPR",
|
||||
"LET_EXPR",
|
||||
|
|
Loading…
Reference in a new issue