|
| 1 | +// SPDX-License-Identifier: CC0-1.0 |
| 2 | + |
| 3 | +//! "Finalized" Incomplete Type Data |
| 4 | +//! |
| 5 | +//! This structure is essentially the same as [types::Final](super::Final) except |
| 6 | +//! that it has free variables (represented by strings) and supports self-reference. |
| 7 | +//! The purpose of this structure is to provide a useful representation of a type |
| 8 | +//! in error messages. |
| 9 | +//! |
| 10 | +
|
| 11 | +use crate::dag::{Dag, DagLike, NoSharing}; |
| 12 | +use crate::types::union_bound::PointerLike; |
| 13 | + |
| 14 | +use super::{Bound, BoundRef, Context}; |
| 15 | + |
| 16 | +use std::fmt; |
| 17 | +use std::sync::Arc; |
| 18 | + |
| 19 | +/// An incomplete type bound for use in error messages. |
| 20 | +#[derive(Clone)] |
| 21 | +pub enum Incomplete { |
| 22 | + /// A free variable. |
| 23 | + Free(String), |
| 24 | + /// A type containing this type. |
| 25 | + Cycle, |
| 26 | + /// A sum of two other types |
| 27 | + Sum(Arc<Incomplete>, Arc<Incomplete>), |
| 28 | + /// A product of two other types |
| 29 | + Product(Arc<Incomplete>, Arc<Incomplete>), |
| 30 | + /// A complete type (including unit) |
| 31 | + Final(Arc<super::Final>), |
| 32 | +} |
| 33 | + |
| 34 | +impl DagLike for &'_ Incomplete { |
| 35 | + type Node = Incomplete; |
| 36 | + fn data(&self) -> &Incomplete { |
| 37 | + self |
| 38 | + } |
| 39 | + fn as_dag_node(&self) -> Dag<Self> { |
| 40 | + match *self { |
| 41 | + Incomplete::Free(_) | Incomplete::Cycle | Incomplete::Final(_) => Dag::Nullary, |
| 42 | + Incomplete::Sum(ref left, ref right) | Incomplete::Product(ref left, ref right) => { |
| 43 | + Dag::Binary(left, right) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl fmt::Debug for Incomplete { |
| 50 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 51 | + fmt::Display::fmt(self, f) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl fmt::Display for Incomplete { |
| 56 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 57 | + let mut skip_next = false; |
| 58 | + for data in self.verbose_pre_order_iter::<NoSharing>(None) { |
| 59 | + if skip_next { |
| 60 | + skip_next = false; |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + match (data.node, data.n_children_yielded) { |
| 65 | + (Incomplete::Free(ref s), _) => f.write_str(s)?, |
| 66 | + (Incomplete::Cycle, _) => f.write_str("<self-reference>")?, |
| 67 | + // special-case 1 + A as A? |
| 68 | + (Incomplete::Sum(ref left, _), 0) if left.is_unit() => { |
| 69 | + skip_next = true; |
| 70 | + } |
| 71 | + (Incomplete::Sum(ref left, _), 1) if left.is_unit() => {} |
| 72 | + (Incomplete::Sum(ref left, _), 2) if left.is_unit() => { |
| 73 | + f.write_str("?")?; |
| 74 | + } |
| 75 | + // other sums and products |
| 76 | + (Incomplete::Sum(..), 0) | (Incomplete::Product(..), 0) => { |
| 77 | + if data.index > 0 { |
| 78 | + f.write_str("(")?; |
| 79 | + } |
| 80 | + } |
| 81 | + (Incomplete::Sum(..), 2) | (Incomplete::Product(..), 2) => { |
| 82 | + if data.index > 0 { |
| 83 | + f.write_str(")")?; |
| 84 | + } |
| 85 | + } |
| 86 | + (Incomplete::Sum(..), _) => f.write_str(" + ")?, |
| 87 | + (Incomplete::Product(..), _) => f.write_str(" × ")?, |
| 88 | + (Incomplete::Final(ref fnl), _) => fnl.fmt(f)?, |
| 89 | + } |
| 90 | + } |
| 91 | + Ok(()) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl Incomplete { |
| 96 | + /// Whether this "incomplete bound" is the unit type. |
| 97 | + pub fn is_unit(&self) -> bool { |
| 98 | + if let Incomplete::Final(ref fnl) = self { |
| 99 | + fnl.is_unit() |
| 100 | + } else { |
| 101 | + false |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// Does the occurs-check on a type bound. |
| 106 | + /// |
| 107 | + /// Returns None on success, and a Some(Incomplete) indicating the occurs-check |
| 108 | + /// failure if there is a cyclic reference. |
| 109 | + pub(super) fn occurs_check(ctx: &Context, bound_ref: BoundRef) -> Option<Arc<Self>> { |
| 110 | + use std::collections::HashSet; |
| 111 | + |
| 112 | + use super::context::OccursCheckId; |
| 113 | + use super::BoundRef; |
| 114 | + |
| 115 | + /// Helper type for the occurs-check. |
| 116 | + enum OccursCheckStack { |
| 117 | + Iterate(BoundRef), |
| 118 | + Complete(OccursCheckId), |
| 119 | + } |
| 120 | + |
| 121 | + // First, do occurs-check to ensure that we have no infinitely sized types. |
| 122 | + let mut stack = vec![OccursCheckStack::Iterate(bound_ref)]; |
| 123 | + let mut in_progress = HashSet::new(); |
| 124 | + let mut completed = HashSet::new(); |
| 125 | + while let Some(top) = stack.pop() { |
| 126 | + let bound = match top { |
| 127 | + OccursCheckStack::Complete(id) => { |
| 128 | + in_progress.remove(&id); |
| 129 | + completed.insert(id); |
| 130 | + continue; |
| 131 | + } |
| 132 | + OccursCheckStack::Iterate(b) => b, |
| 133 | + }; |
| 134 | + |
| 135 | + let id = bound.occurs_check_id(); |
| 136 | + if completed.contains(&id) { |
| 137 | + // Once we have iterated through a type, we don't need to check it again. |
| 138 | + // Without this shortcut the occurs-check would take exponential time. |
| 139 | + continue; |
| 140 | + } |
| 141 | + if !in_progress.insert(id) { |
| 142 | + // FIXME unwind the stack to somehow provide a more useful trace of the occurs-check failure |
| 143 | + return Some(Arc::new(Self::Cycle)); |
| 144 | + } |
| 145 | + |
| 146 | + stack.push(OccursCheckStack::Complete(id)); |
| 147 | + if let Some((_, child)) = (ctx, bound.shallow_clone()).right_child() { |
| 148 | + stack.push(OccursCheckStack::Iterate(child)); |
| 149 | + } |
| 150 | + if let Some((_, child)) = (ctx, bound).left_child() { |
| 151 | + stack.push(OccursCheckStack::Iterate(child)); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + None |
| 156 | + } |
| 157 | + |
| 158 | + pub(super) fn from_bound_ref(ctx: &Context, bound_ref: BoundRef) -> Arc<Self> { |
| 159 | + if let Some(err) = Self::occurs_check(ctx, bound_ref.shallow_clone()) { |
| 160 | + return err; |
| 161 | + } |
| 162 | + |
| 163 | + // Now that we know our bound has finite size, we can safely use a |
| 164 | + // post-order iterator on it. |
| 165 | + let mut finalized = vec![]; |
| 166 | + for data in (ctx, bound_ref).post_order_iter::<NoSharing>() { |
| 167 | + let bound_get = data.node.0.get(&data.node.1); |
| 168 | + let final_data = match bound_get { |
| 169 | + Bound::Free(s) => Incomplete::Free(s), |
| 170 | + Bound::Complete(ref arc) => Incomplete::Final(Arc::clone(arc)), |
| 171 | + Bound::Sum(..) => Incomplete::Sum( |
| 172 | + Arc::clone(&finalized[data.left_index.unwrap()]), |
| 173 | + Arc::clone(&finalized[data.right_index.unwrap()]), |
| 174 | + ), |
| 175 | + Bound::Product(..) => Incomplete::Product( |
| 176 | + Arc::clone(&finalized[data.left_index.unwrap()]), |
| 177 | + Arc::clone(&finalized[data.right_index.unwrap()]), |
| 178 | + ), |
| 179 | + }; |
| 180 | + |
| 181 | + finalized.push(Arc::new(final_data)); |
| 182 | + } |
| 183 | + finalized.pop().unwrap() |
| 184 | + } |
| 185 | +} |
0 commit comments