Repository navigation
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 intoSep 23, 2026
Merged
Conversation
This was referenced Sep 23, 2026
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: |
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
force-pushed
the
fix-1417-windows-h-sema
branch
from
September 23, 2026 19:40
33e6ba5 to
6d50b9d
Compare
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ')'" inPlayMetaFileRecord). Behind that are the classes #1417 lists from #1396's
branch (65 field accesses on opaque records and 6
nullcontexts, 2undefined
with_memset, WINAPI_PARTITION_DESKTOP's binding, the ENUMTYPEparameter) 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 checkthere, and bzip2 runs ("bzip2UAT 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).
__unaligned(ClangBridge.translate_type_recursive_mode). The recordbranch stripped
const/volatilefrom clang's spelling but notMSVC's
__unaligned, and the pointer branch passed the qualifiedpointee through, so the name was
__unaligned struct tagMETARECORD.With has no unaligned pointer: a
*Tread assumes T's alignment, whichthe C contract says the address may not have (D51: never infer what can
create unsafety). A pointer to an
__unalignedpointee now imports as*mut c_void/*const c_void, the address unchanged; reading Tthrough it names T with a cast. The record's own name drops the
qualifier.
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 importsopaque, §16.9). ci_cursor_needs_demoted_layout walks the definition: a
field access through a demoted record (
p->f,(*p).f,s.f), a valueof 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.
with_memsetundeclared. The body lowering maps memcpy/memmove/memset/memcmp (and the
__builtin___*_chkforms) 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.
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 typec_int, the translation a Withbool, "type mismatch in binding". C'scomparison 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 = 100by 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 wasavoided: 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.
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)emittedfn ...(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), andwhen one is renamed the body names the binding
(ci_macro_substitute_arguments). All-lowercase macros emit as before.
~and sizeof in an imported object macro (ci_parse_unary_expr).winnt.h's `((((DWORD)(sizeof(TOKEN_MANDATORY_LABEL)) + sizeof(PVOID) -
(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 importis 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 bodystays 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-migrateand:zlib-migrate(the migration re-runs in-process under the driver'scomptime 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
__unalignedonly in MS mode). All pass ondarwin 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-allswept over test/, examples/, lib/std (3188 files) with base stage2 (main f1e3bd2) vs the stack's stage2.