Skip to content

Commit c8ab4a7

Browse files
committed
feat(lattice): finalise sealed-class implementation with intersected canonicalisation, sealed proptest worlds, and array uninhabitedness fix
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 2f27b6b commit c8ab4a7

7 files changed

Lines changed: 474 additions & 28 deletions

File tree

‎src/lattice/overlaps.rs‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ fn object_overlap<W: World>(
288288
return false;
289289
}
290290

291+
if a_info.name != b_info.name
292+
&& let (Some(pa), Some(pb)) = (world.sealed_parent_of(a_info.name), world.sealed_parent_of(b_info.name))
293+
&& pa == pb
294+
&& !world.descends_from(a_info.name, b_info.name)
295+
&& !world.descends_from(b_info.name, a_info.name)
296+
{
297+
return false;
298+
}
299+
291300
if a_info.name == b_info.name
292301
&& let (Some(a_args_id), Some(b_args_id)) = (a_info.type_args, b_info.type_args)
293302
{
@@ -460,14 +469,14 @@ fn list_uninhabited<W: World>(info: &ListInfo, intersections: Option<ElementList
460469

461470
#[inline]
462471
fn array_uninhabited<W: World>(info: &KeyedArrayInfo, intersections: Option<ElementListId>, world: &W) -> bool {
463-
if let Some(key_t) = info.key_param {
464-
let int_or_string = interner().intern_type(&[prelude::INT, prelude::STRING], FlowFlags::EMPTY);
465-
if !lattice::overlaps(key_t, int_or_string, world, LatticeOptions::default(), &mut LatticeReport::new()) {
466-
return true;
472+
if info.flags.non_empty() {
473+
if let Some(key_t) = info.key_param {
474+
let int_or_string = interner().intern_type(&[prelude::INT, prelude::STRING], FlowFlags::EMPTY);
475+
if !lattice::overlaps(key_t, int_or_string, world, LatticeOptions::default(), &mut LatticeReport::new()) {
476+
return true;
477+
}
467478
}
468-
}
469479

470-
if info.flags.non_empty() {
471480
let key_empty = info.key_param.is_some_and(|t| type_is_value_never(t, world));
472481
let value_empty = info.value_param.is_some_and(|t| type_is_value_never(t, world));
473482
if key_empty || value_empty {

‎src/lattice/sealed.rs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ fn compute_residual_impl<W: World>(
8686

8787
if let Some(grandchildren) = world.sealed_direct_inheritors(si) {
8888
if visited.contains(&si) {
89-
surviving.push(si_elem);
90-
continue;
89+
// A cycle in the sealing graph is unresolvable. Bail
90+
// up to the caller as `NotSealed` rather than emitting
91+
// self-referential survivors ; otherwise downstream
92+
// refines / overlaps consumers loop forever asking the
93+
// same question.
94+
return SealedResidual::NotSealed;
9195
}
9296
visited.push(si);
9397

@@ -107,7 +111,11 @@ fn compute_residual_impl<W: World>(
107111

108112
match sub {
109113
SealedResidual::FullyCovered => {}
110-
SealedResidual::NotSealed => surviving.push(si_elem),
114+
// Recursion bailed (cycle or depth cap). Don't try to
115+
// synthesize a partial cover ; propagate the give-up
116+
// signal so callers fall back to non-sealed reasoning
117+
// rather than looping on a self-referential survivor.
118+
SealedResidual::NotSealed => return SealedResidual::NotSealed,
111119
SealedResidual::Surviving(children) => surviving.extend(children),
112120
}
113121
} else {

‎src/meet/mod.rs‎

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ use crate::ElementId;
4848
use crate::ElementKind;
4949
use crate::FlowFlags;
5050
use crate::TypeId;
51+
use crate::element::payload::MixedInfo;
52+
use crate::element::payload::Truthiness;
5153
use crate::interner::interner;
5254
use crate::lattice::LatticeOptions;
5355
use crate::lattice::LatticeReport;
56+
use crate::lattice::overlaps;
5457
use crate::lattice::refines;
58+
use crate::lattice::sealed::SealedResidual;
59+
use crate::lattice::sealed::compute_residual;
60+
use crate::meet::family::generic;
5561
use crate::prelude::MIXED;
5662
use crate::prelude::NEVER;
5763
use crate::prelude::PLACEHOLDER;
@@ -128,7 +134,7 @@ pub fn narrow<W: World>(
128134
continue;
129135
}
130136

131-
if any_mixed && let Some(pieces) = narrowed_mixed_meet_multi(x, y) {
137+
if any_mixed && let Some(pieces) = narrowed_mixed_meet_multi(x, y, world) {
132138
atoms.extend(pieces);
133139
continue;
134140
}
@@ -173,25 +179,31 @@ fn atom_meet<W: World>(
173179
options: LatticeOptions,
174180
report: &mut LatticeReport,
175181
) -> Option<ElementId> {
176-
if crate::lattice::overlaps::is_uninhabited(a, world) || crate::lattice::overlaps::is_uninhabited(b, world) {
182+
if overlaps::is_uninhabited(a, world) || overlaps::is_uninhabited(b, world) {
177183
return None;
178184
}
185+
179186
if a == b {
180187
return Some(a);
181188
}
189+
182190
if a == NEVER || b == NEVER {
183191
return None;
184192
}
193+
185194
if a == MIXED || a == PLACEHOLDER {
186-
if crate::lattice::overlaps::is_uninhabited(b, world) {
195+
if overlaps::is_uninhabited(b, world) {
187196
return None;
188197
}
198+
189199
return Some(b);
190200
}
201+
191202
if b == MIXED || b == PLACEHOLDER {
192-
if crate::lattice::overlaps::is_uninhabited(a, world) {
203+
if overlaps::is_uninhabited(a, world) {
193204
return None;
194205
}
206+
195207
return Some(a);
196208
}
197209

@@ -217,26 +229,37 @@ fn atom_meet<W: World>(
217229
}
218230

219231
if a.kind() == ElementKind::Mixed || b.kind() == ElementKind::Mixed {
220-
return narrowed_mixed_meet(a, b);
232+
return normalise_meet_result(narrowed_mixed_meet(a, b, world), world);
221233
}
222234

223235
let i = interner();
224236
let a_t = i.intern_type(&[a], FlowFlags::EMPTY);
225237
let b_t = i.intern_type(&[b], FlowFlags::EMPTY);
226238

227239
if refines(a_t, b_t, world, options, report) {
228-
return Some(a);
240+
return normalise_meet_result(Some(a), world);
229241
}
230242

231243
if refines(b_t, a_t, world, options, report) {
232-
return Some(b);
244+
return normalise_meet_result(Some(b), world);
233245
}
234246

235247
if a.kind() == ElementKind::GenericParameter || b.kind() == ElementKind::GenericParameter {
236-
return family::generic::generic_parameter_meet(a, b, world, options, report);
248+
return normalise_meet_result(generic::generic_parameter_meet(a, b, world, options, report), world);
237249
}
238250

239-
family_atom_meet(a, b, world, options, report)
251+
normalise_meet_result(family_atom_meet(a, b, world, options, report), world)
252+
}
253+
254+
/// If the synthesised element is uninhabited (e.g. sealed-class
255+
/// intersection with all inheritors negated), collapse to `None`
256+
/// so the caller treats it as the empty meet.
257+
#[inline]
258+
fn normalise_meet_result<W: World>(result: Option<ElementId>, world: &W) -> Option<ElementId> {
259+
match result {
260+
Some(elem) if overlaps::is_uninhabited(elem, world) => None,
261+
other => other,
262+
}
240263
}
241264

242265
/// Cross-dominator pair meet: `(ArrayKey, Numeric)` shares `int`
@@ -370,20 +393,85 @@ fn intersected_atom_meet<W: World>(
370393
ElementId::intersected(head, &conjuncts)
371394
};
372395

373-
if crate::lattice::overlaps::is_uninhabited(result, world) {
396+
if let Some(canon) = canonicalise_intersected(result, world, options, report) {
397+
return Some(canon);
398+
}
399+
400+
if overlaps::is_uninhabited(result, world) {
374401
return None;
375402
}
376403

377404
Some(result)
378405
}
379406

407+
/// Drop redundant negated conjuncts and collapse sealed-cover residuals.
408+
/// A negated conjunct `!X` is redundant when `head` is disjoint from `X`
409+
/// (`!overlaps(head, X)`), meaning the head already satisfies the negation.
410+
/// After dropping redundancies, a sealed-cover single-survivor residual
411+
/// replaces the Intersected with the bare inheritor.
412+
#[inline]
413+
fn canonicalise_intersected<W: World>(
414+
elem: ElementId,
415+
world: &W,
416+
options: LatticeOptions,
417+
report: &mut LatticeReport,
418+
) -> Option<ElementId> {
419+
if elem.kind() != ElementKind::Intersected {
420+
return None;
421+
}
422+
let i = interner();
423+
let info = *i.get_intersected(elem);
424+
425+
let conjuncts = i.get_element_list(info.conjuncts);
426+
let head_is_object = info.head.kind() == ElementKind::Object;
427+
let head_t = if head_is_object { Some(i.intern_type(&[info.head], FlowFlags::EMPTY)) } else { None };
428+
429+
let mut kept: Vec<ElementId> = Vec::with_capacity(conjuncts.len());
430+
let mut negated_inners: Vec<TypeId> = Vec::with_capacity(conjuncts.len());
431+
for &c in conjuncts {
432+
if c.kind() == ElementKind::Negated {
433+
let inner = i.get_negated(c).inner;
434+
if let Some(t) = head_t
435+
&& !overlaps(t, inner, world, options, report)
436+
{
437+
continue;
438+
}
439+
negated_inners.push(inner);
440+
}
441+
kept.push(c);
442+
}
443+
444+
if kept.is_empty() {
445+
return Some(info.head);
446+
}
447+
448+
if info.head.kind() == ElementKind::Object && !negated_inners.is_empty() {
449+
let residual = compute_residual(info.head, &negated_inners, world, options, report);
450+
match residual {
451+
SealedResidual::Surviving(survivors) if survivors.len() == 1 => {
452+
return Some(survivors[0]);
453+
}
454+
SealedResidual::FullyCovered => {
455+
return Some(crate::prelude::NEVER);
456+
}
457+
_ => {}
458+
}
459+
}
460+
461+
if kept.len() == conjuncts.len() {
462+
return None;
463+
}
464+
465+
Some(ElementId::intersected(info.head, &kept))
466+
}
467+
380468
/// `meet(narrowed-mixed, X)` where `narrowed-mixed` is `truthy-mixed`,
381469
/// `falsy-mixed`, or `non-null-mixed`. Returns `X` filtered by the
382470
/// flag, expressed via the universal [`Intersected`] / [`Negated`]
383471
/// machinery and PHP truthiness semantics for each element kind.
384472
#[inline]
385-
fn narrowed_mixed_meet(a: ElementId, b: ElementId) -> Option<ElementId> {
386-
let pieces = narrowed_mixed_meet_multi(a, b)?;
473+
fn narrowed_mixed_meet<W: World>(a: ElementId, b: ElementId, world: &W) -> Option<ElementId> {
474+
let pieces = narrowed_mixed_meet_multi(a, b, world)?;
387475
match pieces.as_slice() {
388476
[] => None,
389477
[single] => Some(*single),
@@ -395,9 +483,7 @@ fn narrowed_mixed_meet(a: ElementId, b: ElementId) -> Option<ElementId> {
395483
/// neither side is a `Mixed` element. Returns `Some(vec![])` for the
396484
/// empty meet.
397485
#[inline]
398-
fn narrowed_mixed_meet_multi(a: ElementId, b: ElementId) -> Option<Vec<ElementId>> {
399-
use crate::element::payload::MixedInfo;
400-
use crate::element::payload::Truthiness;
486+
fn narrowed_mixed_meet_multi<W: World>(a: ElementId, b: ElementId, world: &W) -> Option<Vec<ElementId>> {
401487
if a.kind() != ElementKind::Mixed && b.kind() != ElementKind::Mixed {
402488
return None;
403489
}
@@ -426,6 +512,10 @@ fn narrowed_mixed_meet_multi(a: ElementId, b: ElementId) -> Option<Vec<ElementId
426512
let (mixed_atom, other) = if a.kind() == ElementKind::Mixed { (a, b) } else { (b, a) };
427513
let info = *i.get_mixed(mixed_atom);
428514
if info == MixedInfo::EMPTY {
515+
if overlaps::is_uninhabited(other, world) {
516+
return Some(Vec::new());
517+
}
518+
429519
return Some(vec![other]);
430520
}
431521

‎src/subtract/mod.rs‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ use crate::lattice::LatticeOptions;
5555
use crate::lattice::LatticeReport;
5656
use crate::lattice::overlaps;
5757
use crate::lattice::refines;
58+
use crate::lattice::sealed;
59+
use crate::lattice::sealed::SealedResidual;
5860
use crate::meet;
5961
use crate::prelude;
6062
use crate::prelude::FALSE;
@@ -158,10 +160,83 @@ pub fn narrow<W: World>(
158160
return SubtractOutcome::Impossible;
159161
}
160162

163+
canonicalise_sealed_residuals(&mut atoms, world, options, report);
164+
161165
let result = TypeId::union(&atoms);
166+
162167
if result == input { SubtractOutcome::Redundant(input) } else { SubtractOutcome::Narrowed(result) }
163168
}
164169

170+
#[inline]
171+
#[allow(clippy::arithmetic_side_effects)]
172+
fn canonicalise_sealed_residuals<W: World>(
173+
atoms: &mut Vec<ElementId>,
174+
world: &W,
175+
options: LatticeOptions,
176+
report: &mut LatticeReport,
177+
) {
178+
if atoms.is_empty() {
179+
return;
180+
}
181+
182+
let i = interner();
183+
let mut idx = 0;
184+
while idx < atoms.len() {
185+
let elem = atoms[idx];
186+
if elem.kind() != ElementKind::Intersected {
187+
idx += 1;
188+
continue;
189+
}
190+
191+
let info = *i.get_intersected(elem);
192+
let conjuncts = i.get_element_list(info.conjuncts);
193+
let head_is_object = info.head.kind() == ElementKind::Object;
194+
let head_t = if head_is_object { Some(i.intern_type(&[info.head], FlowFlags::EMPTY)) } else { None };
195+
196+
let mut kept: Vec<ElementId> = Vec::with_capacity(conjuncts.len());
197+
let mut negated_inners: Vec<TypeId> = Vec::with_capacity(conjuncts.len());
198+
for &c in conjuncts {
199+
if c.kind() == ElementKind::Negated {
200+
let inner = i.get_negated(c).inner;
201+
if let Some(t) = head_t
202+
&& !overlaps(t, inner, world, options, report)
203+
{
204+
continue;
205+
}
206+
negated_inners.push(inner);
207+
}
208+
kept.push(c);
209+
}
210+
211+
if kept.is_empty() {
212+
atoms[idx] = info.head;
213+
idx += 1;
214+
continue;
215+
}
216+
217+
if info.head.kind() == ElementKind::Object && !negated_inners.is_empty() {
218+
let residual = sealed::compute_residual(info.head, &negated_inners, world, options, report);
219+
match residual {
220+
SealedResidual::FullyCovered => {
221+
atoms.remove(idx);
222+
continue;
223+
}
224+
SealedResidual::Surviving(survivors) if survivors.len() == 1 => {
225+
atoms[idx] = survivors[0];
226+
idx += 1;
227+
continue;
228+
}
229+
_ => {}
230+
}
231+
}
232+
233+
if kept.len() != conjuncts.len() {
234+
atoms[idx] = ElementId::intersected(info.head, &kept);
235+
}
236+
idx += 1;
237+
}
238+
}
239+
165240
/// Compute `A \ B`: the largest representable type whose values are in
166241
/// `A` but not in `B`. Thin wrapper over [`narrow`] for callers that
167242
/// don't need the assertion classification.

0 commit comments

Comments
 (0)