Skip to content

Commit 94dc7a3

Browse files
committed
Auto merge of #14251 - Veykril:ty-expr-stmt, r=Veykril
internal: Set expectation for no-semi expression statements to unit
2 parents 5efcfe5 + b85e2af commit 94dc7a3

File tree

16 files changed

+394
-288
lines changed

16 files changed

+394
-288
lines changed

crates/hir-def/src/body/lower.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::{
3737
item_scope::BuiltinShadowMode,
3838
path::{GenericArgs, Path},
3939
type_ref::{Mutability, Rawness, TypeRef},
40-
AdtId, BlockLoc, ModuleDefId, UnresolvedMacro,
40+
AdtId, BlockId, BlockLoc, ModuleDefId, UnresolvedMacro,
4141
};
4242

4343
pub struct LowerCtx<'a> {
@@ -238,33 +238,32 @@ impl ExprCollector<'_> {
238238
}
239239
ast::Expr::BlockExpr(e) => match e.modifier() {
240240
Some(ast::BlockModifier::Try(_)) => {
241-
let body = self.collect_block(e);
242-
self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
241+
self.collect_block_(e, |id, statements, tail| Expr::TryBlock {
242+
id,
243+
statements,
244+
tail,
245+
})
243246
}
244247
Some(ast::BlockModifier::Unsafe(_)) => {
245-
let body = self.collect_block(e);
246-
self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
248+
self.collect_block_(e, |id, statements, tail| Expr::Unsafe {
249+
id,
250+
statements,
251+
tail,
252+
})
247253
}
248-
// FIXME: we need to record these effects somewhere...
249254
Some(ast::BlockModifier::Label(label)) => {
250255
let label = self.collect_label(label);
251-
let res = self.collect_block(e);
252-
match &mut self.body.exprs[res] {
253-
Expr::Block { label: block_label, .. } => {
254-
*block_label = Some(label);
255-
}
256-
_ => unreachable!(),
257-
}
258-
res
259-
}
260-
Some(ast::BlockModifier::Async(_)) => {
261-
let body = self.collect_block(e);
262-
self.alloc_expr(Expr::Async { body }, syntax_ptr)
263-
}
264-
Some(ast::BlockModifier::Const(_)) => {
265-
let body = self.collect_block(e);
266-
self.alloc_expr(Expr::Const { body }, syntax_ptr)
256+
self.collect_block_(e, |id, statements, tail| Expr::Block {
257+
id,
258+
statements,
259+
tail,
260+
label: Some(label),
261+
})
267262
}
263+
Some(ast::BlockModifier::Async(_)) => self
264+
.collect_block_(e, |id, statements, tail| Expr::Async { id, statements, tail }),
265+
Some(ast::BlockModifier::Const(_)) => self
266+
.collect_block_(e, |id, statements, tail| Expr::Const { id, statements, tail }),
268267
None => self.collect_block(e),
269268
},
270269
ast::Expr::LoopExpr(e) => {
@@ -737,6 +736,19 @@ impl ExprCollector<'_> {
737736
}
738737

739738
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
739+
self.collect_block_(block, |id, statements, tail| Expr::Block {
740+
id,
741+
statements,
742+
tail,
743+
label: None,
744+
})
745+
}
746+
747+
fn collect_block_(
748+
&mut self,
749+
block: ast::BlockExpr,
750+
mk_block: impl FnOnce(BlockId, Box<[Statement]>, Option<ExprId>) -> Expr,
751+
) -> ExprId {
740752
let file_local_id = self.ast_id_map.ast_id(&block);
741753
let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
742754
let block_loc =
@@ -769,15 +781,8 @@ impl ExprCollector<'_> {
769781
});
770782

771783
let syntax_node_ptr = AstPtr::new(&block.into());
772-
let expr_id = self.alloc_expr(
773-
Expr::Block {
774-
id: block_id,
775-
statements: statements.into_boxed_slice(),
776-
tail,
777-
label: None,
778-
},
779-
syntax_node_ptr,
780-
);
784+
let expr_id = self
785+
.alloc_expr(mk_block(block_id, statements.into_boxed_slice(), tail), syntax_node_ptr);
781786

