Skip to content

fix(ifc): enforce labels on fields of imported declared types - #2626

Merged
sunholo-voight-kampff merged 2 commits into
devfrom
fix/ifc-imported-type-labels
Dec 2, 2026
Merged

sunholo-voight-kampff merged 2 commits into
devfrom
fix/ifc-imported-type-labels

Conversation

@sunholo-voight-kampff

@sunholo-voight-kampff sunholo-voight-kampff commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

An IFC label declared on a field of a record, alias or ADT was a source label only when the type was declared in the same module. If the type was imported, labelled data reached a {not ℓ} sink and ailang check accepted it. This was reported by docparse on v0.60.4 and reproduced at dev HEAD (a66b854).

Repro: before and after

-- ifct/lib.ail
module ifct/lib
export type Doc = {body: string<pii>, title: string}

-- ifct/main.ail
module ifct/main
import ifct/lib (Doc)
import std/io (println)
func piisink(s: string{not pii}) -> unit ! {IO} = println(s)
func useDoc(d: Doc) -> unit ! {IO} = piisink(d.body)
export func main() -> unit ! {IO} = useDoc({body: "x", title: "t"})
imported Doc Doc declared in main (control)
HEAD ✓ No errors found! (rc 0) information-flow violation: value labelled <pii> reaches parameter "s" which forbids it (rc 1)
this PR ifct/main.ail:7:45: information-flow violation: value labelled <pii> reaches parameter "s" which forbids it (string{not pii}) (rc 1) unchanged (rc 1)

Root cause

The IFC pass reads declared labels from ast.TypeDecls, but ifcTypes indexed only the module's own declarations, so an imported type's deep label was ⊥. The interface could not supply the missing labels either. The builder does convert type bodies with their labels. closeInterfaceTypes then replaces every alias body with the elaborator's, and the elaborator strips labels on purpose, because those bodies feed type checking. So the "labels survive in iface" path was dead for records and aliases.

Fix: one reader for local and imported labels

  • iface: a module whose types reach a label publishes its own type declarations, labelled and unexpanded, in a new IFC-only field Iface.IFCTypes. "Reach" means a label written anywhere in the type, function-type parameters included, or one carried by a type it names, including imported ones. One function, iface.PublishedIFCTypes, makes this decision for both the CLI builder and the REPL/WASM loader. Typing still reads the label-free TypeAliases, so no TLabelled reaches the HM core or the backend. Modules with no labelled types publish nothing, so their interfaces are byte-for-byte unchanged (see the stdlib section below).
  • types: IFCTypeEnv rebuilds imported declarations as ast.TypeDecl, keyed by nominal identity. ifcTypes.decl()/ctor() resolve a name locally first, then through the module's own TypeScope, the resolver the elaborator used, via a new side-effect-free TypeScope.Peek. Every existing lookup (deepLabel, recordTypeOf/fieldType, paramBound, dataOnly, funcTypeOf, constructor typing) now goes through those two methods, so imported and local labels are read by the same code.
  • pipeline: builds the env from the import closure plus the constructor bindings, and hands it to both the IFC gate and the export summaries (AnalyzeModuleIFCWithImports). The cached iface JSON carries ifc_types, the dependency cache key includes its digest, and cacheKeyVersion goes from v16 to v17.
  • REPL/WASM loader: each registered module records what iface.PublishedIFCTypes decides. Importers read it, plus label-free alias bodies and constructors, through the same iface.AddIFCTypes the CLI uses. The type scope is now built before the IFC gate.
  • CI fix: TestCompileCacheClear_Artifacts seeded a manifest pinned to "v16". It now reads the version from a new pipeline.CacheFormatVersion() accessor, so the next bump cannot strand it. No other test pins the cache version; the observatory's v16 is its DB schema, which is unrelated.

Audit

Each case was checked with a HEAD build and with this PR's build. They are all regression rows in TestIFCImportedTypeLabels.

