Repository navigation
c_import: records with no tag and no typedef name are named by their first user (#1396); pointer type args in sizeof; nested records; unspellable layouts opaque - #1419
Conversation
b34fa12 to
4cb0617
Compare
4cb0617 to
ec5a26a
Compare
|
Batch battery X2 (seed-driven, NEW seed, with :drop-audit/:move-audit) on main 977e667 + #1404 → #1418 → #1419 → #1425 (head ee4e2e4, including the #1404/#1418 join-arm merge resolution): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. #1424's four repros run clean on this build. Branches hold exactly the tested chain; merge in order #1404, #1418, #1419, #1425 — then batch Y (#1405 … #1360), then #1428. |
ec5a26a to
187a3ca
Compare
|
Battery (seed-driven, new seed, with :drop-audit/:move-audit) on main 05b674e (incl. batch Y) + #1418 → #1419 → #1425 → #1428 (head eea9017): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds — including #1422's da_option_filter_observes_payload, so this clears main's #1424 red), :test-green ok, :last-green ok. Merges cleanly into main 1c76062. Now GitHub stack #1437 (gh stack link); merge the whole stack: |
…e argument in expression position parses as a type (§16.12) `*` followed by `mut` or `const` never dereferences anything, so inside `name[...]` it begins a type, like the `fn`/`extern` function-pointer case already beside it. c_import emits `sizeof[*mut c_void]()` for C's `sizeof(PVOID)`; winnt.h's TOKEN_INTEGRITY_LEVEL_MAX_SIZE failed with "expected expression" at `mut` once #1396's unnamed records parsed. Test: behav_sizeof_pointer_type_arg.w.
…he first declaration that uses it; one nothing uses is not emitted (#1396) Root cause: ci_translate_struct took with_cimport_decl_name, which for a file-scope record with no tag and no typedef name is libclang's cursor spelling `struct (unnamed at file:line:col)`, and emitted it as a type name; the bindings failed to parse. Every use of such a record translated as `c_void` (ClangBridge translate_type_recursive_mode's Record branch), so `struct { int b; } g;` also imported as `extern var g: c_void`, an array of it collapsed to `c_void`, and a function returning one was omitted. ClangBridge now names each file-scope record with no name after the first declaration, in translation-unit order, whose type reaches it: `g_anon` (variable, also through arrays/pointers), `f_return_anon` / `f_p_anon` (`f_argN_anon` unnamed), `T_anon` (typedef through a pointer/array); a name another declaration already spells takes the first free `_2` suffix. The cursor spelling cache and type translation both answer with it, so the record, `extern var g: g_anon`, `[4]g3_anon` and `fn mk() -> mk_return_anon` agree. Names depend only on declaration order and spellings: deterministic. A record nothing uses — winnt.h's C_ASSERT(TYPE_ALIGNMENT(T)) builds one inside an expression — has no spelling in C or With and is not emitted (with_cimport_decl_is_unused_anon_record), as the issue prescribes. Two neighbours winnt.h/wtypes.h reach next, same root (records libclang lists only under another record): - A member record with no name held through an array or pointer (`struct { ... } ScopeRecord[1];`, SCOPE_TABLE_AMD64) translated as `c_void`, i.e. an opaque by-value field. The bridge names it by CImport's member rule, `Parent_field`, CImport emits it, and an array of a demoted one demotes its holder. The flexible-array accessor rename replaced every occurrence of the field's spelling in the last field, including the new type name; it now renames the field only. - A tag declared inside a record (`union __MIDL_IWinTypes_0009 {...} u;`, and `enum { A, B } k;` constants) is file scope in C but was never collected: the field named an unknown type. hoist_nested_tags lists each just before the top-level record holding it. sqlite3.h's sqlite3_index_constraint/_orderby/_constraint_usage import with fields now instead of opaque. Lookups stay linear in the header (#1386): a record finds its name through a hash on its Decl, and a candidate name is checked against a hash set of the declarations' spellings built once (emitted_name_slot), never a scan. Shapes (darwin, stage1): `struct {..} g;`, `static struct {..} g;`, `extern struct {..} g[4];`, `struct {..} a, b;` (one type), union, anonymous enum (constants), anonymous-in-anonymous members, function result and prototype parameter, pointer typedef, pointer var, name collision, const initializer, and the C_ASSERT expression record (not emitted). Tests: behav_c_import_anon_file_scope_record.w (layouts, literals, const initializer) and _uses.w (check-only: every declaration's type identity).
…s with bitfields, bitfields in member records, fields under their natural alignment (#1396) Three holes in the demotion pre-scan, each an emitted record that either failed to compile or compiled with the wrong layout: - ci_collect_demoted_types skipped every tag with a leading underscore, though ci_translate_struct emits them: winbase.h's `_DCB` bitfields came out as whole DWORD fields with `@[align(1)]` ("alignment is less than natural"), and darwin's `union wait` as four 4-byte fields. - ci_record_decl_directly_demoted_cursor (member records) never looked for bitfields (§16.9: a record with bitfields is opaque). - A field below its natural alignment (`#pragma pack(2)`, wingdi.h's BITMAPFILEHEADER) was emitted `@[align(2)]`; §16.4 rule 2 forbids an alignment under the natural one, and `@[packed]` would drop the record's own alignment to 1. It imports opaque, usable by pointer, like a bitfield layout. An exact spelling needs a packed(N) layout the spec does not have. Windows 10: the 146 "alignment is less than natural" errors in the windows.h closure are gone. Darwin headers change only where they were wrong: `union wait` is opaque; forward-declared `_` records gain the "demoted to opaque" location comment. Test: behav_c_import_nested_records.w (also covers the array/pointer member records and nested tags of the previous commit).
187a3ca to
c0fcc75
Compare
|
Stack #1466 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 1c76062 + #1418 → #1419 → #1425 → #1428 → #1449 → #1377 → #1459 → #1460 → #1461 → #1462 (head cf36211, including the #1449/#1462 Codegen resolution and the fixed #1460): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Each branch holds exactly its tested commits on the layer below. Merge the whole stack: |
Fixes #1396. Rebased onto main after #1386 and #1399 merged; the branch holds three commits.
Root cause (#1396)
src/CImport.wci_translate_structemittedwith_cimport_decl_name(session, idx)as the type name. For a file-scope struct/union with no tag and no typedef name, that is libclang's cursor spelling,struct (unnamed at file:line:col), so the bindings failed to parse. Every use of such a record went throughClangBridge.wtranslate_type_recursive_mode's Record branch, which returnsc_voidfor any anonymous record. Sostruct { int b; } g;also imported asextern var g: c_void, an array of the record collapsed toc_void, and a function returning one was dropped into the manifest.Fix
The spec does not say how to name an unnamed record. §16.1 says c_import "makes all declarations available as With symbols", and §16.3b says an extern variable's type "must be concrete", which
c_voidis not. The issue itself says a record nothing can refer to "is not emitted". Following that:ClangBridge.name_file_scope_anon_recordsnames each file-scope record with no name after the first declaration, in translation-unit order, whose type reaches it:g_anonfor a variable, also through arrays and pointersf_return_anonfor a function's result,f_p_anonfor parameterp,f_argN_anonfor an unnamed parameterT_anonfor a typedef that reaches it through a pointer or arrayIf another declaration already uses the name, it takes the first free
_2,_3, and so on. The name depends only on declaration order and spellings, never on addresses or hashes, so it is fixpoint-deterministic. The cursor spelling cache and type translation both return this name.If nothing uses the record (winnt.h's
C_ASSERT(TYPE_ALIGNMENT(T))builds one inside an array-size expression), neither C nor With can spell it, so it is not emitted (with_cimport_decl_is_unused_anon_record).Lookups stay linear in the header, per c_import is linear in a header's declarations: emitted names are a hash set, decl locations an index #1386. A record finds its name through a hash on its Decl. A candidate name is checked against a hash set of the declarations' spellings, built once with c_import is linear in a header's declarations: emitted names are a hash set, decl locations an index #1386's
emitted_name_slot. The member-record walk collects field cursors only, with no per-field strings. Nothing scans all declarations per name.Shapes matrix (darwin; test/behavior/behav_c_import_anon_file_scope_record{,_uses}.w)
typedef struct { int a; } X;type X(unchanged)struct { int b; } g;type g_anon,extern var g: g_anonstatic struct { int c; } g2;type g2_anon,extern var g2: g2_anonextern struct { int d; long long e; } g3[4];extern var g3: [4]g3_anon(wasc_void)struct { int f; } a, b;a_anon, used by bothunion { ... } gu;type gu_anon = union {...}enum { EA, EB } ge;extern var ge: i32(unchanged)struct { struct { int x; } inner; union { ... }; int w; } gn;gn_anon_inner,gn_anon_anon_1,gn_anonstruct { int r; short s; } mk(void);fn mk() -> mk_return_anon(was omitted)void take(struct { int q; } *p);fn take(p: *mut take_p_anon)(was*mut c_void)typedef struct { int pa; } *PX;type PX_anon,type PX = *mut PX_anonstruct { int s; } *gp;extern var gp: *mut gp_anontypedef int gz_anon;)gz_anon_2static const struct { int lo, hi; } k = {3, 4};let k: k_anon = k_anon { lo: 3, hi: 4 }Blockers Windows hit next
sizeof[*mut c_void]()failed with "expected expression" atmut(§16.12 says sizeof takes any type). Insidename[...], a*followed bymut/constnow parses as a type, like the existingfn/externcase. winnt.h'sTOKEN_INTEGRITY_LEVEL_MAX_SIZEhit this.struct { ... } ScopeRecord[1];in SCOPE_TABLE_AMD64. It came out as a by-valuec_voidfield. Now it is namedParent_fieldby CImport's member rule and emitted, and an array of a demoted record demotes the record that holds it. The flexible-array accessor rename used to replace every occurrence of the field's spelling, including inside the new type name. It now renames only the field.union __MIDL_IWinTypes_0009 {...} u;, and the constants ofenum { A, B } k;. In C these are file scope, but they were never collected, so fields named unknown types.hoist_nested_tagsnow lists each one just before the record that holds it. With this, sqlite3.h'ssqlite3_index_constraint,_orderbyand_constraint_usageimport with their fields instead of opaque.ci_collect_demoted_typesskipped every tag that starts with_. Windows'_DCBbitfields came out as whole DWORD fields plus@[align(1)], and darwin'sunion waitas four 4-byte fields.#pragma pack(N)fields below their natural alignment were emitted@[align(N)]. §16.4 rule 2 forbids that: "Alignment must be at least the natural alignment of the type." These records now import opaque. This needs no ruling. D51's asymmetry rule (§1 of the ruling) says "A convention may be inferred silently when being wrong can only remove capability or reject a valid program." Opaque only removes capability, while an approximate layout could let safe code read or write at wrong offsets. The exact layout needs a packed(N) form the spec does not have yet; filed as c_import: exact packed(N) record layouts #1421.Verification (iterate tier only; the seed battery has not run)
Base for every A/B below: main's stage1 at f008ef3. Candidate: this branch's stage1/stage2 at 4cb0617.
with migrateoutput for pcre2 (34 files) and zlib (19 files) is byte-identical to main's stage1.HOMEcache per run):union waitis now opaque (stdlib.h).// ...: demoted to opaquecomment (stdio.h, curses.h, sys/stat.h, dirent.h).main, and 4 need stage2.behav_c_import_scales_linearly, run alone: passes with stage1, and with stage2 in 4 s.init+get+run): bzip2, sqlite3 and libcurl pass. zlib fails the same way on the old base (shadowing is not allowed for 'HAVE_UNISTD_H').C:\Users\Eric\triage\fix1396):(unnamedspelling is gone from the bzip2 and libcurl bindings.with checkstill fails, in Sema, on classes filed as c_import (Windows): after #1396 the bzip2/libcurl windows.h bindings still fail to type-check — inline bodies over opaque records, with_memset, bool macro typed c_int, uppercase macro params #1417:with_memsetis undeclaredc_intDraft until the seed battery is green.