Debian Codegraph @

gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h

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 not see
17 // <http://www.gnu.org/licenses/>.
18
19 #ifndef RUST_BIR_FACT_COLLECTOR_H
20 #define RUST_BIR_FACT_COLLECTOR_H
21
22 #include "rust-bir-visitor.h"
23 #include "rust-bir.h"
24 #include "rust-bir-place.h"
25 #include "polonius/rust-polonius.h"
26
27 namespace Rust {
28 namespace BIR {
29
30 enum class PointPosition : uint8_t
31 {
32 START,
33 MID
34 };
35
36 class FactCollector : public Visitor
37 {
38 // Output.
39 Polonius::Facts facts;
40
41 // Read-only context.
42 const PlaceDB &place_db;
43 const BasicBlocks &basic_blocks;
44 const PlaceId first_local;
45 const location_t location;
46
47 Resolver::TypeCheckContext &tyctx;
48
49 // Collector state.
50 BasicBlockId current_bb = ENTRY_BASIC_BLOCK;
51 uint32_t current_stmt = 0;
52 PlaceId lhs = INVALID_PLACE;
53
54 // PlaceDB is const in this phase, so this is used to generate fresh regions.
55 FreeRegion next_fresh_region;
56 RegionBinder region_binder{next_fresh_region};
57
58 std::vector<Polonius::Point> cfg_points_all;
59
60 FreeRegions bind_regions (std::vector<TyTy::Region> regions,
61 FreeRegions parent_free_regions)
62 {
63 return region_binder.bind_regions (regions, parent_free_regions);
64 }
65
66 FreeRegions make_fresh_regions (size_t size)
67 {
68 FreeRegions free_regions;
69 for (size_t i = 0; i < size; i++)
70 free_regions.push_back (region_binder.get_next_free_region ());
71
72 return free_regions;
73 }
74
75 public:
76 static Polonius::Facts collect (Function &func)
77 {
78 FactCollector collector (func);
79 collector.init_universal_regions (func.universal_regions,
80 func.universal_region_bounds);
81
82 collector.visit_statemensts ();
83 collector.visit_places (func.arguments);
84
85 return std::move (collector.facts);
86 }
87
88 protected: // Constructor and destructor.
89 explicit FactCollector (Function &func)
90 : place_db (func.place_db), basic_blocks (func.basic_blocks),
91 first_local (func.arguments.empty ()
92 ? FIRST_VARIABLE_PLACE
93 : PlaceId{func.arguments.rbegin ()->value + 1}),
94 location (func.location), tyctx (*Resolver::TypeCheckContext::get ()),
95 next_fresh_region (place_db.peek_next_free_region ())
96 {}
97 ~FactCollector () = default;
98
99 protected: // Main collection entry points (for different categories).
100 void init_universal_regions (
101 const FreeRegions &universal_regions,
102 const decltype (Function::universal_region_bounds) &universal_region_bounds)
103 {
104 size_t next_loan = place_db.get_loans ().size ();
105 facts.universal_region.emplace_back (0);
106 facts.placeholder.emplace_back (0, next_loan++);
107
108 for (auto &region : universal_regions)
109 {
110 facts.universal_region.emplace_back (region.value);
111 facts.placeholder.emplace_back (region.value, next_loan++);
112 facts.known_placeholder_subset.emplace_back (0, region.value);
113 }
114
115 // Copy already collected subset facts, that are universally valid.
116 for (auto &bound : universal_region_bounds)
117 facts.known_placeholder_subset.emplace_back (bound.first.value,
118 bound.second.value);
119 }
120
121 void visit_places (const std::vector<PlaceId> &args)
122 {
123 for (PlaceId place_id = INVALID_PLACE; place_id.value < place_db.size ();
124 ++place_id.value)
125 {
126 auto &place = place_db[place_id];
127
128 switch (place.kind)
129 {
130 case Place::VARIABLE:
131 case Place::TEMPORARY:
132 facts.path_is_var.emplace_back (place_id.value, place_id.value);
133 for (auto &region : place.regions)
134 facts.use_of_var_derefs_origin.emplace_back (place_id.value,
135 region.value);
136
137 // TODO: drop_of_var_derefs_origin
138 break;
139 case Place::FIELD:
140 sanizite_field (place_id);
141 facts.child_path.emplace_back (place_id.value,
142 place.path.parent.value);
143 break;
144 case Place::INDEX:
145 push_subset_all (place.tyty, place.regions,
146 place_db[place.path.parent].regions);
147 facts.child_path.emplace_back (place_id.value,
148 place.path.parent.value);
149 break;
150 case Place::DEREF:
151 sanitize_deref (place_id);
152 facts.child_path.emplace_back (place_id.value,
153 place.path.parent.value);
154 break;
155 case Place::CONSTANT:
156 case Place::INVALID:
157 break;
158 }
159 }
160
161 for (PlaceId arg = PlaceId{FIRST_VARIABLE_PLACE.value + 1};
162 arg < first_local; ++arg.value)
163 facts.path_assigned_at_base.emplace_back (
164 arg.value, get_point (ENTRY_BASIC_BLOCK, 0, PointPosition::START));
165
166 for (PlaceId place = first_local; place.value < place_db.size ();
167 ++place.value)
168 {
169 if (place_db[place].is_var ())
170 facts.path_moved_at_base.emplace_back (
171 place.value,
172 get_point (ENTRY_BASIC_BLOCK, 0, PointPosition::START));
173 }
174 }
175
176 void sanitize_deref (PlaceId place_id)
177 {
178 auto &place = place_db[place_id];
179 auto &base = place_db[place.path.parent];
180
181 rust_debug ("\tSanitize deref of %s", base.tyty->as_string ().c_str ());
182
183 FreeRegions regions;
184 for (auto it = base.regions.begin () + 1; it != base.regions.end (); ++it)
185 {
186 regions.push_back (*it);
187 }
188 push_subset_all (place.tyty, regions, place.regions);
189 }
190 void sanizite_field (PlaceId place_id)
191 {
192 auto &place = place_db[place_id];
193 auto &base = place_db[place.path.parent];
194
195 rust_debug ("\tSanitize field .%d of %s", place.variable_or_field_index,
196 base.tyty->as_string ().c_str ());
197
198 if (base.tyty->is<TyTy::TupleType> ())
199 return;
200 auto r = Resolver::TypeCheckContext::get ()
201 ->get_variance_analysis_ctx ()
202 .query_field_regions (base.tyty->as<TyTy::ADTType> (), 0,
203 place.variable_or_field_index,
204 base.regions); // FIXME
205 push_subset_all (place.tyty, r, place.regions);
206 }
207
208 void visit_statemensts ()
209 {
210 rust_debug ("visit_statemensts");
211
212 for (current_bb = ENTRY_BASIC_BLOCK;
213 current_bb.value < basic_blocks.size (); ++current_bb.value)
214 {
215 auto &bb = basic_blocks[current_bb];
216 for (current_stmt = 0; current_stmt < bb.statements.size ();
217 ++current_stmt)
218 {
219 cfg_points_all.push_back (get_current_point_start ());
220 cfg_points_all.push_back (get_current_point_mid ());
221
222 add_stmt_to_cfg (current_bb, current_stmt);
223
224 visit (bb.statements[current_stmt]);
225 }
226 }
227 current_bb = ENTRY_BASIC_BLOCK;
228 current_stmt = 0;
229 }
230
231 void visit (const Statement &stmt) override
232 {
233 switch (stmt.get_kind ())
234 {
235 case Statement::Kind::ASSIGNMENT: {
236 // TODO: for unwind, must had hadning for non-panic-only assignements
237 issue_write_deep (stmt.get_place ());
238 visit_assignment_expr (stmt.get_place (), stmt.get_expr ());
239 break;
240 }
241 case Statement::Kind::SWITCH: {
242 issue_read_move (stmt.get_place ());
243 issue_jumps ();
244 }
245 break;
246 case Statement::Kind::GOTO: {
247 issue_jumps ();
248 }
249 break;
250 case Statement::Kind::RETURN: {
251 issue_place_access (RETURN_VALUE_PLACE);
252 issue_locals_dealloc ();
253 break;
254 }
255 case Statement::Kind::STORAGE_DEAD: {
256 facts.path_moved_at_base.emplace_back (stmt.get_place ().value,
257 get_current_point_mid ());
258 facts.var_defined_at.emplace_back (stmt.get_place ().value,
259 get_current_point_mid ());
260 break;
261 }
262 case Statement::Kind::STORAGE_LIVE: {
263 issue_write_deep (stmt.get_place (), true);
264 break;
265 }
266 case Statement::Kind::USER_TYPE_ASCRIPTION: {
267 issue_user_type_constraints (stmt.get_place (), stmt.get_type ());
268 break;
269 }
270 case Statement::Kind::FAKE_READ: {
271 issue_place_access (stmt.get_place ());
272 break;
273 }
274 }
275 }
276
277 void visit_assignment_expr (PlaceId lhs, AbstractExpr &expr)
278 {
279 this->lhs = lhs;
280 expr.accept_vis (*this);
281 this->lhs = INVALID_PLACE;
282 }
283
284 void visit (const InitializerExpr &expr) override
285 {
286 sanitize_constrains_at_init (lhs);
287
288 for (auto init_value : expr.get_values ())
289 issue_read_move (init_value);
290 }
291
292 void visit (const Operator<1> &expr) override
293 {
294 sanitize_constrains_at_init (lhs);
295 issue_read_move (expr.get_operand<0> ());
296 }
297
298 void visit (const Operator<2> &expr) override
299 {
300 sanitize_constrains_at_init (lhs);
301 issue_read_move (expr.get_operand<0> ());
302 issue_read_move (expr.get_operand<1> ());
303 }
304
305 void visit (const BorrowExpr &expr) override
306 {
307 rust_debug ("\t_%u = BorrowExpr(_%u)", lhs.value - 1,
308 expr.get_place ().value - 1);
309
310 auto loan = place_db.get_loan (expr.get_loan_id ());
311
312 auto &base_place = place_db[expr.get_place ()];
313 auto &ref_place = place_db[lhs];
314
315 issue_place_access (expr.get_place ());
316
317 // See compiler/rustc_borrowck/src/type_check/mod.rs:add_reborrow_constraint
318 if (base_place.kind == Place::DEREF)
319 {
320 // Reborrow
321
322 auto &main_loan_place = place_db[base_place.path.parent];
323 if (loan.mutability == Mutability::Mut)
324 {
325 if (!main_loan_place.tyty->as<TyTy::ReferenceType> ()
326 ->is_mutable ())
327 rust_error_at (location,
328 "Cannot reborrow immutable borrow as mutable");
329 issue_loan (expr.get_origin (), expr.get_loan_id ());
330 }
331
332 push_subset (main_loan_place.regions[0], {expr.get_origin ()});
333 }
334 else
335 {
336 issue_loan (expr.get_origin (), expr.get_loan_id ());
337 }
338
339 auto loan_regions = base_place.regions.prepend ({expr.get_origin ()});
340 push_subset (ref_place.tyty, loan_regions, ref_place.regions);
341 }
342
343 void visit (const Assignment &expr) override
344 {
345 rust_debug ("\t_%u = Assignment(_%u) at %u:%u", lhs.value - 1,
346 expr.get_rhs ().value - 1, current_bb.value, current_stmt);
347
348 issue_read_move (expr.get_rhs ());
349 push_place_subset (lhs, expr.get_rhs ());
350 }
351
352 void visit (const CallExpr &expr) override
353 {
354 rust_debug ("\t_%u = CallExpr(_%u)", lhs.value - 1,
355 expr.get_callable ().value - 1);
356
357 auto &return_place = place_db[lhs];
358 auto &callable_place = place_db[expr.get_callable ()];
359 auto callable_ty = callable_place.tyty->as<TyTy::CallableTypeInterface> ();
360
361 issue_read_move (expr.get_callable ());
362
363 // Each call needs unique regions.
364 auto call_regions = make_fresh_regions (callable_place.regions.size ());
365
366 for (size_t i = 0; i < expr.get_arguments ().size (); ++i)
367 {
368 auto arg = expr.get_arguments ().at (i);
369 auto arg_regions
370 = bind_regions (Resolver::TypeCheckContext::get ()
371 ->get_variance_analysis_ctx ()
372 .query_type_regions (
373 callable_ty->get_param_type_at (i)),
374 call_regions);
375 issue_read_move (arg);
376 push_subset (place_db[arg].tyty, place_db[arg].regions, arg_regions);
377 }
378
379 // sanitize return regions
380 sanitize_constrains_at_init (lhs);
381
382 auto return_regions
383 = bind_regions (Resolver::TypeCheckContext::get ()
384 ->get_variance_analysis_ctx ()
385 .query_type_regions (
386 callable_ty->as<TyTy::FnType> ()->get_return_type ()),
387 call_regions);
388 push_subset (return_place.tyty, return_regions, return_place.regions);
389
390 issue_jumps ();
391 }
392
393 protected: // Statement visitor helpers
394 WARN_UNUSED_RESULT const BasicBlock &get_current_bb () const
395 {
396 return basic_blocks[current_bb];
397 }
398
399 WARN_UNUSED_RESULT static Polonius::Point
400 get_point (BasicBlockId bb, uint32_t stmt, PointPosition pos)
401 {
402 Polonius::Point point = 0;
403 point |= (bb.value << 16);
404 point |= (stmt << 1);
405 point |= (static_cast<uint8_t> (pos) & 1);
406 return point;
407 }
408
409 WARN_UNUSED_RESULT Polonius::Point get_current_point_start () const
410 {
411 return get_point (current_bb, current_stmt, PointPosition::START);
412 }
413
414 WARN_UNUSED_RESULT Polonius::Point get_current_point_mid () const
415 {
416 return get_point (current_bb, current_stmt, PointPosition::MID);
417 }
418
419 void add_stmt_to_cfg (BasicBlockId bb, uint32_t stmt)
420 {
421 if (stmt != 0)
422 {
423 facts.cfg_edge.emplace_back (get_point (bb, stmt - 1,
424 PointPosition::MID),
425 get_point (bb, stmt,
426 PointPosition::START));
427 }
428
429 facts.cfg_edge.emplace_back (get_point (bb, stmt, PointPosition::START),
430 get_point (bb, stmt, PointPosition::MID));
431 }
432
433 protected: // Generic BIR operations.
434 void issue_jumps ()
435 {
436 for (auto succ : get_current_bb ().successors)
437 facts.cfg_edge.emplace_back (get_current_point_mid (),
438 get_point (succ, 0, PointPosition::START));
439 }
440
441 /* Shallow r/w access */
442 void issue_place_access (PlaceId place_id)
443 {
444 auto &place = place_db[place_id];
445
446 if (place.is_constant ())
447 return;
448
449 if (place_id != RETURN_VALUE_PLACE)
450 facts.path_accessed_at_base.emplace_back (place_id.value,
451 get_current_point_mid ());
452
453 if (place.is_var ())
454 facts.var_used_at.emplace_back (place_id.value, get_current_point_mid ());
455 else if (place.is_path ())
456 {
457 facts.var_used_at.emplace_back (place_db.get_var (place_id).value,
458 get_current_point_mid ());
459 }
460 }
461
462 /** Deep read access, which consumes the place. */
463 void issue_read_move (PlaceId place_id)
464 {
465 auto &place = place_db[place_id];
466
467 issue_place_access (place_id);
468 if (place.should_be_moved ())
469 {
470 issue_move (place_id);
471 }
472 else
473 {
474 check_read_for_conflicts (place_id);
475 }
476 }
477
478 void issue_write_deep (PlaceId place_id, bool is_init = false)
479 {
480 auto &place = place_db[place_id];
481 rust_assert (place.is_lvalue () || place.is_rvalue ());
482
483 if (place.is_var ())
484 facts.var_defined_at.emplace_back (place_id.value,
485 get_current_point_mid ());
486
487 if (!is_init)
488 {
489 facts.path_assigned_at_base.emplace_back (place_id.value,
490 get_current_point_mid ());
491 check_write_for_conflict (place_id);
492 kill_borrows_for_place (place_id);
493 }
494 }
495
496 void issue_move (PlaceId place_id, bool initial = false)
497 {
498 if (!place_db[place_id].should_be_moved ())
499 return;
500
501 facts.path_moved_at_base.emplace_back (place_id.value,
502 initial
503 ? get_point (ENTRY_BASIC_BLOCK, 0,
504 PointPosition::START)
505 : get_current_point_mid ());
506
507 check_move_behind_reference (place_id);
508
509 if (!initial)
510 {
511 check_write_for_conflict (place_id);
512 kill_borrows_for_place (place_id);
513 }
514 }
515
516 void issue_loan (Polonius::Origin origin, LoanId loan_id)
517 {
518 facts.loan_issued_at.emplace_back (origin, loan_id.value,
519 get_current_point_mid ());
520
521 check_for_borrow_conficts (place_db.get_loan (loan_id).place, loan_id,
522 place_db.get_loan (loan_id).mutability);
523 }
524
525 void issue_locals_dealloc ()
526 {
527 for (LoanId loan_id = {0}; loan_id.value < place_db.get_loans ().size ();
528 ++loan_id.value)
529 {
530 auto &loan = place_db.get_loan (loan_id);
531 auto loaned_var_id = place_db.get_var (loan.place);
532 if (place_db[loaned_var_id].tyty->is<TyTy::ReferenceType> ())
533 continue;
534 if (loaned_var_id >= first_local)
535 facts.loan_invalidated_at.emplace_back (get_current_point_start (),
536 loan_id.value);
537 }
538 }
539
540 void issue_user_type_constraints (PlaceId place_id, TyTy::BaseType *type)
541 {
542 auto user_regions = Resolver::TypeCheckContext::get ()
543 ->get_variance_analysis_ctx ()
544 .query_type_regions (type);
545 push_subset_user (place_db[place_id].tyty, place_db[place_id].regions,
546 user_regions);
547 }
548
549 void check_read_for_conflicts (PlaceId place_id)
550 {
551 place_db.for_each_path_segment (place_id, [&] (PlaceId id) {
552 for (auto loan : place_db[id].borrowed_by)
553 {
554 if (place_db.get_loan (loan).mutability == Mutability::Mut)
555 {
556 facts.loan_invalidated_at.emplace_back (
557 get_current_point_start (), loan.value);
558 }
559 }
560 });
561 place_db.for_each_path_from_root (place_id, [&] (PlaceId id) {
562 for (auto loan : place_db[id].borrowed_by)
563 {
564 if (place_db.get_loan (loan).mutability == Mutability::Mut)
565 {
566 facts.loan_invalidated_at.emplace_back (
567 get_current_point_start (), loan.value);
568 }
569 }
570 });
571 }
572
573 void check_write_for_conflict (PlaceId place_id)
574 {
575 place_db.for_each_path_segment (place_id, [&] (PlaceId id) {
576 for (auto loan : place_db[id].borrowed_by)
577 facts.loan_invalidated_at.emplace_back (get_current_point_start (),
578 loan.value);
579 });
580 place_db.for_each_path_from_root (place_id, [&] (PlaceId id) {
581 for (auto loan : place_db[id].borrowed_by)
582 facts.loan_invalidated_at.emplace_back (get_current_point_start (),
583 loan.value);
584 });
585 }
586
587 void check_for_borrow_conficts (PlaceId place_id, LoanId loan,
588 Mutability mutability)
589 {
590 place_db.for_each_path_segment (place_id, [&] (PlaceId id) {
591 for (auto other_loan : place_db[id].borrowed_by)
592 {
593 if (mutability == Mutability::Imm
594 && place_db.get_loan (other_loan).mutability == Mutability::Imm)
595 continue;
596 else
597 facts.loan_invalidated_at.emplace_back (get_current_point_start (),
598 other_loan.value);
599 }
600 });
601
602 place_db.for_each_path_from_root (place_id, [&] (PlaceId id) {
603 for (auto other_loan : place_db[id].borrowed_by)
604 {
605 if (mutability == Mutability::Imm
606 && place_db.get_loan (other_loan).mutability == Mutability::Imm)
607 continue;
608 else
609 facts.loan_invalidated_at.emplace_back (get_current_point_start (),
610 other_loan.value);
611 }
612 });
613 }
614
615 void check_move_behind_reference (PlaceId place_id)
616 {
617 place_db.for_each_path_segment (place_id, [&] (PlaceId id) {
618 if (id == place_id)
619 return;
620 if (place_db[id].kind == Place::DEREF)
621 rust_error_at (location, "Cannot move from behind a reference.");
622 });
623 }
624
625 void kill_borrows_for_place (PlaceId place_id)
626 {
627 auto &place = place_db[place_id];
628 for (auto loan : place.borrowed_by)
629 {
630 // TODO: this is more complicated, see
631 // compiler/rustc_borrowck/src/constraint_generation.rs:176
632 facts.loan_killed_at.emplace_back (loan.value,
633 get_current_point_mid ());
634 }
635 }
636
637 protected: // Subset helpers.
638 void push_subset (FreeRegion lhs, FreeRegion rhs)
639 {
640 rust_debug ("\t\tpush_subset: '?%lu: '?%lu", (unsigned long) lhs.value,
641 (unsigned long) rhs.value);
642
643 facts.subset_base.emplace_back (lhs.value, rhs.value,
644 get_current_point_mid ());
645 }
646
647 void push_subset_all (FreeRegion lhs, FreeRegion rhs)
648 {
649 rust_debug ("\t\tpush_subset_all: '?%lu: '?%lu", (unsigned long) lhs.value,
650 (unsigned long) rhs.value);
651
652 for (auto point : cfg_points_all)
653 facts.subset_base.emplace_back (lhs.value, rhs.value, point);
654 }
655
656 void push_subset (Variance variance, FreeRegion lhs, FreeRegion rhs)
657 {
658 if (variance.is_covariant ())
659 push_subset (lhs, rhs);
660 else if (variance.is_contravariant ())
661 push_subset (rhs, lhs);
662 else if (variance.is_invariant ())
663 {
664 push_subset (lhs, rhs);
665 push_subset (rhs, lhs);
666 }
667 }
668
669 void push_subset_all (Variance variance, FreeRegion lhs, FreeRegion rhs)
670 {
671 if (variance.is_covariant ())
672 push_subset_all (lhs, rhs);
673 else if (variance.is_contravariant ())
674 push_subset_all (rhs, lhs);
675 else if (variance.is_invariant ())
676 {
677 push_subset_all (lhs, rhs);
678 push_subset_all (rhs, lhs);
679 }
680 }
681
682 void push_place_subset (PlaceId lhs, PlaceId rhs)
683 {
684 auto &lhs_place = place_db[lhs];
685 auto &rhs_place = place_db[rhs];
686
687 push_subset (lhs_place.tyty, rhs_place.regions, lhs_place.regions);
688 }
689
690 void push_subset (TyTy::BaseType *type, FreeRegions lhs, FreeRegions rhs)
691 {
692 auto variances = Resolver::TypeCheckContext::get ()
693 ->get_variance_analysis_ctx ()
694 .query_type_variances (type);
695 rust_assert (lhs.size () == rhs.size ());
696 rust_assert (lhs.size () == variances.size ());
697 for (size_t i = 0; i < lhs.size (); ++i)
698 push_subset (variances[i], lhs[i], rhs[i]);
699 }
700
701 void push_subset_all (TyTy::BaseType *type, FreeRegions lhs, FreeRegions rhs)
702 {
703 auto variances = Resolver::TypeCheckContext::get ()
704 ->get_variance_analysis_ctx ()
705 .query_type_variances (type);
706 rust_assert (lhs.size () == rhs.size ());
707 rust_assert (lhs.size () == variances.size ());
708 for (size_t i = 0; i < lhs.size (); ++i)
709 push_subset_all (variances[i], lhs[i], rhs[i]);
710 }
711
712 void push_subset_user (TyTy::BaseType *type, FreeRegions free_regions,
713 std::vector<TyTy::Region> user_regions)
714 {
715 auto variances = Resolver::TypeCheckContext::get ()
716 ->get_variance_analysis_ctx ()
717 .query_type_variances (type);
718 rust_assert (free_regions.size () == user_regions.size ());
719 rust_assert (free_regions.size () == variances.size ());
720
721 for (size_t i = 0; i < free_regions.size (); ++i)
722 {
723 if (user_regions[i].is_named ())
724 push_subset (variances[i], free_regions[i],
725 {Polonius::Origin (user_regions[i].get_index ())});
726 else if (user_regions[i].is_anonymous ())
727 {
728 // IGNORE
729 }
730 else
731 rust_internal_error_at (UNKNOWN_LOCATION, "Unexpected region type");
732 }
733 }
734
735 /**
736 * Apply type and lifetime bounds
737 *
738 * For a place we have a list of fresh regions. We need to apply constraints
739 * from type definition to it. First `n` regions belong to the lifetime
740 * parameters of the type. The rest are flatten lifetime parameters of the
741 * type arguments. We walk the type arguments with a offset
742 */
743 void sanitize_constrains_at_init (PlaceId place_id)
744 {
745 auto &place = place_db[place_id];
746
747 rust_debug ("\tSanitize constraints of %s",
748 place.tyty->as_string ().c_str ());
749
750 if (auto generic = place.tyty->try_as<TyTy::SubstitutionRef> ())
751 {
752 auto &regions = place.regions;
753 auto region_end = sanitize_constraints (*generic, 0, regions);
754 rust_assert (region_end == regions.size ());
755 }
756 else if (place.tyty->is<TyTy::ReferenceType> ())
757 {
758 for (auto &region : place.regions)
759 {
760 if (region != place.regions[0])
761 push_subset (region, place.regions[0]);
762 }
763 }
764 }
765
766 size_t sanitize_constraints (const TyTy::BaseType *type, size_t region_start,
767 const FreeRegions &regions)
768 {
769 switch (type->get_kind ())
770 {
771 case TyTy::ADT:
772 return sanitize_constraints (type->as<const TyTy::ADTType> (),
773 region_start, regions);
774 case TyTy::STR:
775 return region_start;
776 case TyTy::REF:
777 return 1
778 + sanitize_constraints (
779 type->as<const TyTy::ReferenceType> ()->get_base (),
780 region_start, regions);
781 case TyTy::POINTER:
782 return sanitize_constraints (
783 type->as<const TyTy::PointerType> ()->get_base (), region_start,
784 regions);
785 case TyTy::ARRAY:
786 return sanitize_constraints (
787 type->as<const TyTy::ArrayType> ()->get_element_type (), region_start,
788 regions);
789 case TyTy::SLICE:
790 return sanitize_constraints (
791 type->as<const TyTy::SliceType> ()->get_element_type (), region_start,
792 regions);
793 case TyTy::FNDEF:
794 case TyTy::TUPLE: {
795 for (auto &field : type->as<const TyTy::TupleType> ()->get_fields ())
796 sanitize_constraints (field.get_tyty (), region_start, regions);
797 }
798 break;
799 case TyTy::FNPTR:
800 case TyTy::PROJECTION:
801 return sanitize_constraints (*type->as<const TyTy::SubstitutionRef> (),
802 region_start, regions);
803 case TyTy::BOOL:
804 case TyTy::CHAR:
805 case TyTy::INT:
806 case TyTy::UINT:
807 case TyTy::FLOAT:
808 case TyTy::USIZE:
809 case TyTy::ISIZE:
810 case TyTy::NEVER:
811 case TyTy::DYNAMIC:
812 case TyTy::CLOSURE:
813 case TyTy::ERROR:
814 return region_start;
815 case TyTy::PLACEHOLDER:
816 case TyTy::INFER:
817 case TyTy::PARAM:
818 case TyTy::OPAQUE:
819 rust_unreachable ();
820 }
821 rust_unreachable ();
822 }
823
824 size_t sanitize_constraints (const TyTy::SubstitutionRef &type,
825 size_t region_start, const FreeRegions &regions)
826 {
827 for (auto constr : type.get_region_constraints ().region_region)
828 {
829 rust_assert (constr.first.is_early_bound ());
830 rust_assert (constr.second.is_early_bound ());
831 auto lhs = constr.first.get_index () + region_start;
832 auto rhs = constr.second.get_index () + region_start;
833 push_subset (regions[lhs], regions[rhs]);
834 }
835
836 size_t region_end = region_start + type.get_num_lifetime_params ();
837
838 /*
839 * For type `Foo<'a, T1, T2>`, where `T1 = &'b Vec<&'c i32>` and `T2 = &'d
840 * i32 the regions are `['a, 'b, 'c, 'd]`. The ranges
841 */
842 std::vector<size_t> type_param_region_ranges;
843 type_param_region_ranges.push_back (region_end);
844
845 for (auto type_param : type.get_substs ())
846 {
847 TyTy::SubstitutionArg arg = TyTy::SubstitutionArg::error ();
848 bool ok = type.get_used_arguments ().get_argument_for_symbol (
849 type_param.get_param_ty (), &arg);
850 rust_assert (ok);
851 region_end
852 = sanitize_constraints (arg.get_tyty (), region_end, regions);
853 type_param_region_ranges.push_back (region_end);
854 }
855
856 /*
857 * For constrain of form: `T: 'a` push outlives with all in range
858 * `indexof(T)..(indexof(T) + 1)`
859 */
860 for (auto constr : type.get_region_constraints ().type_region)
861 {
862 auto type_param_index_opt
863 = type.get_used_arguments ().find_symbol (*constr.first);
864 rust_assert (type_param_index_opt.has_value ());
865 size_t type_param_index = type_param_index_opt.value ();
866
867 for (size_t i = type_param_region_ranges[type_param_index];
868 i < type_param_region_ranges[type_param_index + 1]; ++i)
869 {
870 push_subset (regions[i],
871 regions[constr.second.get_index () + region_start]);
872 }
873 }
874
875 return region_end;
876 }
877 }; // namespace BIR
878
879 } // namespace BIR
880 } // namespace Rust
881
882 #endif // RUST_BIR_FACT_COLLECTOR_H