Skip to content

Soundness: subtyping, interfaces, records, enums, equality - #235

Merged
vito merged 31 commits into
mainfrom
soundness-subtyping
Nov 1, 2026
Merged

vito merged 31 commits into
mainfrom
soundness-subtyping

Conversation

@vito

@vito vito commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Soundness fixes for subtyping, objects, interfaces, records, enums and
variance: programs the checker accepted but that went wrong at runtime
(wrong-typed values, missing fields, null in a non-null slot, an
"exhaustive" case that didn't match). Almost everything here only makes
the checker stricter; the exceptions are listed under release notes.

Overview of the whole soundness effort: https://gistpreview.github.io/?7466a76346390a8516a0d9f03a430aff

Bugs fixed

Each was accepted before and failed at runtime; now it's a compile error,
unless noted.

Function parameters weren't contravariant. Function types now check
arguments and the block contravariantly and the result covariantly. They are
also rechecked under the combined substitution, so a generic function like
ident(d: a): a can't stand in for (d: Animal!): Dog!.

type Host { hook(a: Animal!): String! { a.name }; fire: String! { hook(Cat("c")) } }
dogOnly(a: Dog!): String! { a.bark }
h.hook = &dogOnly   # before: runtime `module Cat does not have a field "bark"`

Parameter names weren't part of function types, though arguments bind by name.

myFun(&block(x: Int!): Int!): Int! { block(x: 42) }
myFun { y => y + 1 }   # before: y was null → "addition not supported for NullValue"; now 43
  • A function with differently named or swapped parameters could be stored in a function-typed field, crossing its arguments. Now rejected.
  • A block now takes on the parameter names of the signature it's passed for.
  • A positional call now binds by the name resolved against the callee's static type.

Interface conformance missed block parameters and private members.

  • An implementation could call its block with a supertype of what the interface promised the block. It must now accept the interface's block type and name its parameters the same.
  • A let member satisfied a public interface field, and a child interface could redeclare a parent's public field as let. Both now rejected.

Records were taken for named types.

let d: Dog! = {{name: "x"}}   # Dog has just `name`
let p: Pet! = d
case (p) { x: Dog => x.name; c: Cat => c.meow }   # before: "exhaustive case did not match the value"

Type names were typed as instances of the type. Interface, union, enum and scalar names, builtin JSON/YAML/TOML included, are now typed as their namespace (meta-type).

  • Named passed for a Named! and crashed on .name.
  • Color passed for a Color!.
  • Color.RED.GREEN and Email.domain type-checked and crashed.
  • describe(JSON) type-checked and crashed.
  • A case arm n: Named matched the namespace value at runtime.

Enum values now have no members, and private statics stay private when the name is held in a variable.

Invalid enum literals failed only at runtime.

  • let c: Color! = "PURPLE" is now a compile error, as are argument, list, map, cast and case literals: "PURPLE" is not a value of Color; expected one of RED, GREEN.
  • Casting a non-literal String is still checked at runtime.

Equality between unrelated types always answered false. It's now a compile error; string literals convert to the scalar or enum they're compared with. See release notes.

Smaller fixes:

  • A method declared before a field was taken for a constructor parameter at runtime, so Impl(3) failed with "required field v has no value".
  • dang fmt printed an interface method's block parameter as f: (a: Int!): String!, which doesn't parse.

Release notes (behaviour changes)

  • Equality is type-checked. ==/!= only accept operands that could be equal:
    • allowed: the same type up to nullability, Int/Float (they compare numerically), a subtype against its supertype or interface, a union against its members, and null;
    • rejected: 1 == "1", unrelated object types, records of different shapes, a named object vs a record, two different scalars;
    • a string literal compared with a string-backed scalar (custom, ID, JSON, …) or an enum value converts to it, as literals already do for arguments and case patterns: node.id == "User:1", value.contents == "null" and c == "GREEN" all work. An enum literal must name a value: c == "PURPLE" is a compile error ("PURPLE" is not a value of Color; expected one of RED, GREEN). c == Color.GREEN is still the clearer form for enums;
    • behaviour change: c == "GREEN" for an enum value was always false on main; it's now true when c is GREEN;
    • a non-literal String compared with a scalar or enum needs a cast, with a hint: id == (s :: ID!), c == (s :: Color!);
    • in this repo, 10 comparisons across 4 test files compared genuinely mismatched types and were fixed; the casts written for scalar literals during review still work (a few were switched back to plain literals).
  • Block parameter names: a block parameter named like a different parameter of its signature is rejected ({ b, a => } for (a, b)), rather than silently binding by position.
  • Interface positional order (runtime change): when an implementation orders same-typed parameters differently from its interface, a positional call through the interface now follows the interface's order. Before, it followed the implementation's order, which disagreed with named calls.
  • Records and GraphQL input objects: a record literal can still be passed for a GraphQL input object of exactly its shape. That's the one exception to records no longer standing in for named types.
  • Interfaces: let members don't implement public fields; implementations' block parameters must match the interface's names and accept its block type.
  • Calling a value that may be one of several functions with different signatures (if (c) { &f } else { &g }) gets an explanatory error.

Known gaps (filed)

Closes #227: a case naming every member of an enum (Color.RED, an imported bare RED, or "RED"), or true and false for a Boolean, is now exhaustive (non-null, no else); a nullable operand (enum, Boolean or union) is exhaustive with a null => pattern as well.

Also: an implementation that declares an interface's reserved id: ID! field is now type-checked like any other field (id: String! used to be accepted).

Function values must name their parameters identically and in the same
order, unlike interface conformance (which matches by name in any order).
This is intended.

Testing

Regression tests for each fix live under tests/ (should-pass) and
tests/errors/ (with goldens). pkg/dang, pkg/hm, pkg/lsp, cmd/dang,
docs/go and tests pass; docs and the language/internals skills are
updated.

vito added 28 commits October 31, 2026 21:08
extractConstructorParameters also runs at eval, after inference has wrapped
some method values in a Coerce, so a method declared before a field (e.g.
`cb(x: Int): Int! { ... }` then `v: Int!`) was taken for a constructor
parameter: `Impl(3)` bound 3 to `cb` and failed with "required field v has
no value", although it type-checked against the inferred constructor type.
Look through the Coerce with funDecl().

Signed-off-by: Alex Suraci <[email protected]>
A signature without a body (an interface method like
`visit(&f(a: Int!): String!): String!`) printed its block parameter as
`f: (a: Int!): String!`, which doesn't parse.

Signed-off-by: Alex Suraci <[email protected]>
Function types were unified component-wise in the same direction as the
function, so a function accepting only `Dog!` could be stored where one
accepting any `Animal!` was expected, and crashed when called with a Cat.
Arguments and the block argument are now checked contravariantly, the result
covariantly, without value-level coercions.

Interface conformance never compared block parameters at all: an
implementation could call its block with a supertype of what the
interface's callers' blocks accept. Check them contravariantly too, and
reject an implementation requiring a block the interface doesn't pass.

Stricter only: programs this rejects could go wrong at runtime.

Signed-off-by: Alex Suraci <[email protected]>
Calls may name their arguments, and arguments are bound by name, yet
function types were compared by position only. So:

- a block called by name with the signature's names (`block(x: 42)` for
  `&block(x: Int!)`) got null for a block written `{ y => ... }`, typed
  Int!;
