Skip to content

selfhost: port the Python type checker to flowc (sem_check) with a parity gate - #1071

Merged
godofecht merged 5 commits into
mainfrom
selfhost/typecheck-parity
Sep 30, 2026
Merged

godofecht merged 5 commits into
mainfrom
selfhost/typecheck-parity

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Ports src/flow/type_checker.py to flowc, rule by rule and message by message, so the Python checker can be deleted. This branch is built on port/remaining-python-commands (#1064), which is merged in; review after #1064 lands, or read the diff against that branch.

What changed

  • compiler/src/sem_check.flow, sem_model.flow, sem_types.flow, sem_util.flow, sem_cheader.flow: the type checker. It runs in single-file and bundle mode, strict by default and lenient under FLOWC_LENIENT=1 (only the errors Python kept fatal stop a lenient compile). Modules are checked in the Python resolver's DFS order. Error positions follow the Python lexer's line counting. @cImport headers are read with cpp for the names they declare.
  • typecheck.flow keeps only what C generation needs when the semantic check is on, so no diagnostic is printed twice.
  • FLOWC_CHECK_ONLY=1 stops after the type check.
  • A module compiled alone that has imports is checked with its imports resolved, as the Python host did. roundtrip.sh and the self-emit scripts build the new modules one object each.

Gates

  • compiler/scripts/parity_typecheck.sh: the Python verdict and first diagnostic, strict and lenient, for every tracked .flow, 169 snippets extracted from the Python unit tests, and every doc-example harness rung (4506 inputs), recorded as goldens from f102d88f. --check needs no Python and fails above the per-OS ceilings in compiler/fixtures/typecheck_parity/ceiling.txt.
  • compiler/scripts/typecheck_rules.sh: one negative program per ported rule, 113 fixtures in compiler/fixtures/typecheck_rules/. Each file's first line is the Python message it expects. A fixture fails against the checker before the port.

Five-way counts (strict / lenient)

class before after
agree-accept 2803 / 2978 2911 / 3153
agree-reject, same message 24 / 1 237 / 2
agree-reject, different message 34 / 0 1 / 0
python rejects, flowc accepts 102 / 0 5 / 0
flowc rejects, python accepts 16 / 0 1 / 0
front end (parse error in either) 1345 1353

"Before" is the old flowc against the first goldens (4324 inputs). The input set grew with the tree.

Remaining differences

All intended, and listed in ceiling.txt:

  • Python rejects, flowc accepts (5): language features the Python checker never typed. Chained assignment (flowc: chained assignment a = b = 1 does not parse #1036, two programs), generic struct literal inference (two programs), a module alias used as a value (one program). The checker types a chained assignment as its value.
  • Different message (1): the chained-assignment rule fixture, where Python typed the inner assignment unknown.
  • flowc rejects, Python accepts (1): the modules-namespacing.md block that defines gain in two modules. Python kept both as overloads and clang rejected the redefinition.

Doc examples

The doc checker compiles each block with flowc_emit.sh --strict and reports the first type error at stage types. The eight expect-error blocks that had fallen to unverified verify again. docs/generated/example-status.json now matches the Python checker's ledger on every status and stage; one detail differs and is listed in tests/doc_examples/KNOWN_DIFFERENCES.md.

Codegen gaps from #1064

Closes #1065. Closes #1067. Closes #1068.

Source fixes

The compiler, tools/, scripts/tools/ and a few library files now pass the strict check: casts such as null as string and as ptr<u8>, let mut where a binding is reassigned, and @unsafe extern for system, sprintf and realpath.

Gate results on this branch

bootstrap_from_c.sh --verify, self_host_full.sh, roundtrip.sh, parse_coverage.sh --check, fmt_check.sh --check, tests/cgen/run.sh, ./flow test-lang, typecheck_rules.sh, check_doc_examples.sh --check-ledger, tests/doc_examples/run.sh, corpus_parity.sh --check, check_run_without_python.sh, parity_typecheck.sh --check, the tool suites and every other parity_*.sh pass, with two exceptions that fail the same way on port/remaining-python-commands:

  • parity_shader_dsl.sh: the Metal step (xcrun metal rejects the gallery shader).
  • parity_mlir.sh: tests/lang/test_typecheck_overload_integration.flow MLIR changed since its golden was recorded.

parity_mlir.sh and its GPU-mode emit now run flowc lenient, as flow compile --mlir ran the Python checker.

Not done

  • CI wiring for parity_typecheck.sh --check and typecheck_rules.sh. The workflow file is guarded here; this needs a follow-up that edits .github/workflows/ci.yml.
  • test "..." { } blocks are not type-checked (the Python checker checked them).
  • The goldens are recorded on macOS. Another OS reports its counts without failing until it has ceilings.

…rity gate

Rebuilt on main from selfhost/typecheck-parity (#1071). The bootstrap C
is regenerated in the next commit.
ft_read_file returns null as string on the malloc failure path, which the
ported checker requires. The MLIR corpus is re-recorded from 84806b0:
test_typecheck_overload_integration.flow imports compiler/src/typecheck.flow,
so its MLIR follows that module.
@godofecht
godofecht force-pushed the selfhost/typecheck-parity branch from 5252b35 to 5086a82 Compare September 29, 2026 23:08
sem_define_function wrote tys.ret[f] = sem_parse_type(...). The emitted C
may load tys->ret before the call, and the call can grow the type table,
so gcc stored into the freed old array. glibc had handed that block to
the effects array, and flowc crashed on Linux building tools/lsp. Each
such store now evaluates the call into a local first.
@godofecht
godofecht merged commit ee5048d into main Sep 30, 2026
36 checks passed
godofecht added a commit that referenced this pull request Sep 30, 2026
Rebuilt on main from selfhost/mlir-finish (#1072) after #1071. The
scripts/tools programs follow the strict type check, and the tc_reject
cgen goldens expect the ported checker's diagnostics. The design note on
what the deletion left open now lists the three checks flowc --strict
still accepts. The bootstrap C is regenerated in the next commit.
godofecht added a commit that referenced this pull request Sep 30, 2026
The 11 stores from #1071 that went through a local to dodge gcc reading
the array before the call are plain assignments again. cgen now evaluates
a call on the right of a store through a pointer before it reads the
target.
godofecht added a commit that referenced this pull request Sep 30, 2026
… target (#1077)

cgen evaluates a call on the right of a store into a temporary before it reads the store's target. A call that grows the array under the store, like a type-table append in sem_check, no longer leaves gcc writing into the freed old array. The eleven hand-written locals added to sem_check for #1071 are gone, and tests/cgen and tests/lang each gain a store_after_call case.
@godofecht
godofecht deleted the selfhost/typecheck-parity branch October 1, 2026 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant