Skip to content

Commit a142bea

Browse files
committed
Implement const block inference
1 parent 0a780c0 commit a142bea

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

crates/hir_def/src/body/lower.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ impl ExprCollector<'_> {
246246
let body = self.collect_block_opt(e.block_expr());
247247
self.alloc_expr(Expr::Async { body }, syntax_ptr)
248248
}
249+
ast::Effect::Const(_) => {
250+
let body = self.collect_block_opt(e.block_expr());
251+
self.alloc_expr(Expr::Const { body }, syntax_ptr)
252+
}
249253
},
250254
ast::Expr::BlockExpr(e) => self.collect_block(e),
251255
ast::Expr::LoopExpr(e) => {

crates/hir_def/src/expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ pub enum Expr {
114114
Async {
115115
body: ExprId,
116116
},
117+
Const {
118+
body: ExprId,
119+
},
117120
Cast {
118121
expr: ExprId,
119122
type_ref: TypeRef,
@@ -253,7 +256,10 @@ impl Expr {
253256
f(*expr);
254257
}
255258
}
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),
257263
Expr::Loop { body, .. } => f(*body),
258264
Expr::While { condition, body, .. } => {
259265
f(*condition);

crates/hir_ty/src/infer/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'a> InferenceContext<'a> {
155155
}
156156
None => self.infer_block(statements, *tail, expected),
157157
},
158-
Expr::Unsafe { body } => self.infer_expr(*body, expected),
158+
Expr::Unsafe { body } | Expr::Const { body } => self.infer_expr(*body, expected),
159159
Expr::TryBlock { body } => {
160160
let _inner = self.infer_expr(*body, expected);
161161
// FIXME should be std::result::Result<{inner}, _>

crates/hir_ty/src/tests/simple.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,7 @@ fn effects_smoke_test() {
18941894
let x = unsafe { 92 };
18951895
let y = async { async { () }.await };
18961896
let z = try { () };
1897+
let w = const { 92 };
18971898
let t = 'a: { 92 };
18981899
}
18991900
@@ -1905,7 +1906,7 @@ fn effects_smoke_test() {
19051906
}
19061907
"#,
19071908
expect![[r#"
1908-
16..136 '{ ...2 }; }': ()
1909+
16..162 '{ ...2 }; }': ()
19091910
26..27 'x': i32
19101911
30..43 'unsafe { 92 }': i32
19111912
37..43 '{ 92 }': i32
@@ -1921,9 +1922,13 @@ fn effects_smoke_test() {
19211922
99..109 'try { () }': {unknown}
19221923
103..109 '{ () }': ()
19231924
105..107 '()': ()
1924-
119..120 't': i32
1925-
127..133 '{ 92 }': i32
1926-
129..131 '92': i32
1925+
119..120 'w': i32
1926+
123..135 'const { 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
19271932
"#]],
19281933
)
19291934
}

crates/syntax/src/ast/expr_ext.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ pub enum Effect {
358358
Async(SyntaxToken),
359359
Unsafe(SyntaxToken),
360360
Try(SyntaxToken),
361+
Const(SyntaxToken),
361362
// Very much not an effect, but we stuff it into this node anyway
362363
Label(ast::Label),
363364
}
@@ -373,6 +374,9 @@ impl ast::EffectExpr {
373374
if let Some(token) = self.try_token() {
374375
return Effect::Try(token);
375376
}
377+
if let Some(token) = self.const_token() {
378+
return Effect::Const(token);
379+
}
376380
if let Some(label) = self.label() {
377381
return Effect::Label(label);
378382
}

0 commit comments

Comments
 (0)