- a function with differently named (or swapped) parameters could be
  stored in a function-typed field and then had its arguments crossed;
- an object's method called positionally through an interface bound
  its arguments in the object's parameter order, not the interface's,
  though conformance only matches parameters by name.

Function types now only unify when their parameter records have the same
names in the same order (hm.Labelled). A block takes on the parameter
names of the signature it's passed for, binding them to its own names
locally, so blocks keep working with any names. Positional arguments bind
by the name inference resolved against the callee's static type, so a call
through an interface follows the interface's order. Interface conformance
also requires an object's block parameter to name its parameters like the
interface's.

Mostly stricter. Changes meaning only where an interface and an
implementation order same-typed parameters differently: a positional call
through the interface used to bind them in the implementation's order.

Signed-off-by: Alex Suraci <[email protected]>
An anonymous record compared structurally against any module, so a record
literal with the same fields as a named type or interface was accepted as
one: `let d: Dog! = {{name: "x"}}` type-checked, yet the value isn't a Dog
at runtime, so `==` against a Dog, `case` type patterns and exhaustive case
over a union containing Dog all miss it ("exhaustive case did not match").
Named types are nominal; a record now only matches records.

Also give the record mismatch errors a message of their own: they printed
"  is structurally compatible with Dog but  is not declared as an
implementation", with the record's empty name.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
A `let` member satisfied an interface field, so `let name` on a type
implementing Named was readable by anyone through a Named value. Anyone
holding the interface may read its fields, so its implementations must
declare them public.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
An interface or union name is a runtime value (`Named != null`), but it was
typed as an instance of the type, so it could be passed wherever a Named
was expected, or narrowed by `!= null` and read from: `greet(Named)`
crashed with `module "module Named" does not have a field "name"`, and a
`case` over a union given the union's name matched no member.

