|
| 1 | +use crate::TypeId; |
| 2 | + |
| 3 | +/// Caller-controlled options for the lattice operations |
| 4 | +/// ([`refines`](crate::lattice::refines), |
| 5 | +/// [`generalizes`](crate::lattice::generalizes), |
| 6 | +/// [`intersects`](crate::lattice::intersects)). |
| 7 | +/// |
| 8 | +/// Each field tweaks the algorithm without changing its meaning at the |
| 9 | +/// type level. Defaults are all `false`. Use [`LatticeOptions::default`] |
| 10 | +/// for the common case and chain `with_*` builders for any flags you |
| 11 | +/// need; or derive options from a type's [`FlowFlags`](crate::FlowFlags) |
| 12 | +/// via [`LatticeOptions::of_type`] / [`LatticeOptions::assertion_of_type`]. |
| 13 | +/// |
| 14 | +/// `LatticeOptions` is `Copy` and small enough to pass by value; the |
| 15 | +/// operations take it that way. |
| 16 | +#[derive(Debug, Clone, Copy, Default)] |
| 17 | +pub struct LatticeOptions { |
| 18 | + /// Skip `null` elements in the input union when refining. Used by |
| 19 | + /// nullsafe-aware analyzers: a `?int` argument can be passed to an |
| 20 | + /// `int` parameter under this flag without a "null leak" diagnostic. |
| 21 | + pub ignore_null: bool, |
| 22 | + /// Skip the `false` element in the input union when refining. Used by |
| 23 | + /// `int|false` style return values that the caller has narrowed away |
| 24 | + /// from `false`. |
| 25 | + pub ignore_false: bool, |
| 26 | + /// The refinement is being checked inside a runtime assertion (e.g. |
| 27 | + /// `assert($x instanceof Foo)`). Some rules become more permissive |
| 28 | + /// in this mode. |
| 29 | + pub inside_assertion: bool, |
| 30 | +} |
| 31 | + |
| 32 | +impl LatticeOptions { |
| 33 | + /// Derive options from a type's [`FlowFlags`](crate::FlowFlags): |
| 34 | + /// |
| 35 | + /// `ignore_null` mirrors `flags.ignore_nullable_issues()` and |
| 36 | + /// `ignore_false` mirrors `flags.ignore_falsable_issues()`. |
| 37 | + /// `inside_assertion` stays `false`. |
| 38 | + pub fn of_type(ty: TypeId) -> Self { |
| 39 | + let f = ty.as_ref().flags; |
| 40 | + Self { |
| 41 | + ignore_null: f.ignore_nullable_issues(), |
| 42 | + ignore_false: f.ignore_falsable_issues(), |
| 43 | + inside_assertion: false, |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// Same as [`of_type`](Self::of_type), but with `inside_assertion` set. |
| 48 | + pub fn assertion_of_type(ty: TypeId) -> Self { |
| 49 | + Self::of_type(ty).inside_assertion() |
| 50 | + } |
| 51 | + |
| 52 | + /// Set [`ignore_null`](Self::ignore_null) to `true`. |
| 53 | + #[must_use] |
| 54 | + pub const fn with_ignore_null(mut self) -> Self { |
| 55 | + self.ignore_null = true; |
| 56 | + self |
| 57 | + } |
| 58 | + |
| 59 | + /// Set [`ignore_false`](Self::ignore_false) to `true`. |
| 60 | + #[must_use] |
| 61 | + pub const fn with_ignore_false(mut self) -> Self { |
| 62 | + self.ignore_false = true; |
| 63 | + self |
| 64 | + } |
| 65 | + |
| 66 | + /// Set [`inside_assertion`](Self::inside_assertion) to `true`. |
| 67 | + #[must_use] |
| 68 | + pub const fn inside_assertion(mut self) -> Self { |
| 69 | + self.inside_assertion = true; |
| 70 | + self |
| 71 | + } |
| 72 | +} |
0 commit comments