mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
[#1083] Try block syntax
This commit is contained in:
parent
b79e6294a6
commit
281c9eeaff
9 changed files with 125 additions and 2 deletions
|
@ -6,7 +6,7 @@ use rustc_hash::FxHashMap;
|
|||
use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
|
||||
use ra_syntax::{
|
||||
SyntaxNodePtr, AstPtr, AstNode,
|
||||
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner},
|
||||
ast::{self, TryBlockBodyOwner, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -216,6 +216,9 @@ pub enum Expr {
|
|||
Try {
|
||||
expr: ExprId,
|
||||
},
|
||||
TryBlock {
|
||||
body: ExprId,
|
||||
},
|
||||
Cast {
|
||||
expr: ExprId,
|
||||
type_ref: TypeRef,
|
||||
|
@ -299,6 +302,7 @@ impl Expr {
|
|||
f(*expr);
|
||||
}
|
||||
}
|
||||
Expr::TryBlock { body } => f(*body),
|
||||
Expr::Loop { body } => f(*body),
|
||||
Expr::While { condition, body } => {
|
||||
f(*condition);
|
||||
|
@ -578,6 +582,10 @@ where
|
|||
self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
|
||||
}
|
||||
}
|
||||
ast::ExprKind::TryBlockExpr(e) => {
|
||||
let body = self.collect_block_opt(e.try_body());
|
||||
self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
|
||||
}
|
||||
ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()),
|
||||
ast::ExprKind::LoopExpr(e) => {
|
||||
let body = self.collect_block_opt(e.loop_body());
|
||||
|
|
|
@ -946,6 +946,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||
then_ty
|
||||
}
|
||||
Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected),
|
||||
Expr::TryBlock { body } => {
|
||||
let _inner = self.infer_expr(*body, expected);
|
||||
|
||||
// FIXME should be std::result::Result<{inner}, _>
|
||||
Ty::Unknown
|
||||
}
|
||||
Expr::Loop { body } => {
|
||||
self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
|
||||
// FIXME handle break with value
|
||||
|
|
|
@ -52,6 +52,7 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet =
|
|||
CONTINUE_KW,
|
||||
LIFETIME,
|
||||
ASYNC_KW,
|
||||
TRY_KW,
|
||||
]);
|
||||
|
||||
const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW];
|
||||
|
@ -75,6 +76,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
|
|||
T![loop] => loop_expr(p, None),
|
||||
T![for] => for_expr(p, None),
|
||||
T![while] => while_expr(p, None),
|
||||
T![try] => try_expr(p, None),
|
||||
LIFETIME if la == T![:] => {
|
||||
let m = p.start();
|
||||
label(p);
|
||||
|
@ -116,7 +118,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
|
|||
}
|
||||
};
|
||||
let blocklike = match done.kind() {
|
||||
IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => BlockLike::Block,
|
||||
IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_BLOCK_EXPR => BlockLike::Block,
|
||||
_ => BlockLike::NotBlock,
|
||||
};
|
||||
Some((done, blocklike))
|
||||
|
@ -491,3 +493,17 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker {
|
|||
}
|
||||
m.complete(p, BREAK_EXPR)
|
||||
}
|
||||
|
||||
// test try_expr
|
||||
// fn foo() {
|
||||
// try {
|
||||
//
|
||||
// }
|
||||
// }
|
||||
fn try_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
|
||||
assert!(p.at(T![try]));
|
||||
let m = m.unwrap_or_else(|| p.start());
|
||||
p.bump();
|
||||
block(p);
|
||||
m.complete(p, TRY_EXPR)
|
||||
}
|
||||
|
|
|
@ -103,6 +103,7 @@ pub enum SyntaxKind {
|
|||
LET_KW,
|
||||
MOVE_KW,
|
||||
RETURN_KW,
|
||||
TRY_KW,
|
||||
AUTO_KW,
|
||||
DEFAULT_KW,
|
||||
UNION_KW,
|
||||
|
@ -184,6 +185,7 @@ pub enum SyntaxKind {
|
|||
STRUCT_LIT,
|
||||
NAMED_FIELD_LIST,
|
||||
NAMED_FIELD,
|
||||
TRY_BLOCK_EXPR,
|
||||
CALL_EXPR,
|
||||
INDEX_EXPR,
|
||||
METHOD_CALL_EXPR,
|
||||
|
@ -331,6 +333,7 @@ macro_rules! T {
|
|||
(let) => { $crate::SyntaxKind::LET_KW };
|
||||
(move) => { $crate::SyntaxKind::MOVE_KW };
|
||||
(return) => { $crate::SyntaxKind::RETURN_KW };
|
||||
(try) => { $crate::SyntaxKind::TRY_KW };
|
||||
(auto) => { $crate::SyntaxKind::AUTO_KW };
|
||||
(default) => { $crate::SyntaxKind::DEFAULT_KW };
|
||||
(union) => { $crate::SyntaxKind::UNION_KW };
|
||||
|
@ -388,6 +391,7 @@ impl SyntaxKind {
|
|||
| LET_KW
|
||||
| MOVE_KW
|
||||
| RETURN_KW
|
||||
| TRY_KW
|
||||
| AUTO_KW
|
||||
| DEFAULT_KW
|
||||
| UNION_KW
|
||||
|
@ -559,6 +563,7 @@ impl SyntaxKind {
|
|||
LET_KW => &SyntaxInfo { name: "LET_KW" },
|
||||
MOVE_KW => &SyntaxInfo { name: "MOVE_KW" },
|
||||
RETURN_KW => &SyntaxInfo { name: "RETURN_KW" },
|
||||
TRY_KW => &SyntaxInfo { name: "TRY_KW" },
|
||||
AUTO_KW => &SyntaxInfo { name: "AUTO_KW" },
|
||||
DEFAULT_KW => &SyntaxInfo { name: "DEFAULT_KW" },
|
||||
UNION_KW => &SyntaxInfo { name: "UNION_KW" },
|
||||
|
@ -640,6 +645,7 @@ impl SyntaxKind {
|
|||
STRUCT_LIT => &SyntaxInfo { name: "STRUCT_LIT" },
|
||||
NAMED_FIELD_LIST => &SyntaxInfo { name: "NAMED_FIELD_LIST" },
|
||||
NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
|
||||
TRY_BLOCK_EXPR => &SyntaxInfo { name: "TRY_BLOCK_EXPR" },
|
||||
CALL_EXPR => &SyntaxInfo { name: "CALL_EXPR" },
|
||||
INDEX_EXPR => &SyntaxInfo { name: "INDEX_EXPR" },
|
||||
METHOD_CALL_EXPR => &SyntaxInfo { name: "METHOD_CALL_EXPR" },
|
||||
|
@ -734,6 +740,7 @@ impl SyntaxKind {
|
|||
"let" => LET_KW,
|
||||
"move" => MOVE_KW,
|
||||
"return" => RETURN_KW,
|
||||
"try" => TRY_KW,
|
||||
_ => return None,
|
||||
};
|
||||
Some(kw)
|
||||
|
|
|
@ -713,6 +713,7 @@ pub enum ExprKind<'a> {
|
|||
MethodCallExpr(&'a MethodCallExpr),
|
||||
FieldExpr(&'a FieldExpr),
|
||||
TryExpr(&'a TryExpr),
|
||||
TryBlockExpr(&'a TryBlockExpr),
|
||||
CastExpr(&'a CastExpr),
|
||||
RefExpr(&'a RefExpr),
|
||||
PrefixExpr(&'a PrefixExpr),
|
||||
|
@ -826,6 +827,11 @@ impl<'a> From<&'a TryExpr> for &'a Expr {
|
|||
Expr::cast(&n.syntax).unwrap()
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a TryBlockExpr> for &'a Expr {
|
||||
fn from(n: &'a TryBlockExpr) -> &'a Expr {
|
||||
Expr::cast(&n.syntax).unwrap()
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a CastExpr> for &'a Expr {
|
||||
fn from(n: &'a CastExpr) -> &'a Expr {
|
||||
Expr::cast(&n.syntax).unwrap()
|
||||
|
@ -887,6 +893,7 @@ impl AstNode for Expr {
|
|||
| METHOD_CALL_EXPR
|
||||
| FIELD_EXPR
|
||||
| TRY_EXPR
|
||||
| TRY_BLOCK_EXPR
|
||||
| CAST_EXPR
|
||||
| REF_EXPR
|
||||
| PREFIX_EXPR
|
||||
|
@ -929,6 +936,7 @@ impl Expr {
|
|||
METHOD_CALL_EXPR => ExprKind::MethodCallExpr(MethodCallExpr::cast(&self.syntax).unwrap()),
|
||||
FIELD_EXPR => ExprKind::FieldExpr(FieldExpr::cast(&self.syntax).unwrap()),
|
||||
TRY_EXPR => ExprKind::TryExpr(TryExpr::cast(&self.syntax).unwrap()),
|
||||
TRY_BLOCK_EXPR => ExprKind::TryBlockExpr(TryBlockExpr::cast(&self.syntax).unwrap()),
|
||||
CAST_EXPR => ExprKind::CastExpr(CastExpr::cast(&self.syntax).unwrap()),
|
||||
REF_EXPR => ExprKind::RefExpr(RefExpr::cast(&self.syntax).unwrap()),
|
||||
PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()),
|
||||
|
@ -3672,6 +3680,35 @@ impl TraitDef {
|
|||
}
|
||||
}
|
||||
|
||||
// TryBlockExpr
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
#[repr(transparent)]
|
||||
pub struct TryBlockExpr {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
unsafe impl TransparentNewType for TryBlockExpr {
|
||||
type Repr = rowan::SyntaxNode;
|
||||
}
|
||||
|
||||
impl AstNode for TryBlockExpr {
|
||||
fn cast(syntax: &SyntaxNode) -> Option<&Self> {
|
||||
match syntax.kind() {
|
||||
TRY_BLOCK_EXPR => Some(TryBlockExpr::from_repr(syntax.into_repr())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
|
||||
impl ToOwned for TryBlockExpr {
|
||||
type Owned = TreeArc<TryBlockExpr>;
|
||||
fn to_owned(&self) -> TreeArc<TryBlockExpr> { TreeArc::cast(self.syntax.to_owned()) }
|
||||
}
|
||||
|
||||
|
||||
impl ast::TryBlockBodyOwner for TryBlockExpr {}
|
||||
impl TryBlockExpr {}
|
||||
|
||||
// TryExpr
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
#[repr(transparent)]
|
||||
|
|
|
@ -33,6 +33,12 @@ pub trait LoopBodyOwner: AstNode {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait TryBlockBodyOwner: AstNode {
|
||||
fn try_body(&self) -> Option<&ast::Block> {
|
||||
child_opt(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ArgListOwner: AstNode {
|
||||
fn arg_list(&self) -> Option<&ast::ArgList> {
|
||||
child_opt(self)
|
||||
|
|
|
@ -95,6 +95,7 @@ Grammar(
|
|||
"let",
|
||||
"move",
|
||||
"return",
|
||||
"try",
|
||||
],
|
||||
contextual_keywords: [
|
||||
"auto",
|
||||
|
@ -189,6 +190,7 @@ Grammar(
|
|||
"STRUCT_LIT",
|
||||
"NAMED_FIELD_LIST",
|
||||
"NAMED_FIELD",
|
||||
"TRY_BLOCK_EXPR",
|
||||
|
||||
// postfix
|
||||
"CALL_EXPR",
|
||||
|
@ -417,6 +419,9 @@ Grammar(
|
|||
"LoopExpr": (
|
||||
traits: ["LoopBodyOwner"],
|
||||
),
|
||||
"TryBlockExpr": (
|
||||
traits: ["TryBlockBodyOwner"],
|
||||
),
|
||||
"ForExpr": (
|
||||
traits: ["LoopBodyOwner"],
|
||||
options: [
|
||||
|
@ -499,6 +504,7 @@ Grammar(
|
|||
"MethodCallExpr",
|
||||
"FieldExpr",
|
||||
"TryExpr",
|
||||
"TryBlockExpr",
|
||||
"CastExpr",
|
||||
"RefExpr",
|
||||
"PrefixExpr",
|
||||
|
|
5
crates/ra_syntax/tests/data/parser/ok/0051_try_block.rs
Normal file
5
crates/ra_syntax/tests/data/parser/ok/0051_try_block.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
fn main() {
|
||||
let res = try {
|
||||
|
||||
};
|
||||
}
|
32
crates/ra_syntax/tests/data/parser/ok/0051_try_block.txt
Normal file
32
crates/ra_syntax/tests/data/parser/ok/0051_try_block.txt
Normal file
|
@ -0,0 +1,32 @@
|
|||
SOURCE_FILE@[0; 41)
|
||||
FN_DEF@[0; 41)
|
||||
FN_KW@[0; 2) "fn"
|
||||
WHITESPACE@[2; 3) " "
|
||||
NAME@[3; 7)
|
||||
IDENT@[3; 7) "main"
|
||||
PARAM_LIST@[7; 9)
|
||||
L_PAREN@[7; 8) "("
|
||||
R_PAREN@[8; 9) ")"
|
||||
WHITESPACE@[9; 10) " "
|
||||
BLOCK@[10; 41)
|
||||
L_CURLY@[10; 11) "{"
|
||||
WHITESPACE@[11; 16) "\n "
|
||||
LET_STMT@[16; 39)
|
||||
LET_KW@[16; 19) "let"
|
||||
WHITESPACE@[19; 20) " "
|
||||
BIND_PAT@[20; 23)
|
||||
NAME@[20; 23)
|
||||
IDENT@[20; 23) "res"
|
||||
WHITESPACE@[23; 24) " "
|
||||
EQ@[24; 25) "="
|
||||
WHITESPACE@[25; 26) " "
|
||||
TRY_EXPR@[26; 38)
|
||||
TRY_KW@[26; 29) "try"
|
||||
WHITESPACE@[29; 30) " "
|
||||
BLOCK@[30; 38)
|
||||
L_CURLY@[30; 31) "{"
|
||||
WHITESPACE@[31; 37) "\n\n "
|
||||
R_CURLY@[37; 38) "}"
|
||||
SEMI@[38; 39) ";"
|
||||
WHITESPACE@[39; 40) "\n"
|
||||
R_CURLY@[40; 41) "}"
|
Loading…
Add table
Add a link
Reference in a new issue