Type the name as the type's meta-type instead, the type of a type name used
for statics: it has no members and isn't an instance. Also for interface and
union names imported from a GraphQL schema. LSP hover still describes the
type the name refers to.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
An enum's type served both as the type of its values and as the type of its
name, the namespace holding them, so each was taken for the other:

- `Color.RED.GREEN` and `someColor.values` type-checked and crashed
  ("cannot select field GREEN from dang.EnumValue");
- `Color` itself passed as a `Color!` value (`paint(Color)`), reaching
  `case` and GraphQL as a module object.

Type an enum's name as its meta-type, holding copies of its values and
`values`, for Dang, GraphQL and builtin enums alike, and give enum values no
members. The enum's own table keeps its values for hover, imports and
literal validation.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
Like interfaces and unions, a custom scalar's name (one without new()) was
typed as a value of the scalar, nullable: `Email.domain` called an instance
method with the namespace object as `self` and crashed (`module "module
Email" does not have a field "domain"`), and the name narrowed by `!= null`
passed for an Email anywhere. Type it as the scalar's meta-type, which holds
its statics. Builtin scalars (JSON, ...) keep doubling as their namespace.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
The previous commit made named types nominal, which also stopped record
literals from being passed for GraphQL input objects (`sortedUsers(sort:
{{field: ..., direction: ...}})`). Input objects are plain data with no
identity, so a record of their exact shape still is one.

Signed-off-by: Alex Suraci <[email protected]>
A scalar without new() now has its name typed as its meta-type, which holds
its statics, but the name was still bound to an empty object at runtime, so
`let ns = Tag; ns.sep` type-checked and then crashed. Bind the name to the
statics namespace when the scalar declares statics.

Signed-off-by: Alex Suraci <[email protected]>
Only an interface's public fields are readable through it, so a private
interface member may still be implemented by a private one.

Signed-off-by: Alex Suraci <[email protected]>
Enum, interface, union and scalar names are now bound to their meta-types,
of ObjectKind, so completion took them for plain values: lexical completion
offered every schema enum/interface/union/scalar name, the REPL doc browser
classified them as types and listed no fields for interfaces. Look through
the meta-type to the type it names. Also offer no members on an enum value,
which has none.

Signed-off-by: Alex Suraci <[email protected]>
Reading an instance member off a type's name (`Email.domain`) reported
`field "domain" not found in Email`, which contradicts `e.domain` working,
since the meta-type prints as the type's name. Say the field belongs to
values of the type instead.

Signed-off-by: Alex Suraci <[email protected]>
Blocks now take on their signature's parameter names by position, so in
`two { b, a => ... }` for `&f(a: Int!, b: Int!)` a call `f(a: 1, b: 2)`
binds the block's `b` to 1, where it used to bind it by name to 2. Rather
than silently change what such programs compute, reject a block parameter
named like a different parameter of the signature.

Stricter only; together with the previous commits, named calls to blocks
no longer change meaning for any program that still compiles.

