From 1cd78a3355ea70d3070cabb00c80a5d195499752 Mon Sep 17 00:00:00 2001 From: robojumper Date: Sun, 31 May 2020 10:59:40 +0200 Subject: correctly infer labelled breaks --- crates/ra_hir_def/src/body/lower.rs | 56 +++++++++++++++++++++++++++++-------- crates/ra_hir_def/src/body/scope.rs | 4 +-- crates/ra_hir_def/src/expr.rs | 19 +++++++++---- 3 files changed, 60 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 905c0cf5d..dc52c6bd9 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -134,7 +134,7 @@ impl ExprCollector<'_> { self.make_expr(expr, Err(SyntheticSyntax)) } fn empty_block(&mut self) -> ExprId { - self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None }) + self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None, label: None }) } fn missing_expr(&mut self) -> ExprId { self.alloc_expr_desugared(Expr::Missing) @@ -215,7 +215,13 @@ impl ExprCollector<'_> { ast::Expr::BlockExpr(e) => self.collect_block(e), ast::Expr::LoopExpr(e) => { let body = self.collect_block_opt(e.loop_body()); - self.alloc_expr(Expr::Loop { body }, syntax_ptr) + self.alloc_expr( + Expr::Loop { + body, + label: e.label().and_then(|l| l.lifetime_token()).map(|l| Name::new_lifetime(&l)), + }, + syntax_ptr, + ) } ast::Expr::WhileExpr(e) => { let body = self.collect_block_opt(e.loop_body()); @@ -230,25 +236,47 @@ impl ExprCollector<'_> { let pat = self.collect_pat(pat); let match_expr = self.collect_expr_opt(condition.expr()); let placeholder_pat = self.missing_pat(); - let break_ = self.alloc_expr_desugared(Expr::Break { expr: None }); + let break_ = + self.alloc_expr_desugared(Expr::Break { expr: None, label: None }); let arms = vec![ MatchArm { pat, expr: body, guard: None }, MatchArm { pat: placeholder_pat, expr: break_, guard: None }, ]; let match_expr = self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms }); - return self.alloc_expr(Expr::Loop { body: match_expr }, syntax_ptr); + return self.alloc_expr( + Expr::Loop { + body: match_expr, + label: e.label().and_then(|l| l.lifetime_token()).map(|l| Name::new_lifetime(&l)), + }, + syntax_ptr, + ); } }, }; - self.alloc_expr(Expr::While { condition, body }, syntax_ptr) + self.alloc_expr( + Expr::While { + condition, + body, + label: e.label().and_then(|l| l.lifetime_token()).map(|l| Name::new_lifetime(&l)), + }, + syntax_ptr, + ) } ast::Expr::ForExpr(e) => { let iterable = self.collect_expr_opt(e.iterable()); let pat = self.collect_pat_opt(e.pat()); let body = self.collect_block_opt(e.loop_body()); - self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr) + self.alloc_expr( + Expr::For { + iterable, + pat, + body, + label: e.label().and_then(|l| l.lifetime_token()).map(|l| Name::new_lifetime(&l)), + }, + syntax_ptr, + ) } ast::Expr::CallExpr(e) => { let callee = self.collect_expr_opt(e.expr()); @@ -301,13 +329,18 @@ impl ExprCollector<'_> { .unwrap_or(Expr::Missing); self.alloc_expr(path, syntax_ptr) } - ast::Expr::ContinueExpr(_e) => { - // FIXME: labels - self.alloc_expr(Expr::Continue, syntax_ptr) + ast::Expr::ContinueExpr(e) => { + self.alloc_expr( + Expr::Continue { label: e.lifetime_token().map(|l| Name::new_lifetime(&l)) }, + syntax_ptr, + ) } ast::Expr::BreakExpr(e) => { let expr = e.expr().map(|e| self.collect_expr(e)); - self.alloc_expr(Expr::Break { expr }, syntax_ptr) + self.alloc_expr( + Expr::Break { expr, label: e.lifetime_token().map(|l| Name::new_lifetime(&l)) }, + syntax_ptr, + ) } ast::Expr::ParenExpr(e) => { let inner = self.collect_expr_opt(e.expr()); @@ -529,7 +562,8 @@ impl ExprCollector<'_> { }) .collect(); let tail = block.expr().map(|e| self.collect_expr(e)); - self.alloc_expr(Expr::Block { statements, tail }, syntax_node_ptr) + let label = block.label().and_then(|l| l.lifetime_token()).map(|t| Name::new_lifetime(&t)); + self.alloc_expr(Expr::Block { statements, tail, label }, syntax_node_ptr) } fn collect_block_items(&mut self, block: &ast::BlockExpr) { diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index 09e92b74e..e48ff38f9 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -138,10 +138,10 @@ fn compute_block_scopes( fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) { scopes.set_scope(expr, scope); match &body[expr] { - Expr::Block { statements, tail } => { + Expr::Block { statements, tail, .. } => { compute_block_scopes(&statements, *tail, body, scopes, scope); } - Expr::For { iterable, pat, body: body_expr } => { + Expr::For { iterable, pat, body: body_expr, .. } => { compute_expr_scopes(*iterable, body, scopes, scope); let scope = scopes.new_scope(scope); scopes.add_bindings(body, scope, *pat); diff --git a/crates/ra_hir_def/src/expr.rs b/crates/ra_hir_def/src/expr.rs index f25c6f958..8683f6c7f 100644 --- a/crates/ra_hir_def/src/expr.rs +++ b/crates/ra_hir_def/src/expr.rs @@ -52,18 +52,22 @@ pub enum Expr { Block { statements: Vec, tail: Option, + label: Option, }, Loop { body: ExprId, + label: Option, }, While { condition: ExprId, body: ExprId, + label: Option, }, For { iterable: ExprId, pat: PatId, body: ExprId, + label: Option, }, Call { callee: ExprId, @@ -79,9 +83,12 @@ pub enum Expr { expr: ExprId, arms: Vec, }, - Continue, + Continue { + label: Option, + }, Break { expr: Option, + label: Option, }, Return { expr: Option, @@ -225,7 +232,7 @@ impl Expr { f(*else_branch); } } - Expr::Block { statements, tail } => { + Expr::Block { statements, tail, .. } => { for stmt in statements { match stmt { Statement::Let { initializer, .. } => { @@ -241,8 +248,8 @@ impl Expr { } } Expr::TryBlock { body } => f(*body), - Expr::Loop { body } => f(*body), - Expr::While { condition, body } => { + Expr::Loop { body, .. } => f(*body), + Expr::While { condition, body, .. } => { f(*condition); f(*body); } @@ -268,8 +275,8 @@ impl Expr { f(arm.expr); } } - Expr::Continue => {} - Expr::Break { expr } | Expr::Return { expr } => { + Expr::Continue { .. } => {}, + Expr::Break { expr, .. } | Expr::Return { expr } => { if let Some(expr) = expr { f(*expr); } -- cgit v1.2.3