Skip to content

Sema: a module that uses c_import resolves the program's C types as it already resolves its C values (#1372) - #1526

Merged
ehartford merged 1 commit into
fix-1414-move-of-moved-placefrom
fix-1372-c-import-shared-types
Sep 23, 2026
Merged

ehartford merged 1 commit into
fix-1414-move-of-moved-placefrom
fix-1372-c-import-shared-types

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

A root module and a module it imports each use c_import(...). The
frontend deduplicates C declarations across every c_import of a
compilation (expand_c_imports_frontend: "Emitted names deduplicate
declarations only within this compilation"): cu.w's <stdio.h> expansion
declares c_uint, c_ulonglong, _opaque_pthread_attr_t, ..., and the
root's <stdlib.h> expansion names them without declaring them again. Those
declarations are cu.w's, and not pub.

Values were designed for this: symbol_visible_from_current returns 1 for a
c_import symbol when the current module itself uses c_import
(is_ci_visible, a6951bc "a c_import symbol is visible only when the current
module itself uses c_import"), so the root calls cu.w's atoi today. Type
lookup (lookup_named_type_filtered) had no such rule and applied module
privacy: the root failed with 825 errors ("'c_ulonglong' requires an
explicit import (§18.1); add: use std.re.defs.c_ulonglong", "symbol
'_opaque_pthread_attr_t' is private to module 'cu.w'").

Each named-type candidate now records whether a c_import expansion declared
it (named_type_candidate_ci, from decl_is_c_import of the declaring node),
and lookup_named_type_filtered, when no candidate is visible by the
ordinary rules — the module's own declarations first (§18.1) — returns a
c_import candidate to a module that uses c_import. Which modules use
c_import is registered before declarations are collected
(register_c_import_modules), since the root's expansion's types resolve
during collection; build_ci_scoping still derives the rest after it. A
module with no c_import still sees no C type (test_no_leak's rule).

The issue proposed the other model — every module's c_import surface its
own, complete expansion. That contradicts the value rule above and the
frontend's one-expansion-per-header dedup, and would make one C struct two
nominal With types in two modules; this change makes types follow the
existing rule instead. If per-module surfaces are wanted, that is a ruling
on the dedup and on is_ci_visible together.

Fixture: test/behavior/behav_1372_two_modules_c_import.w: the issue's two
modules run and print, and a module without c_import still cannot name
c_uint. It runs the programs from src/ by bare name: only then is module
privacy enforced at all (#1520, filed); spelled src/m.w, main passes the
issue's program on every platform. From src/, main fails it on darwin and
on native Windows alike.

Closes #1372

Stack #1528, bottom to top: #1522 (#1411) → #1523 (#1413) → #1524 (#1429) → #1525 (#1414) → #1526 (#1372) → #1527 (#1417). Each layer holds only its own commit.

Verification (iterate tier; no battery yet — draft until the seed-driven battery is green): stage1 and stage2 built from the stack top on darwin-aarch64 (SDKROOT CLT 15.4) and on native Windows 10 x64; fixtures below pass on both; check --validate-all swept over test/, examples/, lib/std (3188 files) with base stage2 (main f1e3bd2) vs the stack's stage2.

@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1528 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main + #1522 → … → #1527 (head 33e6ba5; every PR head is a tested commit): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merges cleanly into main. Merge the stack: gh stack merge 1527 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 16:45
…t already resolves its C values (#1372)

A root module and a module it imports each `use c_import(...)`. The
frontend deduplicates C declarations across every c_import of a
compilation (expand_c_imports_frontend: "Emitted names deduplicate
declarations only within this compilation"): cu.w's <stdio.h> expansion
declares `c_uint`, `c_ulonglong`, `_opaque_pthread_attr_t`, ..., and the
root's <stdlib.h> expansion names them without declaring them again. Those
declarations are cu.w's, and not `pub`.

Values were designed for this: symbol_visible_from_current returns 1 for a
c_import symbol when the current module itself uses c_import
(is_ci_visible, a6951bc "a c_import symbol is visible only when the current
module itself uses c_import"), so the root calls cu.w's `atoi` today. Type
lookup (lookup_named_type_filtered) had no such rule and applied module
privacy: the root failed with 825 errors ("'c_ulonglong' requires an
explicit import (§18.1); add: use std.re.defs.c_ulonglong", "symbol
'_opaque_pthread_attr_t' is private to module 'cu.w'").

Each named-type candidate now records whether a c_import expansion declared
it (named_type_candidate_ci, from decl_is_c_import of the declaring node),
and lookup_named_type_filtered, when no candidate is visible by the
ordinary rules — the module's own declarations first (§18.1) — returns a
c_import candidate to a module that uses c_import. Which modules use
c_import is registered before declarations are collected
(register_c_import_modules), since the root's expansion's types resolve
during collection; build_ci_scoping still derives the rest after it. A
module with no c_import still sees no C type (test_no_leak's rule).

The issue proposed the other model — every module's c_import surface its
own, complete expansion. That contradicts the value rule above and the
frontend's one-expansion-per-header dedup, and would make one C struct two
nominal With types in two modules; this change makes types follow the
existing rule instead. If per-module surfaces are wanted, that is a ruling
on the dedup and on is_ci_visible together.

Fixture: test/behavior/behav_1372_two_modules_c_import.w: the issue's two
modules run and print, and a module without c_import still cannot name
`c_uint`. It runs the programs from src/ by bare name: only then is module
privacy enforced at all (#1520, filed); spelled `src/m.w`, main passes the
issue's program on every platform. From src/, main fails it on darwin and
on native Windows alike.
@ehartford
ehartford force-pushed the fix-1372-c-import-shared-types branch from 1be67d4 to 33d278f Compare September 23, 2026 19:40
@ehartford
ehartford removed this pull request from stack #1528 September 23, 2026 19:43
@ehartford
ehartford added this pull request to stack #1563 September 23, 2026 19:43
@ehartford
ehartford merged commit c79a8b8 into main Sep 23, 2026
@ehartford
ehartford deleted the fix-1372-c-import-shared-types branch September 24, 2026 16:55
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.

Two modules that each c_import: the root's c_import expansion binds the other module's private C scalar aliases (c_uint, c_ulonglong)

1 participant