@@ -48,10 +48,16 @@ use crate::ElementId;
4848use crate :: ElementKind ;
4949use crate :: FlowFlags ;
5050use crate :: TypeId ;
51+ use crate :: element:: payload:: MixedInfo ;
52+ use crate :: element:: payload:: Truthiness ;
5153use crate :: interner:: interner;
5254use crate :: lattice:: LatticeOptions ;
5355use crate :: lattice:: LatticeReport ;
56+ use crate :: lattice:: overlaps;
5457use crate :: lattice:: refines;
58+ use crate :: lattice:: sealed:: SealedResidual ;
59+ use crate :: lattice:: sealed:: compute_residual;
60+ use crate :: meet:: family:: generic;
5561use crate :: prelude:: MIXED ;
5662use crate :: prelude:: NEVER ;
5763use 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
0 commit comments