Skip to content

c_import: windows.h type-checks — bodies over opaque records omitted, mem* declared, predicate macros are C ints, macro parameters are bindings, __unaligned pointers untyped, object-macro sizeof is size_t (#1417) - #1527

Merged
ehartford merged 1 commit into
fix-1372-c-import-shared-typesfrom
fix-1417-windows-h-sema
Sep 23, 2026

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Native Windows 10 x64, stage2 of main 5848ae9: the bzip2 and libcurl
release UATs stop parsing windows.h — type PMETARECORD = *mut __unaligned struct tagMETARECORD (5 "expected declaration", 1 "expected ')'" in
PlayMetaFileRecord). Behind that are the classes #1417 lists from #1396's
branch (65 field accesses on opaque records and 6 null contexts, 2
undefined with_memset, WINAPI_PARTITION_DESKTOP's binding, the ENUMTYPE
parameter) and one more that surfaced once they cleared,
TOKEN_INTEGRITY_LEVEL_MAX_SIZE's "bitwise operands with mixed signedness".
With this commit both UATs pass with check there, and bzip2 runs ("bzip2
UAT passed"). libcurl's run then fails in codegen on an unrelated Windows
defect, #1495 (a c_import auto-method over an opaque struct asks for its
layout; four header lines reproduce it on main).

  1. __unaligned (ClangBridge.translate_type_recursive_mode). The record
    branch stripped const /volatile from clang's spelling but not
    MSVC's __unaligned , and the pointer branch passed the qualified
    pointee through, so the name was __unaligned struct tagMETARECORD.
    With has no unaligned pointer: a *T read assumes T's alignment, which
    the C contract says the address may not have (D51: never infer what can
    create unsafety). A pointer to an __unaligned pointee now imports as
    *mut c_void / *const c_void, the address unchanged; reading T
    through it names T with a cast. The record's own name drops the
    qualifier.

  2. Inline bodies over opaque records (CImport.ci_translate_function). A
    static/forced-inline body was translated without consulting the demoted
    set, so winnt.h's TpInitializeCallbackEnviron wrote the fields of
    _TP_CALLBACK_ENVIRON_V3 (it holds a bitfield record, so it imports
    opaque, §16.9). ci_cursor_needs_demoted_layout walks the definition: a
    field access through a demoted record (p->f, (*p).f, s.f), a value
    of its type (a local, a deref, a by-value parameter or result), or a type
    operand of sizeof/alignof/offsetof. Such a function is omitted with
    "inline body needs the layout of 'X', which c_import imports opaque
    (§16.9)", category inexpressible — an inline function has no symbol an
    extern could bind. ci_try_translate_fn_body's definition search is split
    out (ci_fn_definition_cursor) so the check and the translation share one
    search; migrate's call is unchanged.

  3. with_memset undeclared. The body lowering maps memcpy/memmove/memset/
    memcmp (and the __builtin___*_chk forms) to with_mem*
    (build_libc_call_value_expr), which the migrate preamble declares
    (CiMigrate) but the c_import preamble did not: it declared only
    with_alloc/with_alloc_zeroed/with_realloc/with_free. It declares the
    four with the rest. D30 retires this seam as a whole; this completes the
    existing preamble, it adds no new seam.

  4. Predicate macros (ci_translate_macros, object path). A system header's
    macro is never probed (probes run for non-system macros only), so
    #define WINAPI_PARTITION_DESKTOP (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP) reached the semantic path: clang's type
    c_int, the translation a With bool, "type mismatch in binding". C's
    comparison and logical operators yield an int 0 or 1, so the value
    follows the type: ci_fold_c_predicate folds it over the constants
    emitted so far (WINAPI_FAMILY itself was emitted let WINAPI_FAMILY: c_int = 100 by the semantic path, which does not extend known_values;
    literal_values records such lets for this fold only), else it is
    ((expr) as c_int). Every winapifamily.h partition folds (DESKTOP 1,
    PC_APP 1, PHONE_APP 0, GAMES 1, SYSTEM 0). An (if ...) spelling was
    avoided: a control-flow global initializer fails codegen, Codegen: a module-level let whose initializer has control flow (or / and / if) leaves an unterminated block in __with_init_const_* #1483.

  5. Macro parameters (ci_translate_macros, function-like path). Parameters
    were spelled with ci_escape_reserved only, so winnt.h's
    DEFINE_ENUM_FLAG_OPERATORS(ENUMTYPE) emitted fn ...(ENUMTYPE: i32),
    an uppercase-initial binding that parses as a variant pattern (§9.7).
    They take the wrappers' rule (ci_escape_param_name, p_ENUMTYPE), and
    when one is renamed the body names the binding
    (ci_macro_substitute_arguments). All-lowercase macros emit as before.

  6. ~ and sizeof in an imported object macro (ci_parse_unary_expr).
    winnt.h's `((((DWORD)(sizeof(TOKEN_MANDATORY_LABEL)) + sizeof(PVOID) -

    1. & (sizeof(PVOID)-1)) + SECURITY_MAX_SID_SIZE)rendered sizeof as With's signed i64 next to an unsigned cast, andxas(0 - x - 1), which underflows for an unsigned x. In an import is With's(type-preserving, like C's), and an object macro's sizeof crosses as the size_t it is,(sizeofT as usize)`; a function-like macro's body
      stays generic over T. Migration keeps both old spellings: the pcre2 and
      zlib outputs are pinned.

Migrate: every change to shared code is either gated off in migrate mode
(6), c_import-only (1's MSVC qualifier, 3), or reachable only by output
that never compiled (4, 5). pcre2 and zlib re-migrate byte-identically:
the base stage2 and this stage2 each drove with build :pcre2-migrate and
:zlib-migrate (the migration re-runs in-process under the driver's
comptime evaluator), and the 36 and 23 generated files match. (Both runs
then fail the same later step, and both differ from the committed
lib/std/re in the same two files; neither is this change.)

macOS: the five release UATs (bzip2, zlib, sqlite3, libcurl, raylib) check
with fresh translations, and bzip2, zlib, sqlite3 and libcurl run.

Fixtures: behav_1417_windows_h_shapes (a kept pointer-only inline beside
omitted ones, memset/memcpy bodies, uppercase macro parameters),
err_1417_inline_body_over_opaque_record (the omission's message),
c_import_predicate_macro_is_c_int and c_import_macro_sizeof_is_size_t
(check-only, over an extern variable, since a non-system macro over
constants is folded by clang instead), behav_1417_unaligned_pointer
(only-on: windows; clang takes __unaligned only in MS mode). All pass on
darwin and on native Windows.

Also filed: #1483 (control-flow global initializers), #1485 (stage1: a
user module's <string.h> memchr wrapper hijacks std.re's memchr), #1495.

Closes #1417

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
… mem* declared, predicate macros are C ints, macro parameters are bindings, __unaligned pointers untyped, object-macro sizeof is size_t (#1417)

Native Windows 10 x64, stage2 of main 5848ae9: the bzip2 and libcurl
release UATs stop parsing windows.h — `type PMETARECORD = *mut __unaligned
struct tagMETARECORD` (5 "expected declaration", 1 "expected ')'" in
PlayMetaFileRecord). Behind that are the classes #1417 lists from #1396's
branch (65 field accesses on opaque records and 6 `null` contexts, 2
undefined `with_memset`, WINAPI_PARTITION_DESKTOP's binding, the ENUMTYPE
parameter) and one more that surfaced once they cleared,
TOKEN_INTEGRITY_LEVEL_MAX_SIZE's "bitwise operands with mixed signedness".
With this commit both UATs pass `with check` there, and bzip2 runs ("bzip2
UAT passed"). libcurl's run then fails in codegen on an unrelated Windows
defect, #1495 (a c_import auto-method over an opaque struct asks for its
layout; four header lines reproduce it on main).

1. `__unaligned` (ClangBridge.translate_type_recursive_mode). The record
   branch stripped `const `/`volatile ` from clang's spelling but not
   MSVC's `__unaligned `, and the pointer branch passed the qualified
   pointee through, so the name was `__unaligned struct tagMETARECORD`.
   With has no unaligned pointer: a `*T` read assumes T's alignment, which
   the C contract says the address may not have (D51: never infer what can
   create unsafety). A pointer to an `__unaligned` pointee now imports as
   `*mut c_void` / `*const c_void`, the address unchanged; reading T
   through it names T with a cast. The record's own name drops the
   qualifier.

2. Inline bodies over opaque records (CImport.ci_translate_function). A
   static/forced-inline body was translated without consulting the demoted
   set, so winnt.h's TpInitializeCallbackEnviron wrote the fields of
   `_TP_CALLBACK_ENVIRON_V3` (it holds a bitfield record, so it imports
   opaque, §16.9). ci_cursor_needs_demoted_layout walks the definition: a
   field access through a demoted record (`p->f`, `(*p).f`, `s.f`), a value
   of its type (a local, a deref, a by-value parameter or result), or a type
   operand of sizeof/alignof/offsetof. Such a function is omitted with
   "inline body needs the layout of 'X', which c_import imports opaque
   (§16.9)", category inexpressible — an inline function has no symbol an
   extern could bind. ci_try_translate_fn_body's definition search is split
   out (ci_fn_definition_cursor) so the check and the translation share one
   search; migrate's call is unchanged.

3. `with_memset` undeclared. The body lowering maps memcpy/memmove/memset/
   memcmp (and the `__builtin___*_chk` forms) to with_mem*
   (build_libc_call_value_expr), which the migrate preamble declares
   (CiMigrate) but the c_import preamble did not: it declared only
   with_alloc/with_alloc_zeroed/with_realloc/with_free. It declares the
   four with the rest. D30 retires this seam as a whole; this completes the
   existing preamble, it adds no new seam.

4. Predicate macros (ci_translate_macros, object path). A system header's
   macro is never probed (probes run for non-system macros only), so
   `#define WINAPI_PARTITION_DESKTOP (WINAPI_FAMILY ==
   WINAPI_FAMILY_DESKTOP_APP)` reached the semantic path: clang's type
   `c_int`, the translation a With `bool`, "type mismatch in binding". C's
   comparison and logical operators yield an int 0 or 1, so the value
   follows the type: ci_fold_c_predicate folds it over the constants
   emitted so far (WINAPI_FAMILY itself was emitted `let WINAPI_FAMILY:
   c_int = 100` by the semantic path, which does not extend known_values;
   literal_values records such lets for this fold only), else it is
   `((expr) as c_int)`. Every winapifamily.h partition folds (DESKTOP 1,
   PC_APP 1, PHONE_APP 0, GAMES 1, SYSTEM 0). An `(if ...)` spelling was
   avoided: a control-flow global initializer fails codegen, #1483.

5. Macro parameters (ci_translate_macros, function-like path). Parameters
   were spelled with ci_escape_reserved only, so winnt.h's
   `DEFINE_ENUM_FLAG_OPERATORS(ENUMTYPE)` emitted `fn ...(ENUMTYPE: i32)`,
   an uppercase-initial binding that parses as a variant pattern (§9.7).
   They take the wrappers' rule (ci_escape_param_name, `p_ENUMTYPE`), and
   when one is renamed the body names the binding
   (ci_macro_substitute_arguments). All-lowercase macros emit as before.

6. `~` and sizeof in an imported object macro (ci_parse_unary_expr).
   winnt.h's `((((DWORD)(sizeof(TOKEN_MANDATORY_LABEL)) + sizeof(PVOID) -
   1) & ~(sizeof(PVOID)-1)) + SECURITY_MAX_SID_SIZE)` rendered sizeof as
   With's signed i64 next to an unsigned cast, and `~x` as `(0 - x - 1)`,
   which underflows for an unsigned x. In an import `~` is With's `~`
   (type-preserving, like C's), and an object macro's sizeof crosses as the
   size_t it is, `(sizeof[T]() as usize)`; a function-like macro's body
   stays generic over T. Migration keeps both old spellings: the pcre2 and
   zlib outputs are pinned.

Migrate: every change to shared code is either gated off in migrate mode
(6), c_import-only (1's MSVC qualifier, 3), or reachable only by output
that never compiled (4, 5). pcre2 and zlib re-migrate byte-identically:
the base stage2 and this stage2 each drove `with build :pcre2-migrate` and
`:zlib-migrate` (the migration re-runs in-process under the driver's
comptime evaluator), and the 36 and 23 generated files match. (Both runs
then fail the same later step, and both differ from the committed
lib/std/re in the same two files; neither is this change.)

macOS: the five release UATs (bzip2, zlib, sqlite3, libcurl, raylib) check
with fresh translations, and bzip2, zlib, sqlite3 and libcurl run.

Fixtures: behav_1417_windows_h_shapes (a kept pointer-only inline beside
omitted ones, memset/memcpy bodies, uppercase macro parameters),
err_1417_inline_body_over_opaque_record (the omission's message),
c_import_predicate_macro_is_c_int and c_import_macro_sizeof_is_size_t
(check-only, over an extern variable, since a non-system macro over
constants is folded by clang instead), behav_1417_unaligned_pointer
(only-on: windows; clang takes `__unaligned` only in MS mode). All pass on
darwin and on native Windows.

Also filed: #1483 (control-flow global initializers), #1485 (stage1: a
user module's <string.h> memchr wrapper hijacks std.re's memchr), #1495.
@ehartford
ehartford force-pushed the fix-1417-windows-h-sema branch from 33e6ba5 to 6d50b9d 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 ce7508c into main Sep 23, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Confirming battery on the rebased stack (head 6d50b9d, on main after #1516): build, :fixpoint, :drop-audit, :move-audit, :test, :test-green, :last-green all ok.

@ehartford
ehartford deleted the fix-1417-windows-h-sema 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

1 participant