From a142beaf013a016a48eb9f193b55e0cbcb80b6a9 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 23 Dec 2020 12:24:24 +0100 Subject: Implement const block inference --- crates/hir_def/src/body/lower.rs | 4 ++++ crates/hir_def/src/expr.rs | 8 +++++++- crates/hir_ty/src/infer/expr.rs | 2 +- crates/hir_ty/src/tests/simple.rs | 13 +++++++++---- crates/syntax/src/ast/expr_ext.rs | 4 ++++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 4492a7d77..1b98504bb 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -246,6 +246,10 @@ impl ExprCollector<'_> { let body = self.collect_block_opt(e.block_expr()); self.alloc_expr(Expr::Async { body }, syntax_ptr) } + ast::Effect::Const(_) => { + let body = self.collect_block_opt(e.block_expr()); + self.alloc_expr(Expr::Const { body }, syntax_ptr) + } }, ast::Expr::BlockExpr(e) => self.collect_block(e), ast::Expr::LoopExpr(e) => { diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs index b1e57c693..3bba30397 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs @@ -114,6 +114,9 @@ pub enum Expr { Async { body: ExprId, }, + Const { + body: ExprId, + }, Cast { expr: ExprId, type_ref: TypeRef, @@ -253,7 +256,10 @@ impl Expr { f(*expr); } } - Expr::TryBlock { body } | Expr::Unsafe { body } | Expr::Async { body } => f(*body), + Expr::TryBlock { body } + | Expr::Unsafe { body } + | Expr::Async { body } + | Expr::Const { body } => f(*body), Expr::Loop { body, .. } => f(*body), Expr::While { condition, body, .. } => { f(*condition); diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 2cdce2cef..744569e6e 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -155,7 +155,7 @@ impl<'a> InferenceContext<'a> { } None => self.infer_block(statements, *tail, expected), }, - Expr::Unsafe { body } => self.infer_expr(*body, expected), + Expr::Unsafe { body } | Expr::Const { body } => self.infer_expr(*body, expected), Expr::TryBlock { body } => { let _inner = self.infer_expr(*body, expected); // FIXME should be std::result::Result<{inner}, _> diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index a569223b4..a61282d5a 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -1894,6 +1894,7 @@ fn effects_smoke_test() { let x = unsafe { 92 }; let y = async { async { () }.await }; let z = try { () }; + let w = const { 92 }; let t = 'a: { 92 }; } @@ -1905,7 +1906,7 @@ fn effects_smoke_test() { } "#, expect![[r#" - 16..136 '{ ...2 }; }': () + 16..162 '{ ...2 }; }': () 26..27 'x': i32 30..43 'unsafe { 92 }': i32 37..43 '{ 92 }': i32 @@ -1921,9 +1922,13 @@ fn effects_smoke_test() { 99..109 'try { () }': {unknown} 103..109 '{ () }': () 105..107 '()': () - 119..120 't': i32 - 127..133 '{ 92 }': i32 - 129..131 '92': i32 + 119..120 'w': i32 + 123..135 'const { 92 }': i32 + 129..135 '{ 92 }': i32 + 131..133 '92': i32 + 145..146 't': i32 + 153..159 '{ 92 }': i32 + 155..157 '92': i32 "#]], ) } diff --git a/crates/syntax/src/ast/expr_ext.rs b/crates/syntax/src/ast/expr_ext.rs index e4a9b945c..636ce166d 100644 --- a/crates/syntax/src/ast/expr_ext.rs +++ b/crates/syntax/src/ast/expr_ext.rs @@ -358,6 +358,7 @@ pub enum Effect { Async(SyntaxToken), Unsafe(SyntaxToken), Try(SyntaxToken), + Const(SyntaxToken), // Very much not an effect, but we stuff it into this node anyway Label(ast::Label), } @@ -373,6 +374,9 @@ impl ast::EffectExpr { if let Some(token) = self.try_token() { return Effect::Try(token); } + if let Some(token) = self.const_token() { + return Effect::Const(token); + } if let Some(label) = self.label() { return Effect::Label(label); } -- cgit v1.2.3