diff options
author | Lukas Wirth <[email protected]> | 2020-12-23 11:24:24 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2020-12-23 11:24:24 +0000 |
commit | a142beaf013a016a48eb9f193b55e0cbcb80b6a9 (patch) | |
tree | 6445b1e3de954913766b0b53aad15ebdd100a587 | |
parent | 0a780c0ab3869d92fb56ae3b2ddc7636fb169314 (diff) |
Implement const block inference
-rw-r--r-- | crates/hir_def/src/body/lower.rs | 4 | ||||
-rw-r--r-- | crates/hir_def/src/expr.rs | 8 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 2 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/simple.rs | 13 | ||||
-rw-r--r-- | 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<'_> { | |||
246 | let body = self.collect_block_opt(e.block_expr()); | 246 | let body = self.collect_block_opt(e.block_expr()); |
247 | self.alloc_expr(Expr::Async { body }, syntax_ptr) | 247 | self.alloc_expr(Expr::Async { body }, syntax_ptr) |
248 | } | 248 | } |
249 | ast::Effect::Const(_) => { | ||
250 | let body = self.collect_block_opt(e.block_expr()); | ||
251 | self.alloc_expr(Expr::Const { body }, syntax_ptr) | ||
252 | } | ||
249 | }, | 253 | }, |
250 | ast::Expr::BlockExpr(e) => self.collect_block(e), | 254 | ast::Expr::BlockExpr(e) => self.collect_block(e), |
251 | ast::Expr::LoopExpr(e) => { | 255 | 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 { | |||
114 | Async { | 114 | Async { |
115 | body: ExprId, | 115 | body: ExprId, |
116 | }, | 116 | }, |
117 | Const { | ||
118 | body: ExprId, | ||
119 | }, | ||
117 | Cast { | 120 | Cast { |
118 | expr: ExprId, | 121 | expr: ExprId, |
119 | type_ref: TypeRef, | 122 | type_ref: TypeRef, |
@@ -253,7 +256,10 @@ impl Expr { | |||
253 | f(*expr); | 256 | f(*expr); |
254 | } | 257 | } |
255 | } | 258 | } |
256 | Expr::TryBlock { body } | Expr::Unsafe { body } | Expr::Async { body } => f(*body), | 259 | Expr::TryBlock { body } |
260 | | Expr::Unsafe { body } | ||
261 | | Expr::Async { body } | ||
262 | | Expr::Const { body } => f(*body), | ||
257 | Expr::Loop { body, .. } => f(*body), | 263 | Expr::Loop { body, .. } => f(*body), |
258 | Expr::While { condition, body, .. } => { | 264 | Expr::While { condition, body, .. } => { |
259 | f(*condition); | 265 | 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> { | |||
155 | } | 155 | } |
156 | None => self.infer_block(statements, *tail, expected), | 156 | None => self.infer_block(statements, *tail, expected), |
157 | }, | 157 | }, |
158 | Expr::Unsafe { body } => self.infer_expr(*body, expected), | 158 | Expr::Unsafe { body } | Expr::Const { body } => self.infer_expr(*body, expected), |
159 | Expr::TryBlock { body } => { | 159 | Expr::TryBlock { body } => { |
160 | let _inner = self.infer_expr(*body, expected); | 160 | let _inner = self.infer_expr(*body, expected); |
161 | // FIXME should be std::result::Result<{inner}, _> | 161 | // 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() { | |||
1894 | let x = unsafe { 92 }; | 1894 | let x = unsafe { 92 }; |
1895 | let y = async { async { () }.await }; | 1895 | let y = async { async { () }.await }; |
1896 | let z = try { () }; | 1896 | let z = try { () }; |
1897 | let w = const { 92 }; | ||
1897 | let t = 'a: { 92 }; | 1898 | let t = 'a: { 92 }; |
1898 | } | 1899 | } |
1899 | 1900 | ||
@@ -1905,7 +1906,7 @@ fn effects_smoke_test() { | |||
1905 | } | 1906 | } |
1906 | "#, | 1907 | "#, |
1907 | expect![[r#" | 1908 | expect![[r#" |
1908 | 16..136 '{ ...2 }; }': () | 1909 | 16..162 '{ ...2 }; }': () |
1909 | 26..27 'x': i32 | 1910 | 26..27 'x': i32 |
1910 | 30..43 'unsafe { 92 }': i32 | 1911 | 30..43 'unsafe { 92 }': i32 |
1911 | 37..43 '{ 92 }': i32 | 1912 | 37..43 '{ 92 }': i32 |
@@ -1921,9 +1922,13 @@ fn effects_smoke_test() { | |||
1921 | 99..109 'try { () }': {unknown} | 1922 | 99..109 'try { () }': {unknown} |
1922 | 103..109 '{ () }': () | 1923 | 103..109 '{ () }': () |
1923 | 105..107 '()': () | 1924 | 105..107 '()': () |
1924 | 119..120 't': i32 | 1925 | 119..120 'w': i32 |
1925 | 127..133 '{ 92 }': i32 | 1926 | 123..135 'const { 92 }': i32 |
1926 | 129..131 '92': i32 | 1927 | 129..135 '{ 92 }': i32 |
1928 | 131..133 '92': i32 | ||
1929 | 145..146 't': i32 | ||
1930 | 153..159 '{ 92 }': i32 | ||
1931 | 155..157 '92': i32 | ||
1927 | "#]], | 1932 | "#]], |
1928 | ) | 1933 | ) |
1929 | } | 1934 | } |
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 { | |||
358 | Async(SyntaxToken), | 358 | Async(SyntaxToken), |
359 | Unsafe(SyntaxToken), | 359 | Unsafe(SyntaxToken), |
360 | Try(SyntaxToken), | 360 | Try(SyntaxToken), |
361 | Const(SyntaxToken), | ||
361 | // Very much not an effect, but we stuff it into this node anyway | 362 | // Very much not an effect, but we stuff it into this node anyway |
362 | Label(ast::Label), | 363 | Label(ast::Label), |
363 | } | 364 | } |
@@ -373,6 +374,9 @@ impl ast::EffectExpr { | |||
373 | if let Some(token) = self.try_token() { | 374 | if let Some(token) = self.try_token() { |
374 | return Effect::Try(token); | 375 | return Effect::Try(token); |
375 | } | 376 | } |
377 | if let Some(token) = self.const_token() { | ||
378 | return Effect::Const(token); | ||
379 | } | ||
376 | if let Some(label) = self.label() { | 380 | if let Some(label) = self.label() { |
377 | return Effect::Label(label); | 381 | return Effect::Label(label); |
378 | } | 382 | } |