Debian Codegraph @

gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If
17 // not see
18 // <http://www.gnu.org/licenses/>.
19
20 #include "rust-bir-builder-expr-stmt.h"
21 #include "rust-bir-builder-lazyboolexpr.h"
22 #include "rust-bir-builder-pattern.h"
23 #include "rust-bir-builder-struct.h"
24 #include "rust-hir-expr.h"
25
26 namespace Rust {
27 namespace BIR {
28
29 using LoopAndLabelCtx = BuilderContext::LoopAndLabelCtx;
30
31 BuilderContext::LoopAndLabelCtx &
32 ExprStmtBuilder::setup_loop (HIR::BaseLoopExpr &expr)
33 {
34 NodeId label
35 = (expr.has_loop_label ())
36 ? expr.get_loop_label ().get_lifetime ().get_mappings ().get_nodeid ()
37 : UNKNOWN_NODEID;
38 PlaceId label_var = take_or_create_return_place (lookup_type (expr));
39
40 BasicBlockId continue_bb = new_bb ();
41 push_goto (continue_bb);
42 ctx.current_bb = continue_bb;
43 // falseUnwind
44 start_new_consecutive_bb ();
45
46 BasicBlockId break_bb = new_bb ();
47 // We are still outside the loop block;
48 ScopeId continue_scope
49 = ctx.place_db.get_current_scope_id ().next_scope_id ();
50 ctx.loop_and_label_stack.emplace_back (true, label, label_var, break_bb,
51 continue_bb, continue_scope);
52
53 return ctx.loop_and_label_stack.back ();
54 }
55
56 BuilderContext::LoopAndLabelCtx &
57 ExprStmtBuilder::get_label_ctx (HIR::Lifetime &label)
58 {
59 NodeId label_id = resolve_label (label);
60 auto lookup = std::find_if (ctx.loop_and_label_stack.rbegin (),
61 ctx.loop_and_label_stack.rend (),
62 [label_id] (LoopAndLabelCtx &info) {
63 return info.label == label_id;
64 });
65 rust_assert (lookup != ctx.loop_and_label_stack.rend ());
66 return *lookup;
67 }
68
69 LoopAndLabelCtx &
70 ExprStmtBuilder::get_unnamed_loop_ctx ()
71 {
72 auto lookup
73 = std::find_if (ctx.loop_and_label_stack.rbegin (),
74 ctx.loop_and_label_stack.rend (),
75 [] (LoopAndLabelCtx &info) { return info.is_loop; });
76 rust_assert (lookup != ctx.loop_and_label_stack.rend ());
77 return *lookup;
78 }
79
80 void
81 ExprStmtBuilder::visit (HIR::ClosureExpr &expr)
82 {
83 auto closure_ty = lookup_type (expr)->as<TyTy::ClosureType> ();
84 std::vector<PlaceId> captures;
85 std::vector<location_t> capture_locations;
86 for (auto &capture : closure_ty->get_captures ())
87 {
88 captures.push_back (ctx.place_db.lookup_variable (capture));
89 auto location = Analysis::Mappings::get ()
90 .lookup_ast_item (capture)
91 .value ()
92 ->get_locus ();
93 capture_locations.push_back (location);
94 }
95 move_all (captures, capture_locations);
96
97 // Note: Not a coercion site for captures.
98 return_expr (new InitializerExpr (std::move (captures)), lookup_type (expr),
99 expr.get_locus ());
100 }
101
102 void
103 ExprStmtBuilder::visit (HIR::StructExprStructFields &fields)
104 {
105 auto *p_adt_type = lookup_type (fields)->as<TyTy::ADTType> ();
106 auto struct_ty = p_adt_type->get_variants ().at (0);
107 auto init_values = StructBuilder (ctx, struct_ty).build (fields);
108 // collect fields locations
109 std::vector<location_t> field_locations;
110 for (auto &field : fields.get_fields ())
111 {
112 field_locations.push_back (field->get_locus ());
113 }
114 move_all (init_values, field_locations);
115 return_expr (new InitializerExpr (std::move (init_values)),
116 lookup_type (fields), fields.get_locus ());
117 }
118
119 void
120 ExprStmtBuilder::visit (HIR::StructExprStruct &expr)
121 {
122 // There is no way to modify empty struct, which makes them constant.
123 return_place (ctx.place_db.get_constant (lookup_type (expr)),
124 expr.get_locus ());
125 }
126
127 void
128 ExprStmtBuilder::visit (HIR::LiteralExpr &expr)
129 {
130 // Different literal values of the same type are not distinguished in BIR.
131 return_place (ctx.place_db.get_constant (lookup_type (expr)),
132 expr.get_locus ());
133 }
134
135 void
136 ExprStmtBuilder::visit (HIR::BorrowExpr &expr)
137 {
138 auto operand = visit_expr (expr.get_expr ());
139 if (ctx.place_db[operand].is_constant ())
140 {
141 // Cannot borrow a constant, must create a temporary copy.
142 push_tmp_assignment (operand, expr.get_locus ());
143 operand = translated;
144 }
145
146 // BorrowExpr cannot be annotated with lifetime.
147 return_borrowed (operand, lookup_type (expr), expr.get_locus ());
148 }
149
150 void
151 ExprStmtBuilder::visit (HIR::DereferenceExpr &expr)
152 {
153 auto operand = visit_expr (expr.get_expr ());
154 return_place (ctx.place_db.lookup_or_add_path (Place::DEREF,
155 lookup_type (expr), operand),
156 expr.get_locus ());
157 }
158
159 void
160 ExprStmtBuilder::visit (HIR::ErrorPropagationExpr &expr)
161 {
162 // TODO: desugar in AST->HIR
163 rust_sorry_at (expr.get_locus (), "error propagation is not supported");
164 }
165
166 void
167 ExprStmtBuilder::visit (HIR::NegationExpr &expr)
168 {
169 PlaceId operand = visit_expr (expr.get_expr ());
170 return_expr (new Operator<1> (
171 {move_place (operand, expr.get_expr ().get_locus ())}),
172 lookup_type (expr), expr.get_locus ());
173 }
174
175 void
176 ExprStmtBuilder::visit (HIR::ArithmeticOrLogicalExpr &expr)
177 {
178 PlaceId lhs = visit_expr (expr.get_lhs ());
179 PlaceId rhs = visit_expr (expr.get_rhs ());
180 return_expr (new Operator<2> (
181 {move_place (lhs, expr.get_lhs ().get_locus ()),
182 move_place (rhs, expr.get_rhs ().get_locus ())}),
183 lookup_type (expr), expr.get_locus ());
184 }
185
186 void
187 ExprStmtBuilder::visit (HIR::ComparisonExpr &expr)
188 {
189 PlaceId lhs = visit_expr (expr.get_lhs ());
190 PlaceId rhs = visit_expr (expr.get_rhs ());
191 return_expr (new Operator<2> (
192 {move_place (lhs, expr.get_lhs ().get_locus ()),
193 move_place (rhs, expr.get_rhs ().get_locus ())}),
194 lookup_type (expr), expr.get_locus ());
195 }
196
197 void
198 ExprStmtBuilder::visit (HIR::LazyBooleanExpr &expr)
199 {
200 return_place (LazyBooleanExprBuilder (ctx, take_or_create_return_place (
201 lookup_type (expr)))
202 .build (expr),
203 expr.get_locus ());
204 }
205
206 void
207 ExprStmtBuilder::visit (HIR::TypeCastExpr &expr)
208 {
209 auto operand = visit_expr (expr.get_expr ());
210 return_expr (new Operator<1> ({operand}), lookup_type (expr),
211 expr.get_locus ());
212 }
213
214 void
215 ExprStmtBuilder::visit (HIR::AssignmentExpr &expr)
216 {
217 auto lhs = visit_expr (expr.get_lhs ());
218 auto rhs = visit_expr (expr.get_rhs ());
219 push_assignment (lhs, rhs, expr.get_locus ());
220 translated = INVALID_PLACE;
221 }
222
223 void
224 ExprStmtBuilder::visit (HIR::CompoundAssignmentExpr &expr)
225 {
226 auto lhs = visit_expr (expr.get_lhs ());
227 auto rhs = visit_expr (expr.get_rhs ());
228 push_assignment (lhs, new Operator<2> ({lhs, rhs}), expr.get_locus ());
229 }
230
231 void
232 ExprStmtBuilder::visit (HIR::GroupedExpr &expr)
233 {
234 return_place (visit_expr (expr.get_expr_in_parens ()), expr.get_locus ());
235 }
236
237 void
238 ExprStmtBuilder::visit (HIR::ArrayExpr &expr)
239 {
240 auto &elems = expr.get_internal_elements ();
241 switch (elems.get_array_expr_type ())
242 {
243 case HIR::ArrayElems::VALUES: {
244 auto &elem_vals = (static_cast<HIR::ArrayElemsValues &> (elems));
245 auto init_values = visit_list (elem_vals.get_values ());
246 // collect locations
247 std::vector<location_t> value_locations;
248 for (auto &value : elem_vals.get_values ())
249 {
250 value_locations.push_back (value->get_locus ());
251 }
252 move_all (init_values, value_locations);
253 return_expr (new InitializerExpr (std::move (init_values)),
254 lookup_type (expr), expr.get_locus ());
255 break;
256 }
257 case HIR::ArrayElems::COPIED: {
258 auto &elem_copied = (static_cast<HIR::ArrayElemsCopied &> (elems));
259 auto init = visit_expr (elem_copied.get_elem_to_copy ());
260 return_expr (new InitializerExpr ({init}), lookup_type (expr),
261 expr.get_locus ());
262 break;
263 }
264 }
265 }
266
267 void
268 ExprStmtBuilder::visit (HIR::ArrayIndexExpr &expr)
269 {
270 auto lhs = visit_expr (expr.get_array_expr ());
271 auto rhs = visit_expr (expr.get_index_expr ());
272 // The index is not tracked in BIR.
273 std::ignore = rhs;
274 return_place (ctx.place_db.lookup_or_add_path (Place::INDEX,
275 lookup_type (expr), lhs),
276 expr.get_locus ());
277 }
278
279 void
280 ExprStmtBuilder::visit (HIR::TupleExpr &expr)
281 {
282 std::vector<PlaceId> init_values = visit_list (expr.get_tuple_elems ());
283 return_expr (new InitializerExpr (std::move (init_values)),
284 lookup_type (expr), expr.get_locus ());
285 }
286
287 void
288 ExprStmtBuilder::visit (HIR::TupleIndexExpr &expr)
289 {
290 auto tuple = visit_expr (expr.get_tuple_expr ());
291 return_place (ctx.place_db.lookup_or_add_path (Place::FIELD,
292 lookup_type (expr), tuple,
293 expr.get_tuple_index ()),
294 expr.get_locus ());
295 }
296
297 void
298 ExprStmtBuilder::visit (HIR::CallExpr &expr)
299 {
300 PlaceId fn = visit_expr (expr.get_fnexpr ());
301 std::vector<PlaceId> arguments = visit_list (expr.get_arguments ());
302
303 const auto fn_type
304 = ctx.place_db[fn].tyty->as<const TyTy::CallableTypeInterface> ();
305
306 for (size_t i = 0; i < fn_type->get_num_params (); ++i)
307 {
308 coercion_site (arguments[i], fn_type->get_param_type_at (i));
309 }
310
311 // collect parameter locations
312 std::vector<location_t> parameter_locations;
313 for (auto &parameter : expr.get_arguments ())
314 {
315 parameter_locations.push_back (parameter->get_locus ());
316 }
317 move_all (arguments, parameter_locations);
318
319 return_expr (new CallExpr (fn, std::move (arguments)), lookup_type (expr),
320 expr.get_locus (), true);
321 }
322
323 void
324 ExprStmtBuilder::visit (HIR::InlineAsm &expr)
325 {}
326
327 void
328 ExprStmtBuilder::visit (HIR::MethodCallExpr &expr)
329 {}
330
331 void
332 ExprStmtBuilder::visit (HIR::FieldAccessExpr &expr)
333 {
334 auto receiver = visit_expr (expr.get_receiver_expr ());
335 auto type = autoderef (receiver);
336 rust_assert (type->get_kind () == TyTy::ADT);
337 auto adt = type->as<TyTy::ADTType> ();
338 rust_assert (!adt->is_enum ());
339 rust_assert (adt->number_of_variants () == 1);
340 auto struct_ty = adt->get_variants ().at (0);
341
342 TyTy::StructFieldType *field_ty = nullptr;
343 size_t field_index = 0;
344 bool ok = struct_ty->lookup_field (expr.get_field_name ().as_string (),
345 &field_ty, &field_index);
346 rust_assert (ok);
347
348 return_place (ctx.place_db.lookup_or_add_path (Place::FIELD,
349 field_ty->get_field_type (),
350 receiver, field_index),
351 expr.get_locus ());
352 }
353
354 void
355 ExprStmtBuilder::visit (HIR::BlockExpr &block)
356 {
357 push_new_scope ();
358
359 if (block.has_label ())
360 {
361 NodeId label
362 = block.get_label ().get_lifetime ().get_mappings ().get_nodeid ();
363 PlaceId label_var = take_or_create_return_place (lookup_type (block));
364 ctx.loop_and_label_stack.emplace_back (
365 false, label, label_var, new_bb (), INVALID_BB,
366 ctx.place_db.get_current_scope_id ());
367 }
368
369 // Eliminates dead code after break, continue, return.
370 bool unreachable = false;
371 for (auto &stmt : block.get_statements ())
372 {
373 stmt->accept_vis (*this);
374 if (ctx.get_current_bb ().is_terminated ())
375 {
376 unreachable = true;
377 break;
378 }
379 }
380
381 if (block.has_label ())
382 {
383 auto block_ctx = ctx.loop_and_label_stack.back ();
384 if (block.has_expr () && !unreachable)
385 {
386 push_assignment (block_ctx.label_var,
387 visit_expr (block.get_final_expr ()),
388 block.get_start_locus ());
389 }
390 if (!ctx.get_current_bb ().is_terminated ())
391 {
392 push_goto (block_ctx.break_bb);
393 }
394 ctx.current_bb = block_ctx.break_bb;
395 ctx.loop_and_label_stack.pop_back ();
396
397 return_place (block_ctx.label_var, block.get_start_locus ());
398 }
399 else if (block.has_expr () && !unreachable)
400 {
401 return_place (visit_expr (block.get_final_expr (),
402 take_or_create_return_place (
403 lookup_type (block.get_final_expr ()))),
404 block.get_start_locus ());
405 }
406
407 if (!unreachable)
408 pop_scope ();
409 else
410 ctx.place_db.pop_scope ();
411 }
412
413 void
414 ExprStmtBuilder::visit (HIR::ContinueExpr &cont)
415 {
416 LoopAndLabelCtx info = cont.has_label () ? get_label_ctx (cont.get_label ())
417 : get_unnamed_loop_ctx ();
418 start_new_consecutive_bb ();
419 unwind_until (info.continue_scope);
420 push_goto (info.continue_bb);
421 // No code allowed after continue. Handled in BlockExpr.
422 }
423
424 void
425 ExprStmtBuilder::visit (HIR::BreakExpr &brk)
426 {
427 LoopAndLabelCtx info = brk.has_label () ? get_label_ctx (brk.get_label ())
428 : get_unnamed_loop_ctx ();
429 if (brk.has_break_expr ())
430 push_assignment (info.label_var, visit_expr (brk.get_expr ()),
431 brk.get_locus ());
432
433 start_new_consecutive_bb ();
434 unwind_until (ctx.place_db.get_scope (info.continue_scope).parent);
435 push_goto (info.break_bb);
436 // No code allowed after continue. Handled in BlockExpr.
437 }
438
439 void
440 ExprStmtBuilder::visit (HIR::RangeFromToExpr &range)
441 {
442 auto from = visit_expr (range.get_from_expr ());
443 auto to = visit_expr (range.get_to_expr ());
444 return_expr (new InitializerExpr ({from, to}), lookup_type (range),
445 range.get_locus ());
446 }
447
448 void
449 ExprStmtBuilder::visit (HIR::RangeFromExpr &expr)
450 {
451 auto from = visit_expr (expr.get_from_expr ());
452 return_expr (new InitializerExpr ({from}), lookup_type (expr),
453 expr.get_locus ());
454 }
455
456 void
457 ExprStmtBuilder::visit (HIR::RangeToExpr &expr)
458 {
459 auto to = visit_expr (expr.get_to_expr ());
460 return_expr (new InitializerExpr ({to}), lookup_type (expr),
461 expr.get_locus ());
462 }
463
464 void
465 ExprStmtBuilder::visit (HIR::RangeFullExpr &expr)
466 {
467 return_expr (new InitializerExpr ({}), lookup_type (expr), expr.get_locus ());
468 }
469
470 void
471 ExprStmtBuilder::visit (HIR::RangeFromToInclExpr &expr)
472 {
473 auto from = visit_expr (expr.get_from_expr ());
474 auto to = visit_expr (expr.get_to_expr ());
475 return_expr (new InitializerExpr ({from, to}), lookup_type (expr),
476 expr.get_locus ());
477 }
478
479 void
480 ExprStmtBuilder::visit (HIR::RangeToInclExpr &expr)
481 {
482 auto to = visit_expr (expr.get_to_expr ());
483 return_expr (new InitializerExpr ({to}), lookup_type (expr),
484 expr.get_locus ());
485 }
486
487 void
488 ExprStmtBuilder::visit (HIR::ReturnExpr &ret)
489 {
490 if (ret.has_return_expr ())
491 {
492 push_assignment (RETURN_VALUE_PLACE,
493 move_place (visit_expr (ret.get_expr ()),
494 ret.get_expr ().get_locus ()),
495 ret.get_expr ().get_locus ());
496 }
497 unwind_until (ROOT_SCOPE);
498 push_return (ret.get_locus ());
499 translated = INVALID_PLACE;
500 }
501
502 void
503 ExprStmtBuilder::visit (HIR::UnsafeBlockExpr &expr)
504 {
505 rust_sorry_at (expr.get_locus (), "unsafe blocks are not supported");
506 }
507
508 void
509 ExprStmtBuilder::visit (HIR::LoopExpr &expr)
510 {
511 auto loop = setup_loop (expr);
512
513 std::ignore = visit_expr (expr.get_loop_block ());
514 if (!ctx.get_current_bb ().is_terminated ())
515 push_goto (loop.continue_bb);
516
517 ctx.current_bb = loop.break_bb;
518 }
519
520 void
521 ExprStmtBuilder::visit (HIR::WhileLoopExpr &expr)
522 {
523 auto loop = setup_loop (expr);
524
525 auto cond_val = visit_expr (expr.get_predicate_expr ());
526 auto body_bb = new_bb ();
527 push_switch (cond_val, expr.get_locus (), {body_bb, loop.break_bb});
528
529 ctx.current_bb = body_bb;
530 std::ignore = visit_expr (expr.get_loop_block ());
531 push_goto (loop.continue_bb);
532
533 ctx.current_bb = loop.break_bb;
534 }
535
536 void
537 ExprStmtBuilder::visit (HIR::WhileLetLoopExpr &expr)
538 {
539 // TODO: Desugar in AST->HIR
540 rust_sorry_at (expr.get_locus (), "while let loops are not yet supported");
541 }
542
543 void
544 ExprStmtBuilder::visit (HIR::IfExpr &expr)
545 {
546 // If without else cannot return a non-unit value (see [E0317]).
547
548 if (expr.get_if_block ().statements.empty ())
549 return;
550
551 push_switch (visit_expr (expr.get_if_condition ()), expr.get_locus ());
552 BasicBlockId if_block = ctx.current_bb;
553
554 ctx.current_bb = new_bb ();
555 BasicBlockId then_start_block = ctx.current_bb;
556 std::ignore = visit_expr (expr.get_if_block ());
557 if (!ctx.get_current_bb ().is_terminated ())
558 push_goto (INVALID_BB); // Resolved later.
559 BasicBlockId then_end_block = ctx.current_bb;
560
561 ctx.current_bb = new_bb ();
562 BasicBlockId final_block = ctx.current_bb;
563 return_unit (expr);
564
565 // Jumps are added at the end to match rustc MIR order for easier comparison.
566 add_jump (if_block, then_start_block);
567 add_jump (if_block, final_block);
568
569 auto &then_end_bb = ctx.basic_blocks[then_end_block];
570 if (then_end_bb.is_goto_terminated () && then_end_bb.successors.empty ())
571 add_jump (then_end_block, final_block);
572 }
573
574 void
575 ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr)
576 {
577 push_switch (move_place (visit_expr (expr.get_if_condition ()),
578 expr.get_if_condition ().get_locus ()),
579 expr.get_locus ());
580 BasicBlockId if_end_bb = ctx.current_bb;
581
582 PlaceId result = take_or_create_return_place (lookup_type (expr));
583
584 ctx.current_bb = new_bb ();
585 BasicBlockId then_start_bb = ctx.current_bb;
586 std::ignore = visit_expr (expr.get_if_block (), result);
587 if (!ctx.get_current_bb ().is_terminated ())
588 push_goto (INVALID_BB); // Resolved later.
589 BasicBlockId then_end_bb = ctx.current_bb;
590
591 ctx.current_bb = new_bb ();
592 BasicBlockId else_start_bb = ctx.current_bb;
593 std::ignore = visit_expr (expr.get_else_block (), result);
594 if (!ctx.get_current_bb ().is_terminated ())
595 push_goto (INVALID_BB); // Resolved later.
596 BasicBlockId else_end_bb = ctx.current_bb;
597
598 ctx.current_bb = new_bb ();
599 BasicBlockId final_start_bb = ctx.current_bb;
600 return_place (result, expr.get_locus ());
601
602 // Jumps are added at the end to match rustc MIR order for easier comparison.
603 add_jump (if_end_bb, then_start_bb);
604 add_jump (if_end_bb, else_start_bb);
605
606 auto &then_bb = ctx.basic_blocks[then_end_bb];
607 if (then_bb.is_goto_terminated () && then_bb.successors.empty ())
608 add_jump (then_end_bb, final_start_bb);
609
610 auto &else_bb = ctx.basic_blocks[else_end_bb];
611 if (else_bb.is_goto_terminated () && else_bb.successors.empty ())
612 add_jump (else_end_bb, final_start_bb);
613 }
614
615 void
616 ExprStmtBuilder::visit (HIR::MatchExpr &expr)
617 {
618 rust_sorry_at (expr.get_locus (), "match expressions are not supported");
619 // // TODO
620 // expr.get_scrutinee_expr ()->accept_vis (*this);
621 // PlaceId scrutinee = translated;
622 //
623 // BasicBlockId final_bb = new_bb ();
624 //
625 // BasicBlockId next_case_bb = new_bb ();
626 // for (auto &match_case : expr.get_match_cases ())
627 // {
628 // BasicBlockId body_bb = new_bb ();
629 //
630 // BasicBlockId next_pattern_bb = new_bb ();
631 // for (auto &pat : match_case.get_arm ().get_patterns ())
632 // {
633 // compile_pattern_validation (*pat, scrutinee);
634 // push_switch (translated);
635 // add_jump_to (next_pattern_bb);
636 // start_new_subsequent_bb ();
637 // compile_pattern_bindings (*pat, scrutinee);
638 // add_jump_to (body_bb);
639 //
640 // ctx.current_bb = next_pattern_bb;
641 // next_pattern_bb = new_bb ();
642 // }
643 // ctx.current_bb = next_pattern_bb;
644 // // No pattern matched, go to the next case.
645 // add_jump_to (next_case_bb);
646 //
647 // ctx.current_bb = body_bb;
648 // match_case.get_expr ()->accept_vis (*this);
649 // add_jump_to (final_bb);
650 //
651 // ctx.current_bb = next_case_bb;
652 // next_case_bb = new_bb ();
653 // }
654 // add_jump_to (final_bb);
655 //
656 // ctx.current_bb = final_bb;
657 }
658
659 void
660 ExprStmtBuilder::visit (HIR::AwaitExpr &expr)
661 {
662 rust_sorry_at (expr.get_locus (), "await expressions are not supported");
663 }
664
665 void
666 ExprStmtBuilder::visit (HIR::AsyncBlockExpr &expr)
667 {
668 rust_sorry_at (expr.get_locus (), "async blocks are not supported");
669 }
670
671 void
672 ExprStmtBuilder::visit (HIR::QualifiedPathInExpression &expr)
673 {
674 // Note: Type is only stored for the expr, not the segment.
675 PlaceId result = resolve_variable_or_fn (expr, lookup_type (expr));
676 return_place (result, expr.get_locus ());
677 }
678
679 void
680 ExprStmtBuilder::visit (HIR::PathInExpression &expr)
681 {
682 // Note: Type is only stored for the expr, not the segment.
683 PlaceId result = resolve_variable_or_fn (expr, lookup_type (expr));
684 return_place (result, expr.get_locus ());
685 }
686
687 void
688 ExprStmtBuilder::visit (HIR::LetStmt &stmt)
689 {
690 tl::optional<PlaceId> init;
691 tl::optional<TyTy::BaseType *> type_annotation;
692
693 if (stmt.has_type ())
694 type_annotation = lookup_type (stmt.get_type ());
695
696 if (stmt.get_pattern ().get_pattern_type () == HIR::Pattern::IDENTIFIER)
697 {
698 // Only if a pattern is just an identifier, no destructuring is needed.
699 // Hoverer PatternBindingBuilder cannot change existing temporary
700 // (init expr is evaluated before pattern binding) into a
701 // variable, so it would emit extra assignment.
702 auto var = declare_variable (stmt.get_pattern ().get_mappings ());
703 if (stmt.has_type ())
704 push_user_type_ascription (var, lookup_type (stmt.get_type ()));
705
706 if (stmt.has_init_expr ())
707 std::ignore = visit_expr (stmt.get_init_expr (), var);
708 }
709 else
710 {
711 if (stmt.has_init_expr ())
712 init = visit_expr (stmt.get_init_expr ());
713
714 PatternBindingBuilder (ctx, init, type_annotation)
715 .go (stmt.get_pattern ());
716 }
717 }
718
719 void
720 ExprStmtBuilder::visit (HIR::ExprStmt &stmt)
721 {
722 PlaceId result = visit_expr (stmt.get_expr ());
723 // We must read the value for current liveness and we must not store it into
724 // the same place.
725 if (result != INVALID_PLACE)
726 push_tmp_assignment (result, stmt.get_locus ());
727 }
728 } // namespace BIR
729 } // namespace Rust