Case HEAD This PR
imported record field (the report) accepted ✗ rejected ✓
imported ADT constructor field (match) accepted ✗ rejected ✓
type re-exported by a facade (export import) accepted ✗ rejected ✓
local alias of an imported type (type D2 = Doc) accepted ✗ rejected ✓
imported alias of an imported type accepted ✗ rejected ✓
generic imported type (Box[int]) accepted ✗ rejected ✓
module-alias qualified type (L.Doc) accepted ✗ rejected ✓
label reached through an unlabelled intermediate module accepted ✗ rejected ✓
private type named by an exported one accepted ✗ rejected ✓
imported non-record alias (type Secret = string<pii>) accepted ✗ rejected ✓
imported function type labelled only on a parameter (Handler = (string<sqlsafe>) -> string) accepted ✗ rejected ✓
imported function returning the type (mkDoc().body) rejected ✓ rejected ✓
unlabelled field of an imported record (d.title) accepted ✓ accepted ✓ (still field-precise)

Still open (not the same root cause): a {not ℓ} refinement written inside an imported type, on a field or on a function type's parameter (Logger = (string{not pii}) -> unit ! {IO}), does not bind a parameter that the importer declares with that type. Refinements are not stored on types at all (astTypeToInternalType drops them), so fixing it needs a refinement representation in the interface. An imported function with such a parameter is still enforced through its export summary (verified). The IFC guide's limitation line now describes this gap instead of the fixed one.

Soundness: newly rejected programs