782787
self.expander.def_map = prev_def_map;
783788
self.expander.module = prev_local_module;

crates/hir-def/src/body/pretty.rs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,6 @@ impl<'a> Printer<'a> {
292292
self.print_expr(*expr);
293293
w!(self, "?");
294294
}
295-
Expr::TryBlock { body } => {
296-
w!(self, "try ");
297-
self.print_expr(*body);
298-
}
299-
Expr::Async { body } => {
300-
w!(self, "async ");
301-
self.print_expr(*body);
302-
}
303-
Expr::Const { body } => {
304-
w!(self, "const ");
305-
self.print_expr(*body);
306-
}
307295
Expr::Cast { expr, type_ref } => {
308296
self.print_expr(*expr);
309297
w!(self, " as ");
@@ -402,10 +390,6 @@ impl<'a> Printer<'a> {
402390
}
403391
w!(self, ")");
404392
}
405-
Expr::Unsafe { body } => {
406-
w!(self, "unsafe ");
407-
self.print_expr(*body);
408-
}
409393
Expr::Array(arr) => {
410394
w!(self, "[");
411395
if !matches!(arr, Array::ElementList { elements, .. } if elements.is_empty()) {
@@ -428,25 +412,47 @@ impl<'a> Printer<'a> {
428412
}
429413
Expr::Literal(lit) => self.print_literal(lit),
430414
Expr::Block { id: _, statements, tail, label } => {
431-
self.whitespace();
432-
if let Some(lbl) = label {
433-
w!(self, "{}: ", self.body[*lbl].name);
415+
let label = label.map(|lbl| format!("{}: ", self.body[lbl].name));
416+
self.print_block(label.as_deref(), statements, tail);
417+
}
418+
Expr::Unsafe { id: _, statements, tail } => {
419+
self.print_block(Some("unsafe "), statements, tail);
420+
}
421+
Expr::TryBlock { id: _, statements, tail } => {
422+
self.print_block(Some("try "), statements, tail);
423+
}
424+
Expr::Async { id: _, statements, tail } => {
425+
self.print_block(Some("async "), statements, tail);
426+
}
427+
Expr::Const { id: _, statements, tail } => {
428+
self.print_block(Some("const "), statements, tail);
429+
}
430+
}
431+
}
432+
433+
fn print_block(
434+
&mut self,
435+
label: Option<&str>,
436+
statements: &Box<[Statement]>,
437+
tail: &Option<la_arena::Idx<Expr>>,
438+
) {
439+
self.whitespace();
440+
if let Some(lbl) = label {
441+
w!(self, "{}", lbl);
442+
}
443+
w!(self, "{{");
444+
if !statements.is_empty() || tail.is_some() {
445+
self.indented(|p| {
446+
for stmt in &**statements {
447+
p.print_stmt(stmt);
434448
}
435-
w!(self, "{{");
436-
if !statements.is_empty() || tail.is_some() {
437-
self.indented(|p| {
438-
for stmt in &**statements {
439-
p.print_stmt(stmt);
440-
}
441-
if let Some(tail) = tail {
442-
p.print_expr(*tail);
443-
}
444-
p.newline();
445-
});
449+
if let Some(tail) = tail {
450+
p.print_expr(*tail);
446451
}
447-
w!(self, "}}");
448-
}
452+
p.newline();
453+
});
449454
}
455+
w!(self, "}}");
450456
}
451457

452458
fn print_pat(&mut self, pat: PatId) {

crates/hir-def/src/body/scope.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
194194
scopes.set_scope(expr, scope);
195195
compute_block_scopes(statements, *tail, body, scopes, &mut scope);
196196
}
197+
Expr::Unsafe { id, statements, tail }
198+
| Expr::Async { id, statements, tail }
199+
| Expr::Const { id, statements, tail }
200+
| Expr::TryBlock { id, statements, tail } => {
201+
let mut scope = scopes.new_block_scope(*scope, *id, None);
202+
// Overwrite the old scope for the block expr, so that every block scope can be found
203+
// via the block itself (important for blocks that only contain items, no expressions).
204+
scopes.set_scope(expr, scope);
205+
compute_block_scopes(statements, *tail, body, scopes, &mut scope);
206+
}
197207
Expr::For { iterable, pat, body: body_expr, label } => {
198208
compute_expr_scopes(*iterable, body, scopes, scope);
199209
let mut scope = scopes.new_labeled_scope(*scope, make_label(label));

crates/hir-def/src/expr.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ pub enum Expr {
109109
tail: Option<ExprId>,
110110
label: Option<LabelId>,
111111
},
112+
TryBlock {
113+
id: BlockId,
114+
statements: Box<[Statement]>,
115+
tail: Option<ExprId>,
116+
},
117+
Async {
118+
id: BlockId,
119+
statements: Box<[Statement]>,
120+
tail: Option<ExprId>,
121+
},
122+
Const {
123+
id: BlockId,
124+
statements: Box<[Statement]>,
125+
tail: Option<ExprId>,
126+
},
127+
Unsafe {
128+
id: BlockId,
129+
statements: Box<[Statement]>,
130+
tail: Option<ExprId>,
131+
},
112132
Loop {
113133
body: ExprId,
114134
label: Option<LabelId>,
@@ -172,15 +192,6 @@ pub enum Expr {
172192
Try {
173193
expr: ExprId,
174194
},
175-
TryBlock {
176-
body: ExprId,
177-
},
178-
Async {
179-
body: ExprId,
180-
},
181-
Const {
182-
body: ExprId,
183-
},
184195
Cast {
185196
expr: ExprId,
186197
type_ref: Interned<TypeRef>,
@@ -222,9 +233,6 @@ pub enum Expr {
222233
exprs: Box<[ExprId]>,
223234
is_assignee_expr: bool,
224235
},
225-
Unsafe {
226-
body: ExprId,
227-
},
228236
Array(Array),
229237
Literal(Literal),
230238
Underscore,
@@ -290,13 +298,20 @@ impl Expr {
290298
Expr::Let { expr, .. } => {
291299
f(*expr);
292300
}
293-
Expr::Block { statements, tail, .. } => {
301+
Expr::Block { statements, tail, .. }
302+
| Expr::TryBlock { statements, tail, .. }
303+
| Expr::Unsafe { statements, tail, .. }
304+
| Expr::Async { statements, tail, .. }
305+
| Expr::Const { statements, tail, .. } => {
294306
for stmt in statements.iter() {
295307
match stmt {
296-
Statement::Let { initializer, .. } => {
308+
Statement::Let { initializer, else_branch, .. } => {
297309
if let &Some(expr) = initializer {
298310
f(expr);
299311
}
312+
if let &Some(expr) = else_branch {
313+
f(expr);
314+
}
300315
}
301316
Statement::Expr { expr: expression, .. } => f(*expression),
302317
}
@@ -305,10 +320,6 @@ impl Expr {
305320
f(expr);
306321
}
307322
}
308-
Expr::TryBlock { body }
309-
| Expr::Unsafe { body }
310-
| Expr::Async { body }
311-
| Expr::Const { body } => f(*body),
312323
Expr::Loop { body, .. } => f(*body),
313324
Expr::While { condition, body, .. } => {
314325
f(*condition);

crates/hir-def/src/resolver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ impl Resolver {
294294
}
295295
}
296296

297-
if let res @ Some(_) = self.module_scope.resolve_path_in_value_ns(db, path) {
298-
return res;
297+
if let Some(res) = self.module_scope.resolve_path_in_value_ns(db, path) {
298+
return Some(res);
299299
}
300300

301301
// If a path of the shape `u16::from_le_bytes` failed to resolve at all, then we fall back

crates/hir-ty/src/diagnostics/unsafe_check.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ fn walk_unsafe(
9494
unsafe_expr_cb(UnsafeExpr { expr: current, inside_unsafe_block });
9595
}
9696
}
97-
Expr::Unsafe { body: child } => {
98-
return walk_unsafe(db, infer, def, body, *child, true, unsafe_expr_cb);
97+
Expr::Unsafe { .. } => {
98+
return expr.walk_child_exprs(|child| {
99+
walk_unsafe(db, infer, def, body, child, true, unsafe_expr_cb);
100+
});
99101
}
100102
_ => {}
101103
}

0 commit comments

Comments
 (0)