flux_middle/rty/
canonicalize.rs

1//! A canonical type is a type where all [existentials] and [constraint predicates] are *hoisted* to
2//! the top level. For example, the canonical version of `∃a. {∃b. i32[a + b] | b > 0}` is
3//! `∃a,b. {i32[a + b] | b > 0}`.
4//!
5//! Type constructors introduce scopes that can limit the hoisting. For instance, it is generally
6//! not permitted to hoist an existential out of a generic argument. For example, in `Vec<∃v. i32[v]>`
7//! the existential inside the `Vec` cannot be hoisted out.
8//!
9//! However, some type constructors are more "lenient" with respect to hoisting. Consider the tuple
10//! `(∃a. i32[a], ∃b. i32[b])`. Hoisting the existentials results in `∃a,b. (i32[a], i32[b])` which
11//! is an equivalent type (in the sense that subtyping holds both ways). The same applies to shared
12//! references: `&∃a. i32[a]` is equivalent to `∃a. &i32[a]`. We refer to this class of type
13//! constructors as *transparent*. Hoisting existential out of transparent type constructors is useful
14//! as it allows the logical information to be extracted from the type.
15//!
16//! And important case is mutable references. In some situations, it is sound to hoist out of mutable
17//! references. For example, if we have a variable in the environment of type `&mut ∃v. T[v]`, it is
18//! sound to treat it as `&mut T[a]` for a freshly generated `a` (assuming the lifetime of the
19//! reference is alive). However, this may result in a type that is *too specific* because the index
20//! `a` cannot be updated anymore.
21//!
22//! By default, we do *shallow* hoisting, i.e., we stop at the first type constructor. This is enough
23//! for cases where we need to inspect a type structurally one level. The amount of hoisting can be
24//! controlled by configuring the [`Hoister`] struct.
25//!
26//! It's also important to note that canonizalization doesn't imply any form of semantic equality
27//! and it is just a best effort to facilitate syntactic manipulation. For example, the types
28//! `∃a,b. (i32[a], i32[b])` and `∃a,b. (i32[b], i32[a])` are semantically equal but hoisting won't
29//! account for it.
30//!
31//! [existentials]: TyKind::Exists
32//! [constraint predicates]: TyKind::Constr
33use std::fmt::Write;
34
35use flux_arc_interner::List;
36use flux_macros::{TypeFoldable, TypeVisitable};
37use itertools::Itertools;
38use rustc_ast::Mutability;
39use rustc_span::Symbol;
40use rustc_type_ir::{BoundVar, INNERMOST};
41
42use super::{
43    BaseTy, Binder, BoundVariableKind, Expr, FnSig, GenericArg, GenericArgsExt, PolyFnSig,
44    SubsetTy, Ty, TyCtor, TyKind, TyOrBase,
45    fold::{TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable},
46};
47use crate::rty::{BoundReftKind, ExprKind, HoleKind};
48
49/// The [`Hoister`] struct is responsible for hoisting existentials and predicates out of a type.
50/// It can be configured to stop hoisting at specific type constructors.
51///
52/// The struct is generic on a delegate `D` because we use it to do *local* hoisting, keeping
53/// variables bound with a [`Binder`], and for *freeing* variables into the refinement context.
54// Should we use a builder for this?
55pub struct Hoister<D> {
56    pub delegate: D,
57    in_boxes: bool,
58    in_downcast: bool,
59    in_mut_refs: bool,
60    in_shr_refs: bool,
61    in_strg_refs: bool,
62    in_tuples: bool,
63    existentials: bool,
64    slices: bool,
65}
66
67pub trait HoisterDelegate {
68    fn hoist_exists(&mut self, ty_ctor: &TyCtor) -> Ty;
69    fn hoist_constr(&mut self, pred: Expr);
70}
71
72impl<D> Hoister<D> {
73    pub fn with_delegate(delegate: D) -> Self {
74        Hoister {
75            delegate,
76            in_tuples: false,
77            in_shr_refs: false,
78            in_mut_refs: false,
79            in_strg_refs: false,
80            in_boxes: false,
81            in_downcast: false,
82            existentials: true,
83            slices: false,
84        }
85    }
86
87    pub fn hoist_inside_shr_refs(mut self, shr_refs: bool) -> Self {
88        self.in_shr_refs = shr_refs;
89        self
90    }
91
92    pub fn hoist_inside_mut_refs(mut self, mut_refs: bool) -> Self {
93        self.in_mut_refs = mut_refs;
94        self
95    }
96
97    pub fn hoist_inside_strg_refs(mut self, strg_refs: bool) -> Self {
98        self.in_strg_refs = strg_refs;
99        self
100    }
101
102    pub fn hoist_inside_tuples(mut self, tuples: bool) -> Self {
103        self.in_tuples = tuples;
104        self
105    }
106
107    pub fn hoist_inside_boxes(mut self, boxes: bool) -> Self {
108        self.in_boxes = boxes;
109        self
110    }
111
112    pub fn hoist_inside_downcast(mut self, downcast: bool) -> Self {
113        self.in_downcast = downcast;
114        self
115    }
116
117    pub fn hoist_existentials(mut self, exists: bool) -> Self {
118        self.existentials = exists;
119        self
120    }
121
122    pub fn hoist_slices(mut self, slices: bool) -> Self {
123        self.slices = slices;
124        self
125    }
126
127    pub fn transparent(self) -> Self {
128        self.hoist_inside_boxes(true)
129            .hoist_inside_downcast(true)
130            .hoist_inside_mut_refs(false)
131            .hoist_inside_shr_refs(true)
132            .hoist_inside_strg_refs(true)
133            .hoist_inside_tuples(true)
134            .hoist_slices(true)
135    }
136
137    pub fn shallow(self) -> Self {
138        self.hoist_inside_boxes(false)
139            .hoist_inside_downcast(false)
140            .hoist_inside_mut_refs(false)
141            .hoist_inside_shr_refs(false)
142            .hoist_inside_strg_refs(false)
143            .hoist_inside_tuples(false)
144    }
145}
146
147impl<D: HoisterDelegate> Hoister<D> {
148    pub fn hoist(&mut self, ty: &Ty) -> Ty {
149        ty.fold_with(self)
150    }
151}
152
153/// Is `ty` of the form `&m (&m ... (&m T))` where `T` is an exi-indexed slice?
154/// We need to do a "transitive" check to deal with cases like `&mut &mut [i32]`
155/// which arise from closures like that in `tests/tests/pos/surface/closure03.rs`.
156fn is_indexed_slice(ty: &Ty) -> bool {
157    let Some(bty) = ty.as_bty_skipping_existentials() else {
158        return false;
159    };
160    match bty {
161        BaseTy::Slice(_) => true,
162        BaseTy::Ref(_, ty, _) => is_indexed_slice(ty),
163        _ => false,
164    }
165}
166
167impl<D: HoisterDelegate> TypeFolder for Hoister<D> {
168    fn fold_ty(&mut self, ty: &Ty) -> Ty {
169        match ty.kind() {
170            TyKind::Indexed(bty, idx) => Ty::indexed(bty.fold_with(self), idx.clone()),
171            TyKind::Exists(ty_ctor) if self.existentials => {
172                // Avoid hoisting useless parameters for unit sorts. This is important for
173                // canonicalization because we assume mutable references won't be under a
174                // binder after we canonicalize them.
175                // FIXME(nilehmann) this same logic is repeated in a couple of places, e.g.,
176                // TyCtor::to_ty
177                match &ty_ctor.vars()[..] {
178                    [BoundVariableKind::Refine(sort, ..)] => {
179                        if sort.is_unit() {
180                            ty_ctor.replace_bound_reft(&Expr::unit())
181                        } else if let Some(def_id) = sort.is_unit_adt() {
182                            ty_ctor.replace_bound_reft(&Expr::unit_struct(def_id))
183                        } else {
184                            self.delegate.hoist_exists(ty_ctor)
185                        }
186                    }
187                    _ => self.delegate.hoist_exists(ty_ctor),
188                }
189                .fold_with(self)
190            }
191            TyKind::Constr(pred, ty) => {
192                self.delegate.hoist_constr(pred.clone());
193                ty.fold_with(self)
194            }
195            TyKind::StrgRef(..) if self.in_strg_refs => ty.super_fold_with(self),
196            TyKind::Downcast(..) if self.in_downcast => ty.super_fold_with(self),
197            _ => ty.clone(),
198        }
199    }
200
201    fn fold_bty(&mut self, bty: &BaseTy) -> BaseTy {
202        match bty {
203            BaseTy::Adt(adt_def, args) if adt_def.is_box() && self.in_boxes => {
204                let (boxed, alloc) = args.box_args();
205                let args = List::from_arr([
206                    GenericArg::Ty(boxed.fold_with(self)),
207                    GenericArg::Ty(alloc.clone()),
208                ]);
209                BaseTy::Adt(adt_def.clone(), args)
210            }
211            BaseTy::Ref(re, ty, mutability) if is_indexed_slice(ty) && self.slices => {
212                BaseTy::Ref(*re, ty.fold_with(self), *mutability)
213            }
214            BaseTy::Ref(re, ty, Mutability::Not) if self.in_shr_refs => {
215                BaseTy::Ref(*re, ty.fold_with(self), Mutability::Not)
216            }
217            BaseTy::Ref(re, ty, Mutability::Mut) if self.in_mut_refs => {
218                BaseTy::Ref(*re, ty.fold_with(self), Mutability::Mut)
219            }
220            BaseTy::Tuple(tys) if self.in_tuples => BaseTy::Tuple(tys.fold_with(self)),
221            _ => bty.clone(),
222        }
223    }
224}
225
226#[derive(Default)]
227pub struct LocalHoister {
228    vars: Vec<BoundVariableKind>,
229    preds: Vec<Expr>,
230    pub name: Option<Symbol>,
231}
232
233impl LocalHoister {
234    pub fn new(vars: Vec<BoundVariableKind>) -> Self {
235        LocalHoister { vars, preds: vec![], name: None }
236    }
237
238    pub fn bind<T>(self, f: impl FnOnce(List<BoundVariableKind>, Vec<Expr>) -> T) -> Binder<T> {
239        let vars = List::from_vec(self.vars);
240        Binder::bind_with_vars(f(vars.clone(), self.preds), vars)
241    }
242}
243
244impl HoisterDelegate for &mut LocalHoister {
245    fn hoist_exists(&mut self, ty_ctor: &TyCtor) -> Ty {
246        ty_ctor.replace_bound_refts_with(|sort, mode, kind| {
247            let idx = self.vars.len();
248            let kind = if let Some(name) = self.name { BoundReftKind::Named(name) } else { kind };
249            self.vars
250                .push(BoundVariableKind::Refine(sort.clone(), mode, kind));
251            Expr::bvar(INNERMOST, BoundVar::from_usize(idx), kind)
252        })
253    }
254
255    fn hoist_constr(&mut self, pred: Expr) {
256        self.preds.push(pred);
257    }
258}
259
260impl PolyFnSig {
261    /// Convert a function signature with existentials to one where they are all
262    /// bound at the top level. Performs a transparent (i.e. not shallow)
263    /// canonicalization.
264    /// The uses the `LocalHoister` machinery to convert a function template _without_
265    /// binders, e.g. `fn ({v.i32 | *}) -> {v.i32|*})`
266    /// into one _with_ input binders, e.g. `forall <a:int>. fn ({i32[a]|*}) -> {v.i32|*}`
267    /// after which the hole-filling machinery can be used to fill in the holes.
268    /// This lets us get "dependent signatures" for closures, where the output
269    /// can refer to the input. e.g. see `tests/pos/surface/closure09.rs`
270    pub fn hoist_input_binders(&self) -> Self {
271        let original_vars = self.vars().to_vec();
272        let fn_sig = self.skip_binder_ref();
273
274        let mut delegate =
275            LocalHoister { vars: original_vars, preds: fn_sig.requires().to_vec(), name: None };
276        let mut hoister = Hoister::with_delegate(&mut delegate).transparent();
277
278        let inputs = fn_sig
279            .inputs()
280            .iter()
281            .map(|ty| hoister.hoist(ty))
282            .collect_vec();
283
284        delegate.bind(|_vars, mut preds| {
285            let mut keep_hole = true;
286            preds.retain(|pred| {
287                if let ExprKind::Hole(HoleKind::Pred) = pred.kind() {
288                    std::mem::replace(&mut keep_hole, false)
289                } else {
290                    true
291                }
292            });
293
294            FnSig::new(
295                fn_sig.safety,
296                fn_sig.abi,
297                preds.into(),
298                inputs.into(),
299                fn_sig.output().clone(),
300                fn_sig.lifted,
301            )
302        })
303    }
304}
305
306impl Ty {
307    /// Hoist existentials and predicates inside the type stopping when encountering the first
308    /// type constructor.
309    pub fn shallow_canonicalize(&self) -> CanonicalTy {
310        let mut delegate = LocalHoister::default();
311        let ty = self.shift_in_escaping(1);
312        let ty = Hoister::with_delegate(&mut delegate).hoist(&ty);
313        let constr_ty = delegate.bind(|_, preds| {
314            let pred = Expr::and_from_iter(preds);
315            CanonicalConstrTy { ty, pred }
316        });
317        if constr_ty.vars().is_empty() {
318            CanonicalTy::Constr(constr_ty.skip_binder().shift_out_escaping(1))
319        } else {
320            CanonicalTy::Exists(constr_ty)
321        }
322    }
323}
324
325#[derive(TypeVisitable, TypeFoldable)]
326pub struct CanonicalConstrTy {
327    /// Guaranteed to not have any (shallow) [existential] or [constraint] types
328    ///
329    /// [existential]: TyKind::Exists
330    /// [constraint]: TyKind::Constr
331    ty: Ty,
332    pred: Expr,
333}
334
335impl CanonicalConstrTy {
336    pub fn ty(&self) -> Ty {
337        self.ty.clone()
338    }
339
340    pub fn pred(&self) -> Expr {
341        self.pred.clone()
342    }
343
344    pub fn to_ty(&self) -> Ty {
345        Ty::constr(self.pred(), self.ty())
346    }
347}
348
349/// A (shallowly) canonicalized type. This can be either of the form `{T | p}` or `∃v0,…,vn. {T | p}`,
350/// where `T` doesnt have any (shallow) [existential] or [constraint] types.
351///
352/// When canonicalizing a type without a [constraint] type, `p` will be [`Expr::tt()`].
353///
354/// [existential]: TyKind::Exists
355/// [constraint]: TyKind::Constr
356#[derive(TypeVisitable)]
357pub enum CanonicalTy {
358    /// A type of the form `{T | p}`
359    Constr(CanonicalConstrTy),
360    /// A type of the form `∃v0,…,vn. {T | p}`
361    Exists(Binder<CanonicalConstrTy>),
362}
363
364impl CanonicalTy {
365    pub fn to_ty(&self) -> Ty {
366        match self {
367            CanonicalTy::Constr(constr_ty) => constr_ty.to_ty(),
368            CanonicalTy::Exists(poly_constr_ty) => {
369                Ty::exists(poly_constr_ty.as_ref().map(CanonicalConstrTy::to_ty))
370            }
371        }
372    }
373
374    pub fn as_ty_or_base(&self) -> TyOrBase {
375        match self {
376            CanonicalTy::Constr(constr_ty) => {
377                if let TyKind::Indexed(bty, idx) = constr_ty.ty.kind() {
378                    // given {b[e] | p} return λv. {b[v] | p ∧ v == e}
379
380                    // HACK(nilehmann) avoid adding trivial `v == ()` equalities, if we don't do it,
381                    // some debug assertions fail. The assertions expect types to be unrefined so they
382                    // only check for syntactical equality. We should change those cases to handle
383                    // refined types and/or ensure some canonical representation for unrefined types.
384                    let pred = if idx.is_unit() {
385                        constr_ty.pred.clone()
386                    } else {
387                        Expr::and(&constr_ty.pred, Expr::eq(Expr::nu(), idx.shift_in_escaping(1)))
388                    };
389                    let sort = bty.sort();
390                    let constr = SubsetTy::new(bty.shift_in_escaping(1), Expr::nu(), pred);
391                    TyOrBase::Base(Binder::bind_with_sort(constr, sort))
392                } else {
393                    TyOrBase::Ty(self.to_ty())
394                }
395            }
396            CanonicalTy::Exists(poly_constr_ty) => {
397                let constr = poly_constr_ty.as_ref().skip_binder();
398                if let TyKind::Indexed(bty, idx) = constr.ty.kind()
399                    && idx.is_nu()
400                {
401                    let ctor = poly_constr_ty
402                        .as_ref()
403                        .map(|constr| SubsetTy::new(bty.clone(), idx, &constr.pred));
404                    TyOrBase::Base(ctor)
405                } else {
406                    TyOrBase::Ty(self.to_ty())
407                }
408            }
409        }
410    }
411}
412
413mod pretty {
414    use super::*;
415    use crate::pretty::*;
416
417    impl Pretty for CanonicalConstrTy {
418        fn fmt(&self, cx: &PrettyCx, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
419            if self.pred().is_trivially_true() {
420                w!(cx, f, "{:?}", &self.ty)
421            } else {
422                w!(cx, f, "{{ {:?} | {:?} }}", &self.ty, &self.pred)
423            }
424        }
425    }
426
427    impl Pretty for CanonicalTy {
428        fn fmt(&self, cx: &PrettyCx, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
429            match self {
430                CanonicalTy::Constr(constr) => w!(cx, f, "{:?}", constr),
431                CanonicalTy::Exists(poly_constr) => {
432                    let redundant_bvars = poly_constr.skip_binder_ref().redundant_bvars();
433                    cx.with_bound_vars_removable(
434                        poly_constr.vars(),
435                        redundant_bvars,
436                        None,
437                        |f_body| {
438                            let constr = poly_constr.skip_binder_ref();
439                            if constr.pred().is_trivially_true() {
440                                w!(cx, f_body, "{:?}", &constr.ty)
441                            } else {
442                                w!(cx, f_body, "{:?} | {:?}", &constr.ty, &constr.pred)
443                            }
444                        },
445                        |(), bound_var_layer, body| {
446                            let vars = poly_constr
447                                .vars()
448                                .into_iter()
449                                .enumerate()
450                                .filter_map(|(idx, var)| {
451                                    let not_removed = !bound_var_layer
452                                        .successfully_removed_vars
453                                        .contains(&BoundVar::from_usize(idx));
454                                    let refine_var = matches!(var, BoundVariableKind::Refine(..));
455                                    if not_removed && refine_var { Some(var.clone()) } else { None }
456                                })
457                                .collect_vec();
458                            if vars.is_empty() {
459                                write!(f, "{}", body)
460                            } else {
461                                let left = "{";
462                                let right = format!(". {} }}", body);
463                                cx.fmt_bound_vars(false, left, &vars, &right, f)
464                            }
465                        },
466                    )
467                }
468            }
469        }
470    }
471
472    impl_debug_with_default_cx!(CanonicalTy, CanonicalConstrTy);
473}