Yes, intentionally. The programs that are now rejected are exactly those that would be rejected if the imported type were declared in the importer:

  • a labelled field of an imported type reaching a sink that forbids the label;
  • an argument not covered by a label on an imported function type's parameter;
  • an unlabelled field of an imported generic record that has labelled fields (b: Box[string], Box[a] = {v: a, tag: string<pii>}: piisink(b.v) is rejected, because generic records are not field-precise);
  • the arm of an unlabelled constructor of an imported ADT that has a labelled one (match w { W(s) => …, Plain(s) => piisink(s) }: match destructuring gives each binding the scrutinee's full label).

The last two are the existing same-module imprecision, now applied to imported types. Each is a row in TestIFCImportedTypeLabels, paired with its same-module control.

  • ailang check over all 591 examples/**/*.ail with the HEAD binary and this PR's binary gave 512 accepted and 79 rejected for both, with 0 verdict differences (rerun after the follow-up).
  • make verify-examples is green.

Stdlib / freeze

No stdlib type declares a label (the only <label> in std/ is a function return type in std/secret), so no std module publishes IFCTypes. make verify-stdlib reports: ✓ All 49 stdlib interfaces stable. No re-freeze.

Tests

  • internal/pipeline/ifc_imported_type_labels_test.go
    • TestIFCImportedTypeLabels: the repro, a same-module control, the 9 other rejected audit cases, and 3 clean controls (unlabelled field, unlabelled sibling through mid, local declaration shadowing an imported name).
      It also has the Handler repro (a label only on a function type's parameter) with its same-module control, and the generic-record and ADT-arm verdict changes, each with its same-module control.
    • TestIFCImportedTypeLabelsWarmDependency: lib and mid are served from the compile cache and the importer is still rejected.
    • TestIFCImportedTypeLabelAddedToWarmDependency: the importer is cache-warm and unchanged; a label is added to a field of the dependency's type; the importer must now be rejected.
  • internal/repl/ifc_imported_type_labels_test.go
    • TestLoadModule_IFCImportedTypeLabels: record field, ADT field, through an unlabelled module, the Handler repro, and a clean control.
  • cmd/ailang/serve_api_mcp_surface_test.go: TestCompileCacheClear_Artifacts reads the manifest version from pipeline.CacheFormatVersion().

Mutation testing

Each file was copied to a scratch backup, mutated in place, tested, then restored from the backup.

Mutation Result
ifcTypes.decl ignores imported declarations 10 pipeline rows + warm test + 3 REPL rows fail
builder never publishes IFCTypes 9 rows + warm test fail (ADT row survives: constructor fields already kept labels)
cache decode drops IFCTypes warm test fails
pipeline leaves IFCImports.Types unset 10 rows + warm test fail
REPL importer ignores registered IFCTypes 3 REPL rows fail
publish gate ignores imported labels (IFCTypesCarryLabels(file, nil)) "through an unlabelled module", "imported alias of an imported type" and warm test fail
closed named-record → nominal-reference mapping removed survived → I removed that branch as unreachable
local-before-imported order swapped survived: equivalent mutant, because imported names resolve through the TypeScope, which already ranks local declarations first
follow-up: publish predicate counts only a function type's return (the reviewed bug) Handler row fails in both TestIFCImportedTypeLabels and the REPL test
follow-up: publish predicate does not follow named types "imported alias of an imported type", "through an unlabelled module" and the warm test fail
follow-up: REPL publishes nothing (drift from the CLI) 4 REPL rows fail
follow-up: +ifc: part of cacheDepDigest removed TestIFCImportedTypeLabelAddedToWarmDependency fails: the importer is served a stale accept (err = <nil>)
follow-up: CacheFormatVersion() returns "v16" TestCompileCacheClear_Artifacts/success fails with "Cleared 0", the CI symptom

Known, not fixed here: IFCTypes is excluded from the interface Digest and the hash projection, so a label change on a type field does not change a published package's identity (function summaries do). This predates the PR: the compile cache key carries the IFCTypes digest separately.

Gates

make test-changed ✓ · make test-core ✓ · make lint ✓ (0 issues) · make check-file-sizes ✓ · make verify-stdlib ✓ · make verify-examples ✓ · make check-changelog ✓ · make check-boundaries ✓ · GOOS=js GOARCH=wasm go build ./cmd/wasm ✓

🤖 Generated with Claude Code

A label declared on a field of a record, alias or ADT was a source label
only when the type was declared in the same module: for an imported
`Doc = {body: string<pii>, ...}`, `piisink(d.body)` passed `ailang check`.

The IFC pass indexed only the module's own type declarations, and the
interface could not supply an imported one's labels: closeInterfaceTypes
replaces each alias body with the elaborator's, which is label-free because
it feeds type checking.

A module whose types reach a label now publishes its declarations,
labelled and unexpanded, in a new IFC-only iface field (IFCTypes; absent
otherwise, so stdlib interfaces are unchanged). The importer resolves an
imported type name through its own TypeScope (new side-effect-free Peek)
into those declarations, and every IFC lookup goes through one
decl()/ctor() pair, so local and imported labels are read by the same code.
Wired for the CLI gate and export summaries (cache key v17; dep digest
covers the labels) and for the REPL/WASM loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e for CLI and REPL

Review follow-ups to the imported-type label fix:

- IFCTypesCarryLabels now counts a label written anywhere in a type,
  function-type parameters included (it used deepLabel, which counts only a
  function's return). `type Handler = (string<sqlsafe>) -> string` alone was
  not published, so Check C on an imported handler was silently lost.
- iface.PublishedIFCTypes is the single decision of what a module publishes;
  the interface builder and the REPL/WASM loader both call it, and the REPL
  importer reads a registered module in the CLI's interface shape (published
  declarations plus label-free aliases and constructors).
- Regression test: adding a label to a cache-warm dependency's type re-checks
  the unchanged importer (pins the +ifc: part of cacheDepDigest).
- Changelog names the verdict changes precisely (imported generic records are
  not field-precise; unlabelled arms of an imported ADT with a labelled
  constructor), both mirroring same-module behaviour.
- TestCompileCacheClear_Artifacts reads the manifest version from the new
  pipeline.CacheFormatVersion() instead of a hard-coded "v16".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sunholo-voight-kampff
sunholo-voight-kampff merged commit 9b11d18 into dev Dec 2, 2026
23 of 24 checks passed
@sunholo-voight-kampff
sunholo-voight-kampff deleted the fix/ifc-imported-type-labels branch December 2, 2026 17:30
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.

1 participant