Skip to content

c_import: a file-scope record with no tag and no typedef renders as 'type struct (unnamed at ...)' and the import fails to parse (winnt.h C_ASSERT(TYPE_ALIGNMENT(...))) #1396

Closed

Description

@ehartford

Symptom

With #1387 fixed (branch fix-1387-cimport-var-extent), with check of the bzip2 release UAT fixture on native Windows 10 x64 finishes c_import of bzlib.h (which includes windows.h) in 1.6 s instead of dying at 29 GB — and then fails to parse its own generated bindings:

error: expected type body
 --> <c_import bzlib.h>:765:13
765 | type struct (unnamed at C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um\winnt.h:2602:1) { x: c_char = 0, test: _LARGE_INTEGER }
  |             ^
error: expected declaration (fn, type, enum, let, use, extern)
 --> <c_import bzlib.h>:766:22
766 | impl Copy for struct (unnamed at C:\...\winnt.h:2602:1)

winnt.h:2602 is C_ASSERT(TYPE_ALIGNMENT(LARGE_INTEGER) == 8); where TYPE_ALIGNMENT(t) is FIELD_OFFSET(struct { char x; t test; }, test): the unnamed struct is declared inside an expression at file scope, so it is a top-level StructDecl with no tag and no typedef. The later expected expression / expected ']' errors at TOKEN_INTEGRITY_LEVEL_MAX_SIZE look like parser recovery from these.

Repro (darwin, installed v0.15.2.1 and the #1387 branch alike)

// src/an.h
typedef struct { int a; } X;     // fine: the typedef names it
struct { int b; } g;             // broken: no tag, no typedef
int f(void);
// src/an.w
use c_import("an.h")
fn main: print("ok")

with check src/an.w:

31 | type struct (unnamed at .../src/an.h:2:1) { b: c_int = 0 }
error: expected type body

Same with the winnt.h shape:

#define FIELD_OFFSET(type, field) ((long)(long long)&(((type *)0)->field))
#define TYPE_ALIGNMENT(t) FIELD_OFFSET(struct { char x; t test; }, test)
#define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]
C_ASSERT(TYPE_ALIGNMENT(long long) == 8);

Expected

ci_translate_struct (src/CImport.w) takes with_cimport_decl_name, which is libclang's cursor spelling struct (unnamed at file:line:col) for an anonymous record, and emits it as a type name. An anonymous file-scope record that no typedef names either gets a synthesized With name used consistently by every type that refers to it (g's type), or — when nothing can refer to it, as in the C_ASSERT expression — is not emitted. Never an unparseable declaration.

Blocks release-bzip2-uat / release-libcurl-uat on Windows after #1387.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions