1use std::ops::Range;
4use std::{cmp, fmt, mem};
5
6use ena::undo_log::{Rollback, UndoLogs};
7use ena::unify as ut;
8use rustc_hash::FxHashMap;
9use rustc_index::IndexVec;
10use rustc_type_ir::inherent::IntoKind;
11use rustc_type_ir::{RegionKind, RegionVid, UniverseIndex};
12use tracing::{debug, instrument};
13
14use self::CombineMapType::*;
15use self::UndoLog::*;
16use super::MemberConstraint;
17use super::unify_key::RegionVidKey;
18use crate::next_solver::infer::snapshot::undo_log::{InferCtxtUndoLogs, Snapshot};
19use crate::next_solver::infer::unify_key::RegionVariableValue;
20use crate::next_solver::{AliasTy, Binder, DbInterner, ParamTy, PlaceholderTy, Region, Ty};
21
22#[derive(Debug, Clone, Default)]
23pub struct RegionConstraintStorage<'db> {
24 pub(super) var_infos: IndexVec<RegionVid, RegionVariableInfo>,
26
27 pub(super) data: RegionConstraintData<'db>,
28
29 lubs: CombineMap<'db>,
33
34 glbs: CombineMap<'db>,
38
39 pub(super) unification_table: ut::UnificationTableStorage<RegionVidKey<'db>>,
48
49 any_unifications: bool,
52}
53
54pub struct RegionConstraintCollector<'db, 'a> {
55 storage: &'a mut RegionConstraintStorage<'db>,
56 undo_log: &'a mut InferCtxtUndoLogs<'db>,
57}
58
59pub type VarInfos = IndexVec<RegionVid, RegionVariableInfo>;
60
61#[derive(Debug, Default, Clone)]
66pub struct RegionConstraintData<'db> {
67 pub constraints: Vec<Constraint<'db>>,
70
71 pub member_constraints: Vec<MemberConstraint<'db>>,
75
76 pub verifys: Vec<Verify<'db>>,
83}
84
85#[derive(Clone, PartialEq, Eq, Debug, Hash)]
87pub enum Constraint<'db> {
88 VarSubVar(RegionVid, RegionVid),
90
91 RegSubVar(Region<'db>, RegionVid),
93
94 VarSubReg(RegionVid, Region<'db>),
98
99 RegSubReg(Region<'db>, Region<'db>),
103}
104
105impl<'db> Constraint<'db> {
106 pub fn involves_placeholders(&self) -> bool {
107 match self {
108 Constraint::VarSubVar(_, _) => false,
109 Constraint::VarSubReg(_, r) | Constraint::RegSubVar(r, _) => r.is_placeholder(),
110 Constraint::RegSubReg(r, s) => r.is_placeholder() || s.is_placeholder(),
111 }
112 }
113}
114
115#[derive(Debug, Clone)]
116pub struct Verify<'db> {
117 pub kind: GenericKind<'db>,
118 pub region: Region<'db>,
119 pub bound: VerifyBound<'db>,
120}
121
122#[derive(Clone, PartialEq, Eq, Hash)]
123pub enum GenericKind<'db> {
124 Param(ParamTy),
125 Placeholder(PlaceholderTy),
126 Alias(AliasTy<'db>),
127}
128
129#[derive(Debug, Clone)]
147pub enum VerifyBound<'db> {
148 IfEq(Binder<'db, VerifyIfEq<'db>>),
150
151 OutlivedBy(Region<'db>),
162
163 IsEmpty,
165
166 AnyBound(Vec<VerifyBound<'db>>),
177
178 AllBounds(Vec<VerifyBound<'db>>),
190}
191
192#[derive(Debug, Clone)]
231pub struct VerifyIfEq<'db> {
232 pub ty: Ty<'db>,
234
235 pub bound: Region<'db>,
237}
238
239#[derive(Debug, Clone, PartialEq, Eq, Hash)]
240pub(crate) struct TwoRegions<'db> {
241 a: Region<'db>,
242 b: Region<'db>,
243}
244
245#[derive(Clone, PartialEq)]
246pub(crate) enum UndoLog<'db> {
247 AddVar(RegionVid),
249
250 AddConstraint(usize),
252
253 #[expect(dead_code, reason = "this is used in rustc")]
255 AddVerify(usize),
256
257 AddCombination(CombineMapType, TwoRegions<'db>),
259}
260
261#[derive(Clone, PartialEq)]
262pub(crate) enum CombineMapType {
263 Lub,
264 Glb,
265}
266
267type CombineMap<'db> = FxHashMap<TwoRegions<'db>, RegionVid>;
268
269#[derive(Debug, Clone)]
270pub struct RegionVariableInfo {
271 pub universe: UniverseIndex,
284}
285
286pub(crate) struct RegionSnapshot {
287 any_unifications: bool,
288}
289
290impl<'db> RegionConstraintStorage<'db> {
291 #[inline]
292 pub(crate) fn with_log<'a>(
293 &'a mut self,
294 undo_log: &'a mut InferCtxtUndoLogs<'db>,
295 ) -> RegionConstraintCollector<'db, 'a> {
296 RegionConstraintCollector { storage: self, undo_log }
297 }
298}
299
300impl<'db> RegionConstraintCollector<'db, '_> {
301 pub fn num_region_vars(&self) -> usize {
302 self.storage.var_infos.len()
303 }
304
305 pub fn region_constraint_data(&self) -> &RegionConstraintData<'db> {
306 &self.storage.data
307 }
308
309 pub fn take_and_reset_data(&mut self) -> RegionConstraintData<'db> {
322 assert!(!UndoLogs::<UndoLog<'db>>::in_snapshot(&self.undo_log));
323
324 let RegionConstraintStorage {
328 var_infos: _,
329 data,
330 lubs,
331 glbs,
332 unification_table: _,
333 any_unifications,
334 } = self.storage;
335
336 lubs.clear();
341 glbs.clear();
342
343 let data = mem::take(data);
344
345 if *any_unifications {
350 *any_unifications = false;
351 ut::UnificationTable::with_log(&mut self.storage.unification_table, &mut self.undo_log)
353 .reset_unifications(|key| RegionVariableValue::Unknown {
354 universe: self.storage.var_infos[key.vid].universe,
355 });
356 }
357
358 data
359 }
360
361 pub fn data(&self) -> &RegionConstraintData<'db> {
362 &self.storage.data
363 }
364
365 pub(super) fn start_snapshot(&self) -> RegionSnapshot {
366 debug!("RegionConstraintCollector: start_snapshot");
367 RegionSnapshot { any_unifications: self.storage.any_unifications }
368 }
369
370 pub(super) fn rollback_to(&mut self, snapshot: RegionSnapshot) {
371 debug!("RegionConstraintCollector: rollback_to({:?})", snapshot);
372 self.storage.any_unifications = snapshot.any_unifications;
373 }
374
375 pub(super) fn new_region_var(&mut self, universe: UniverseIndex) -> RegionVid {
376 let vid = self.storage.var_infos.push(RegionVariableInfo { universe });
377
378 let u_vid = self.unification_table_mut().new_key(RegionVariableValue::Unknown { universe });
379 assert_eq!(vid, u_vid.vid);
380 self.undo_log.push(AddVar(vid));
381 debug!("created new region variable {:?} in {:?}", vid, universe);
382 vid
383 }
384
385 fn add_constraint(&mut self, constraint: Constraint<'db>) {
386 debug!("RegionConstraintCollector: add_constraint({:?})", constraint);
388
389 let index = self.storage.data.constraints.len();
390 self.storage.data.constraints.push(constraint);
391 self.undo_log.push(AddConstraint(index));
392 }
393
394 pub(super) fn make_eqregion(&mut self, a: Region<'db>, b: Region<'db>) {
395 if a != b {
396 self.make_subregion(a, b);
399 self.make_subregion(b, a);
400
401 match (a.kind(), b.kind()) {
402 (RegionKind::ReVar(a), RegionKind::ReVar(b)) => {
403 debug!("make_eqregion: unifying {:?} with {:?}", a, b);
404 if self.unification_table_mut().unify_var_var(a, b).is_ok() {
405 self.storage.any_unifications = true;
406 }
407 }
408 (RegionKind::ReVar(vid), _) => {
409 debug!("make_eqregion: unifying {:?} with {:?}", vid, b);
410 if self
411 .unification_table_mut()
412 .unify_var_value(vid, RegionVariableValue::Known { value: b })
413 .is_ok()
414 {
415 self.storage.any_unifications = true;
416 };
417 }
418 (_, RegionKind::ReVar(vid)) => {
419 debug!("make_eqregion: unifying {:?} with {:?}", a, vid);
420 if self
421 .unification_table_mut()
422 .unify_var_value(vid, RegionVariableValue::Known { value: a })
423 .is_ok()
424 {
425 self.storage.any_unifications = true;
426 };
427 }
428 (_, _) => {}
429 }
430 }
431 }
432
433 #[instrument(skip(self), level = "debug")]
434 pub(super) fn make_subregion(&mut self, sub: Region<'db>, sup: Region<'db>) {
435 match (sub.kind(), sup.kind()) {
438 (RegionKind::ReBound(..), _) | (_, RegionKind::ReBound(..)) => {
439 panic!("cannot relate bound region: {sub:?} <= {sup:?}");
440 }
441 (_, RegionKind::ReStatic) => {
442 }
444 (RegionKind::ReVar(sub_id), RegionKind::ReVar(sup_id)) => {
445 self.add_constraint(Constraint::VarSubVar(sub_id, sup_id));
446 }
447 (_, RegionKind::ReVar(sup_id)) => {
448 self.add_constraint(Constraint::RegSubVar(sub, sup_id));
449 }
450 (RegionKind::ReVar(sub_id), _) => {
451 self.add_constraint(Constraint::VarSubReg(sub_id, sup));
452 }
453 _ => {
454 self.add_constraint(Constraint::RegSubReg(sub, sup));
455 }
456 }
457 }
458
459 pub(super) fn lub_regions(
460 &mut self,
461 db: DbInterner<'db>,
462 a: Region<'db>,
463 b: Region<'db>,
464 ) -> Region<'db> {
465 debug!("RegionConstraintCollector: lub_regions({:?}, {:?})", a, b);
467 #[expect(clippy::if_same_then_else)]
468 if a.is_static() || b.is_static() {
469 a } else if a == b {
471 a } else {
473 self.combine_vars(db, Lub, a, b)
474 }
475 }
476
477 pub(super) fn glb_regions(
478 &mut self,
479 db: DbInterner<'db>,
480 a: Region<'db>,
481 b: Region<'db>,
482 ) -> Region<'db> {
483 debug!("RegionConstraintCollector: glb_regions({:?}, {:?})", a, b);
485 #[expect(clippy::if_same_then_else)]
486 if a.is_static() {
487 b } else if b.is_static() {
489 a } else if a == b {
491 a } else {
493 self.combine_vars(db, Glb, a, b)
494 }
495 }
496
497 pub fn opportunistic_resolve_var(
500 &mut self,
501 cx: DbInterner<'db>,
502 vid: RegionVid,
503 ) -> Region<'db> {
504 let mut ut = self.unification_table_mut();
505 let root_vid = ut.find(vid).vid;
506 match ut.probe_value(root_vid) {
507 RegionVariableValue::Known { value } => value,
508 RegionVariableValue::Unknown { .. } => Region::new_var(cx, root_vid),
509 }
510 }
511
512 pub fn probe_value(&mut self, vid: RegionVid) -> Result<Region<'db>, UniverseIndex> {
513 match self.unification_table_mut().probe_value(vid) {
514 RegionVariableValue::Known { value } => Ok(value),
515 RegionVariableValue::Unknown { universe } => Err(universe),
516 }
517 }
518
519 fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'db> {
520 match t {
521 Glb => &mut self.storage.glbs,
522 Lub => &mut self.storage.lubs,
523 }
524 }
525
526 fn combine_vars(
527 &mut self,
528 cx: DbInterner<'db>,
529 t: CombineMapType,
530 a: Region<'db>,
531 b: Region<'db>,
532 ) -> Region<'db> {
533 let vars = TwoRegions { a, b };
534 if let Some(c) = self.combine_map(t.clone()).get(&vars) {
535 return Region::new_var(cx, *c);
536 }
537 let a_universe = self.universe(a);
538 let b_universe = self.universe(b);
539 let c_universe = cmp::max(a_universe, b_universe);
540 let c = self.new_region_var(c_universe);
541 self.combine_map(t.clone()).insert(vars.clone(), c);
542 self.undo_log.push(AddCombination(t.clone(), vars));
543 let new_r = Region::new_var(cx, c);
544 for old_r in [a, b] {
545 match t {
546 Glb => self.make_subregion(new_r, old_r),
547 Lub => self.make_subregion(old_r, new_r),
548 }
549 }
550 debug!("combine_vars() c={:?}", c);
551 new_r
552 }
553
554 pub fn universe(&mut self, region: Region<'db>) -> UniverseIndex {
555 match region.kind() {
556 RegionKind::ReStatic
557 | RegionKind::ReErased
558 | RegionKind::ReLateParam(..)
559 | RegionKind::ReEarlyParam(..)
560 | RegionKind::ReError(_) => UniverseIndex::ROOT,
561 RegionKind::RePlaceholder(placeholder) => placeholder.universe,
562 RegionKind::ReVar(vid) => match self.probe_value(vid) {
563 Ok(value) => self.universe(value),
564 Err(universe) => universe,
565 },
566 RegionKind::ReBound(..) => panic!("universe(): encountered bound region {region:?}"),
567 }
568 }
569
570 pub fn vars_since_snapshot(&self, value_count: usize) -> Range<RegionVid> {
571 RegionVid::from(value_count)..RegionVid::from(self.storage.unification_table.len())
572 }
573
574 pub fn region_constraints_added_in_snapshot(&self, mark: &Snapshot) -> bool {
576 self.undo_log
577 .region_constraints_in_snapshot(mark)
578 .any(|elt| matches!(elt, AddConstraint(_)))
579 }
580
581 #[inline]
582 fn unification_table_mut(&mut self) -> super::UnificationTable<'_, 'db, RegionVidKey<'db>> {
583 ut::UnificationTable::with_log(&mut self.storage.unification_table, self.undo_log)
584 }
585}
586
587impl fmt::Debug for RegionSnapshot {
588 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
589 write!(f, "RegionSnapshot")
590 }
591}
592
593impl<'db> fmt::Debug for GenericKind<'db> {
594 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
595 match *self {
596 GenericKind::Param(ref p) => write!(f, "{p:?}"),
597 GenericKind::Placeholder(ref p) => write!(f, "{p:?}"),
598 GenericKind::Alias(ref p) => write!(f, "{p:?}"),
599 }
600 }
601}
602
603impl<'db> fmt::Display for GenericKind<'db> {
604 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
605 match *self {
606 GenericKind::Param(ref p) => write!(f, "{p:?}"),
607 GenericKind::Placeholder(ref p) => write!(f, "{p:?}"),
608 GenericKind::Alias(ref p) => write!(f, "{p}"),
609 }
610 }
611}
612
613impl<'db> GenericKind<'db> {
614 pub fn to_ty(&self, interner: DbInterner<'db>) -> Ty<'db> {
615 match *self {
616 GenericKind::Param(ref p) => (*p).to_ty(interner),
617 GenericKind::Placeholder(ref p) => Ty::new_placeholder(interner, *p),
618 GenericKind::Alias(ref p) => (*p).to_ty(interner),
619 }
620 }
621}
622
623impl<'db> VerifyBound<'db> {
624 pub fn must_hold(&self) -> bool {
625 match self {
626 VerifyBound::IfEq(..) => false,
627 VerifyBound::OutlivedBy(re) => re.is_static(),
628 VerifyBound::IsEmpty => false,
629 VerifyBound::AnyBound(bs) => bs.iter().any(|b| b.must_hold()),
630 VerifyBound::AllBounds(bs) => bs.iter().all(|b| b.must_hold()),
631 }
632 }
633
634 pub fn cannot_hold(&self) -> bool {
635 match self {
636 VerifyBound::IfEq(..) => false,
637 VerifyBound::IsEmpty => false,
638 VerifyBound::OutlivedBy(_) => false,
639 VerifyBound::AnyBound(bs) => bs.iter().all(|b| b.cannot_hold()),
640 VerifyBound::AllBounds(bs) => bs.iter().any(|b| b.cannot_hold()),
641 }
642 }
643
644 pub fn or(self, vb: VerifyBound<'db>) -> VerifyBound<'db> {
645 if self.must_hold() || vb.cannot_hold() {
646 self
647 } else if self.cannot_hold() || vb.must_hold() {
648 vb
649 } else {
650 VerifyBound::AnyBound(vec![self, vb])
651 }
652 }
653}
654
655impl<'db> RegionConstraintData<'db> {
656 pub fn is_empty(&self) -> bool {
659 let RegionConstraintData { constraints, member_constraints, verifys } = self;
660 constraints.is_empty() && member_constraints.is_empty() && verifys.is_empty()
661 }
662}
663
664impl<'db> Rollback<UndoLog<'db>> for RegionConstraintStorage<'db> {
665 fn reverse(&mut self, undo: UndoLog<'db>) {
666 match undo {
667 AddVar(vid) => {
668 self.var_infos.pop().unwrap();
669 assert_eq!(self.var_infos.len(), vid.index());
670 }
671 AddConstraint(index) => {
672 self.data.constraints.pop().unwrap();
673 assert_eq!(self.data.constraints.len(), index);
674 }
675 AddVerify(index) => {
676 self.data.verifys.pop();
677 assert_eq!(self.data.verifys.len(), index);
678 }
679 AddCombination(Glb, ref regions) => {
680 self.glbs.remove(regions);
681 }
682 AddCombination(Lub, ref regions) => {
683 self.lubs.remove(regions);
684 }
685 }
686 }
687}