Skip to content

Commit e314a60

Browse files
committed
refactor: split LatticeContext into LatticeOptions (input, builder) and LatticeReport (output)
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent cb9b7a3 commit e314a60

26 files changed

Lines changed: 3860 additions & 73 deletions

‎src/lattice/context.rs‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎src/lattice/family/class_like_string.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ pub fn refines(input: ElementId, container: ElementId) -> bool {
2424
return false;
2525
}
2626

27-
matches!(
28-
(input_info.specifier, container_info.specifier),
29-
(_, ClassLikeStringSpecifier::Any)
30-
| (ClassLikeStringSpecifier::Literal { .. }, ClassLikeStringSpecifier::Literal { .. })
31-
)
27+
// Container `Any` accepts any specifier of the matching kind. Concrete
28+
// literal-vs-literal equality is already handled by reflexivity in the
29+
// dispatcher; getting here with two distinct literals means they differ
30+
// and the relation does not hold.
31+
matches!(container_info.specifier, ClassLikeStringSpecifier::Any)
3232
}

‎src/lattice/family/float.rs‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,31 @@
33
//! `Float` accepts any Float-kind input; `UnspecifiedLiteral` accepts
44
//! `Literal(_)` and itself; concrete literals only fit themselves
55
//! (reflexivity handles that).
6+
//!
7+
//! `int` and any int-family element refine general `Float` via PHP's
8+
//! implicit int-to-float coercion. Refined float forms (`UnspecifiedLiteral`,
9+
//! `Literal(_)`) do NOT accept ints — coercing `1` to `Literal(1.0)` would
10+
//! pin a runtime value the int wasn't carrying.
611
712
use crate::ElementId;
813
use crate::ElementKind;
914
use crate::element::payload::scalar::FloatInfo;
1015
use crate::interner::interner;
1116

1217
pub fn refines(input: ElementId, container: ElementId) -> bool {
18+
let i = interner();
19+
let container_info = *i.get_float(container);
20+
21+
// int <: general float (PHP coerces int to float).
22+
if input.kind() == ElementKind::Int {
23+
return matches!(container_info, FloatInfo::Unspecified);
24+
}
25+
1326
if input.kind() != ElementKind::Float {
1427
return false;
1528
}
1629

17-
let i = interner();
18-
let container_info = *i.get_float(container);
1930
let input_info = *i.get_float(input);
20-
2131
matches!(
2232
(input_info, container_info),
2333
(_, FloatInfo::Unspecified)

‎src/lattice/family/object.rs‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,51 @@
33
//! narrowings.
44
//!
55
//! `object` (the `ObjectAny` kind) accepts any object-family element. Named
6-
//! objects, enums, shapes, etc., need codebase queries to decide hierarchy
7-
//! relations and are not implemented here yet.
6+
//! objects use [`Codebase::is_subclass_of`] for hierarchy queries; enums
7+
//! match by name (no enum-vs-enum hierarchy in PHP); enum cases narrow
8+
//! their owning enum but not other enums or cases.
89
910
use crate::ElementId;
1011
use crate::ElementKind;
12+
use crate::interner::interner;
13+
use crate::lattice::Codebase;
1114

1215
/// Container is `object` (`ObjectAny`): accept anything in the object
1316
/// family.
1417
pub fn refines_object_any(input: ElementId, _container: ElementId) -> bool {
1518
is_object_family_kind(input.kind())
1619
}
1720

18-
/// Container is `Named(Foo)`, `Enum(E, _)`, `ObjectShape(...)`, etc.
19-
/// Requires codebase queries; not implemented yet.
20-
pub fn refines(_input: ElementId, _container: ElementId) -> bool {
21-
false
21+
/// Refinement for `Object | Enum | ObjectShape | HasMethod | HasProperty`
22+
/// containers. Hierarchy queries flow through [`Codebase::is_subclass_of`].
23+
pub fn refines<C: Codebase>(input: ElementId, container: ElementId, codebase: &C) -> bool {
24+
let i = interner();
25+
26+
match (input.kind(), container.kind()) {
27+
// Named-vs-named: codebase decides.
28+
(ElementKind::Object, ElementKind::Object) => {
29+
let input_info = i.get_object(input);
30+
let container_info = i.get_object(container);
31+
codebase.is_subclass_of(input_info.name, container_info.name)
32+
}
33+
34+
// Enum-vs-enum: same enum (refl handled upstream); different enums
35+
// are disjoint.
36+
(ElementKind::Enum, ElementKind::Enum) => {
37+
let input_info = i.get_enum(input);
38+
let container_info = i.get_enum(container);
39+
// Same enum name, container has no case constraint: input fits
40+
// (whether case-narrowed or not).
41+
input_info.name == container_info.name && container_info.case.is_none()
42+
}
43+
44+
// Named-vs-enum or enum-vs-named: PHP enums and classes don't share
45+
// a hierarchy here (enums implement interfaces, but the object
46+
// family handles those as named objects). Distinct.
47+
(ElementKind::Object, ElementKind::Enum) | (ElementKind::Enum, ElementKind::Object) => false,
48+
49+
_ => false,
50+
}
2251
}
2352

2453
pub(crate) fn is_object_family_kind(kind: ElementKind) -> bool {

‎src/lattice/family/string.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ fn satisfies_flags(input: StringInfo, container_flags: StringRefinementFlags) ->
113113
}
114114

115115
pub(super) fn input_is_non_empty(input: StringInfo) -> bool {
116-
if input.flags.is_non_empty() || input.flags.is_truthy() {
116+
// Truthy, numeric, and callable all imply non-empty (the empty string
117+
// is none of those).
118+
if input.flags.is_non_empty() || input.flags.is_truthy() || input.flags.is_numeric() || input.flags.is_callable() {
117119
return true;
118120
}
119121

‎src/lattice/intersects.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
1515
use crate::TypeId;
1616
use crate::lattice::Codebase;
17-
use crate::lattice::LatticeContext;
17+
use crate::lattice::LatticeOptions;
18+
use crate::lattice::LatticeReport;
1819
use crate::lattice::refines::generalizes;
1920
use crate::lattice::refines::refines;
2021

@@ -27,10 +28,16 @@ use crate::lattice::refines::refines;
2728
///
2829
/// Returning `false` is therefore not yet a guarantee of disjointness; only
2930
/// `true` is sound to act on (modulo the current rule coverage).
30-
pub fn intersects<C: Codebase>(a: TypeId, b: TypeId, ctx: &mut LatticeContext, codebase: &C) -> bool {
31+
pub fn intersects<C: Codebase>(
32+
a: TypeId,
33+
b: TypeId,
34+
codebase: &C,
35+
options: LatticeOptions,
36+
report: &mut LatticeReport,
37+
) -> bool {
3138
if a == b {
3239
return true;
3340
}
3441

35-
refines(a, b, ctx, codebase) || generalizes(a, b, ctx, codebase)
42+
refines(a, b, codebase, options, report) || generalizes(a, b, codebase, options, report)
3643
}

‎src/lattice/mod.rs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@
99
//! - [`intersects`] — `a ∩ b ≠ ∅` (there exists a value in both `a` and
1010
//! `b`).
1111
//!
12-
//! All three take a [`LatticeContext`] (side-effect accumulator for
13-
//! coercions, template bounds, etc.) and a [`Codebase`] (class hierarchy
14-
//! lookups, member existence checks, template metadata).
12+
//! Each takes a [`Codebase`] (class hierarchy lookups, member existence
13+
//! checks, template metadata), a [`LatticeOptions`] value (caller-set
14+
//! knobs like `ignore_null`), and writes diagnostics into a
15+
//! `&mut LatticeReport` (`type_coerced` and friends).
1516
//!
1617
//! Per-family rules live in [`family`]; each [`crate::ElementKind`] family
1718
//! owns its refinement and (eventually) intersection logic in a dedicated
1819
//! submodule.
1920
2021
mod codebase;
21-
mod context;
2222
pub mod family;
2323
mod intersects;
24+
mod options;
2425
mod refines;
26+
mod report;
2527

2628
pub use self::codebase::Codebase;
2729
pub use self::codebase::NullCodebase;
28-
pub use self::context::LatticeContext;
2930
pub use self::intersects::intersects;
31+
pub use self::options::LatticeOptions;
3032
pub use self::refines::generalizes;
3133
pub use self::refines::refines;
34+
pub use self::report::LatticeReport;

‎src/lattice/options.rs‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)