Signed-off-by: Alex Suraci <[email protected]>
Format the record literal added by "types: keep records usable as input
objects".

Signed-off-by: Alex Suraci <[email protected]>
With a scalar's name typed as its meta-type, reading a member off the name
held as a value (`let tags = Lib.LibTag; tags.secret`) resolved `secret` on
the meta-type and checked it like an instance member of a type with no home
module, so a `let` static of another module was readable. Check members of
a meta-type as statics of the type it names.

Signed-off-by: Alex Suraci <[email protected]>
A child interface could redeclare a parent's public field as `let`, letting
an implementation's private member through the child and then out through
the parent. Apply the same visibility rule to interface extension.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
Composing the substitutions of a function's parameter and result pairs
widens a variable bound on both sides to a common supertype, so
`ident(d: a): a` passed for `(d: Animal!): Dog!` (a := Animal!), and
returned a Cat where a Dog was promised. Check every pair again under the
combined substitution.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
An interface's or union's name is typed as its meta-type, but its runtime
value was still an object of the type itself, so in an inline union like
`if (flag) { Named } else { Foo }` a `case` arm `n: Named` matched the
namespace and `n.name` crashed. Make the runtime value an object of the
meta-type too, for Dang and GraphQL-imported types.

Signed-off-by: Alex Suraci <[email protected]>
Since parameter names are part of function types, branches yielding
functions named differently (`if (c) { &f } else { &g }`) widen to a union
of them, and calling it failed with "FunCall.Infer: expected function, got
... (*hm.UnionType)". Say what is wrong instead.

Signed-off-by: Alex Suraci <[email protected]>
A record with an input object's fields but a nullability mismatch (`Int!`
for an optional `Int`) was told it "must have exactly the input's fields".
Say the field types must match too, nullability included.

Signed-off-by: Alex Suraci <[email protected]>
Document the rules the soundness fixes enforce: function-typed slots take
functions with contravariant parameters named like the target's, blocks
take on their signature's names, `let` members don't implement public
interface fields, implementations' blocks are checked like arguments, and
type names are namespaces rather than instances. Bring the internals skill's
subtyping section up to date (Type.Eq is equality; subtyping goes through
Supertypes; meta-types for type names).

Signed-off-by: Alex Suraci <[email protected]>
A string literal handed to an enum (`let c: Color! = "PURPLE"`, an argument,
a list or map element, a literal cast) type-checked whatever it said and
only failed when materialized at runtime. Its value is known, so check it
against the enum's values when the coercion is accepted, and point at the
literal. Casting a non-literal string stays a runtime check.

Enum values are now declared with the enum's name in hoisting pass 0, since
they depend on nothing else and top-level constants, which may hand such
literals to the enum, are inferred before pass 1.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
The builtin scalars' names (JSON, YAML, TOML) were typed as values of the
scalar, so `describe(JSON)` passed the namespace object for a JSON value and
crashed, and a JSON value (e.g. from a schema field) had `encode`/`decode`
members it doesn't have at runtime. Type the names as the scalar's meta-type
and put Dang's members there instead, in the Prelude, for schema-imported
scalars and for user-declared scalars of those names; `JSON.encode` is read
off the namespace value as before (not the statics path). The docs linker
looks through the meta-type to link `JSON.encode`.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
`==` and `!=` accepted operands of any two types, and between unrelated
types answered false (or true) whatever the values: `c == "GREEN"` for an
enum, `1 == "1"`, two distinct object types, records of different shapes.
They now only type-check when the operands could be equal: the same type
up to nullability, Int with Float (they compare numerically at runtime), a
type with its supertype or interface, a union with its members, two
interfaces/unions, or anything involving a type variable (`null`,
generics). An enum against a String suggests the typed form
(`Color.GREEN`), a scalar against a String suggests a cast.

This also rejects a custom scalar or ID against a String, which does
compare by value at runtime; that case is relaxed for string literals in
the next commit.

`case` keeps converting string-literal patterns; a pattern literal that
names no value of the enum now reports that, at the literal, instead of
"Case.Infer: clause N value type mismatch".

