Skip to content

feat(compiler): enforce array element-kind constraints at compile time#1861

Draft
pront wants to merge 10 commits into
mainfrom
pront/parameter-element-kind
Draft

feat(compiler): enforce array element-kind constraints at compile time#1861
pront wants to merge 10 commits into
mainfrom
pront/parameter-element-kind

Conversation

@pront

@pront pront commented Jul 13, 2026

Copy link
Copy Markdown
Member

Summary

Closes #483.

VRL functions that take array arguments often require all elements to be a specific type (e.g. join requires array<bytes>). Previously, each function had to hand-roll an element-kind check inside type_def(), mixing return-type description with argument validation. Functions that forgot this check silently accepted wrong-typed arrays and produced runtime errors.

This PR introduces two improvements:

1. Parameter::with_element_kind(u16) — declarative element-type constraints

A new opt-in field on Parameter lets stdlib functions declare what element kind their array parameter expects:

Parameter::required("substrings", kind::ARRAY, "...")
    .with_element_kind(kind::BYTES)

The compiler's existing arg-binder already does superset/intersect/fallibility inference — it just needed element-type info to work with. Now:

  • Argument element kind ⊆ declared → infallible
  • Argument element kind is unknown/superset → fallible (user must handle with ! or ??)
  • Argument element kind is disjoint → hard compile error (see below)

Functions updated: contains_all, tally, join, encode_key_value, ip_cidr_contains, parse_groks. Each loses its hand-rolled type_def element check; the compiler enforces it uniformly.

2. Element-aware Kind::intersects for arrays

Previously intersects for two array types returned true as long as both were arrays, ignoring element types. Now it delegates to Collection::intersects, which checks whether any known element on either side is provably disjoint from the other side's element kind.

Effect: passing a literal like [1, 2] to a function expecting array<bytes> is now a hard compile error — not just fallible, but rejected outright, even with ! or ??.

error[E110]: invalid argument type
  join([1, 2, 3])
       ^^^^^^^^^
       this expression resolves to the exact type [integer, integer, integer]
       but the parameter "value" expects the exact type array

Test plan

  • cargo test -p vrl --lib — 1854 tests pass
  • contains_all::compiler_flags_non_bytes_element_as_fallible — end-to-end test verifying string literals compile infallible, integer literals are hard compile errors even with ??
  • Kind::test_intersects_arrays — unit tests for all array intersection cases including empty arrays, disjoint known elements, mixed known elements
  • Collection::test_collection_intersects — unit tests for collection-level intersection logic
  • Manual REPL: join([1,2,3]), join!([1,2,3]), join([1,2,3]) ?? "" all produce compile errors; join(["a","b"]) is infallible

@github-actions github-actions Bot added the docs review on hold PR is pending a docs team review label Jul 16, 2026
pront added 7 commits July 16, 2026 12:15
Adds `Parameter::element_kind: u16` (with `with_element_kind` builder)
so stdlib functions can declare what element type their array parameters
expect. The compiler's existing arg-binder automatically uses this to
mark calls fallible (unknown/mixed element types) or reject them as
hard compile errors (provably disjoint element types).

Separately, `Kind::intersects` and the new `Collection::intersects` now
check element kinds when comparing array types, so a literal like
`[1, 2]` passed to a `array<bytes>` parameter is a compile error even
with `!` or `??` — it can never succeed.

Updated functions: contains_all, tally, join, encode_key_value.
Each loses its hand-rolled `type_def` element-kind check; the compiler
enforces the constraint uniformly.
@pront
pront force-pushed the pront/parameter-element-kind branch from fe957e1 to c66ae4f Compare July 16, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs review on hold PR is pending a docs team review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add element type to kind::ARRAY

1 participant