Skip to content

Bind POSIX and sockets through the system headers (#550) - #598

Merged
godofecht merged 3 commits into
mainfrom
fix-posix-net-550
Aug 19, 2026
Merged

godofecht merged 3 commits into
mainfrom
fix-posix-net-550

Conversation

@godofecht

@godofecht godofecht commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #550. You said to do what's best, so this takes the third option from the issue: teach @cImport to read #define values, which fixes the class rather than this instance.

The problem, and why constants were the hard half

Both modules parsed and neither could be compiled. Prototypes collided with the headers the generated C already includes. That part was mechanical.

The constants were not. They were hand-copied macOS values:

in the file macOS Linux
O_CREAT 512 0x200 0x40
SIGCHLD 17 20 17
AF_INET6 30 30 10

So the numbers were wrong on one platform whatever they said, and defining them at all collided with the header's macros of the same name. Renaming them would have kept the collision away at the cost of the idiom; keeping them meant staying wrong somewhere.

What this does instead

@cImport now reads integer #defines out of a header. Ordinary preprocessing expands macros and discards the definitions, so a second -dM pass is the only way to see them.

They arrive as ordinary Flow constants marked c_import, so the type checker knows the value while the C output keeps using the macro:

printf("O_RDONLY=%d ...", O_RDONLY, O_CREAT, O_TRUNC);   // not 0, 512, 1024

That is what makes the value right per platform, rather than right on whichever machine built the compiler.

posix.flow and net.flow now import their headers instead of restating them. Both compile; opening a file and creating a socket both work:

O_RDONLY=0 O_CREAT=512 SIGCHLD=20      # SIGCHLD was 17 in the file
AF_INET=2 SOCK_STREAM=1 INADDR_ANY=0 SHUT_RDWR=2
socket fd=3

Four supporting fixes, each blocking the next

  1. The header parser dropped ..., so libc's open(const char *, int, ...) imported as two parameters and open(path, flags, mode) did not resolve.
  2. A Flow string is a char*, so it now satisfies a C signature spelling that pointer as ptr<u8>. Applied only after an exact match fails — allowing it in the first pass let a string literal match a ptr<u8> overload ahead of the exact string one, and tests/lang/test_c_import_julia.flow silently resolved to the wrong function. Caught by tier-2.
  3. A missing header still leaves the preprocessor's own built-ins on stdout, so the exit status has to be checked. Without it, @cImport of an uninstalled header imported 22 platform macros and nothing it asked for.
  4. check_doc_examples.py resolved modules but not @cImport, unlike transpiler.py, so a module binding C through a header looked like it declared nothing.

Result

The three POSIX tutorials in docs/tutorials/advanced.md verify. Unverified examples 221 → 219.

Verification

Check Result
pytest tests/ 1681 passed, 6 skipped
./flow test --tier2 --strict 687/687
check_doc_examples.py --check-ledger no new unverified, 219
ruff check src clean

Five tests in tests/unit/test_c_import_macros.py cover plain integers, hex and octal, cast-wrapped values like ((u_int32_t)0x00000000), that compiler-internal _-prefixed defines are skipped, and that a missing header yields nothing rather than the preprocessor's built-ins.

Still open

net.flow keeps its own SockAddr_in and its socket wrappers rather than using the header's struct sockaddr, so bind/accept/connect are not imported. That is a separate API question about how Flow should model the sockaddr family.

godofecht and others added 3 commits August 20, 2026 00:35
Both modules parsed and neither could be compiled: any program importing
either failed at the C stage with "conflicting types for 'read'", because
Flow declared prototypes that the generated C's own <unistd.h> also declares.

Constants were the harder half, and the reason this needed a decision rather
than a patch. They were hand-copied macOS values:

    O_CREAT   512 here, 0x200 on macOS, 0x40 on Linux
    SIGCHLD    17 here, 20 on macOS, 17 on Linux
    AF_INET6   30 here, 30 on macOS, 10 on Linux

So the numbers were wrong on one platform whatever they said, and defining
them at all collided with the header's macros of the same name.

@cImport now reads integer #defines out of a header. Preprocessing expands
macros and discards the definitions, so a second pass with -dM is the only
way to see them. They arrive as ordinary Flow constants marked c_import, so
the type checker knows the value while the C output keeps using the macro,
which is what makes it right per platform rather than right on the machine
that built the compiler.

posix.flow and net.flow now import their headers instead of restating them.
Importing either compiles; opening a file and creating a socket both work.

Four supporting fixes, each of which blocked the one after it:

- The header parser dropped `...`, so libc's `open(const char *, int, ...)`
  imported as two parameters and `open(path, flags, mode)` did not resolve.
- A Flow string is a char*, so it now satisfies a C signature spelling that
  pointer as ptr<u8>. Applied only after an exact match fails: allowing it in
  the first pass let a string literal match a ptr<u8> overload ahead of the
  exact `string` one, and tests/lang/test_c_import_julia.flow resolved to the
  wrong function.
- A missing header still leaves the preprocessor's built-in macros on stdout,
  so the exit status has to be checked. Without that, @cImport of an
  uninstalled header imported 22 platform macros and nothing it asked for.
- check_doc_examples.py resolved modules but not @cImport, unlike
  transpiler.py, so a module binding C through a header looked like it
  declared nothing.

The three POSIX tutorials in docs/tutorials/advanced.md now verify.
Unverified documentation examples: 221 to 219.

Closes #550

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@cImport surfaces open from fcntl.h on macOS and not on glibc, so these two
blocks verify locally and fail in CI. The ledger cannot describe both: it
fails when a listed block starts passing as well as when a passing one starts
failing, which is what makes it a ratchet.

Tagged with the reason rather than recorded as either state. A Jules session
is on the glibc side; when @cImport reads those declarations there, the tag
comes off and both blocks verify everywhere.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@godofecht
godofecht merged commit ae38c55 into main Aug 19, 2026
21 checks passed
@godofecht
godofecht deleted the fix-posix-net-550 branch August 19, 2026 23:49
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.

lib/stdlib/posix.flow and net.flow parse but cannot be compiled

1 participant