In-repo breakage: 17 test cases (16 tests/*.dang files and one Go test),
43 comparisons, fixed here: cross-type assertions in test_comparisons,
test_equality and test_object_equality (the runtime semantics are kept
through a generic helper), and scalar/ID-vs-literal comparisons rewritten
as casts. Docs and the language skill describe the rule; prelude, demos
and .dagger needed nothing.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
Comparing a scalar with a String suggests a cast; quote the String operand
as written instead of `...` for non-literals: "cast the literal to compare:
("User:1" :: ID!)", "cast the string to compare: (s :: Email!)". Error
tests cover both.

Signed-off-by: Alex Suraci <[email protected]>
Since enum string literals are checked against the enum's values when they
type-check, `"NOPE" :: Status!` is a compile error; only casting a
non-literal String is still validated at runtime. Fix the docs and the
language skill, which said all enum casts were runtime-checked.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 5e9f1a6
Status: ✅  Deploy successful!
Preview URL: https://12c9117e.dang-3kk.pages.dev
Branch Preview URL: https://soundness-subtyping.dang-3kk.pages.dev

View logs

Conformance skipped an interface's `id: ID!` field outright, since Dagger's
runtime supplies it to implementers that leave it out. That also skipped
implementations that do declare it, so `type W implements T { id: String! }`
was accepted and `w.id` read through T was a String typed ID!. Only skip
the field when the implementation doesn't declare it; same for interface
extension. No other scalar pair has this hole: Int/Float, ID/custom,
custom/String, enum/String and their list, argument and return positions
were already rejected.

Stricter only.

Signed-off-by: Alex Suraci <[email protected]>
vito added 2 commits November 1, 2026 18:32
A case on an enum operand whose value patterns named every member, or on
a Boolean with both `true` and `false`, still had a nullable result and
needed an `else`. Value patterns now count toward exhaustiveness: enum
members by `Color.RED` (read off the enum's namespace), a bare imported
`RED` (only the enum's own binding, not a local of that name), or a
string literal; `true`/`false` for Boolean. A nullable operand (enum,
Boolean or union) is exhaustive with a `null` pattern as well, so a
nullable union matched by type patterns covering every member plus
`null =>` is now non-null too.

Docs and the language skill no longer say there's no exhaustiveness check
for enums.

Closes #227

Signed-off-by: Alex Suraci <[email protected]>
A string literal compared with a string-backed scalar (custom, ID, JSON,
...) or an enum value now converts to that type, as literals already do
for arguments, typed slots and `case` patterns: `node.id == "User:1"`,
`value.contents == "null"`, `c == "GREEN"`. An enum literal must name one
of the enum's values (`c == "PURPLE"` reports `"PURPLE" is not a value of
Color; expected one of RED, GREEN`); a scalar literal goes through the
scalar's new() hook like any other. A non-literal String compared with a
scalar or enum is still rejected, with a cast hint, as is everything else
the rule rejects (`1 == "1"`, unrelated objects).

Behaviour change: `c == "GREEN"` for an enum value `c` was always false
on main (an enum value never equalled a string); it is now true when `c`
is GREEN.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the soundness-subtyping branch 2 times, most recently from e7f92d2 to 5e9f1a6 Compare November 1, 2026 22:23
@vito
vito merged commit c4ab5db into main Nov 1, 2026
9 checks passed
sipsma added a commit to grouville/dagger that referenced this pull request Nov 3, 2026
Since vito/dang#235 (761f6710), Dang binds an interface's name to its
namespace: an object of the interface's meta-type, which is an object
type with no members. Registering a module's types finds its interfaces
by the kind of each public binding's type, so it skipped them all. A
module's own interfaces were missing from its API, and its consumers
failed to load: "interface DepGreeter not found" for a type implementing
a dependency's interface.

Use the type a meta-type describes, the interface itself, both to find
the interface and to build its type definition.

Signed-off-by: Erik Sipsma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exhaustive enum/Boolean case is still nullable

1 participant