Files
Nim/compiler
elijahr 780c9eeef0 fixes #25405; initialization for objects with opaque importc fields (#25406)
Objects containing `importc` fields without `completeStruct` fail to
compile when used as const/static. The C codegen generates "aggregate
initialization" which is invalid for opaque types.

Fixes #25405.

Nim code:

```nim
  type
    OpaqueInt {.importc: "_Atomic int", nodecl.} = object

    ContainsImportc = object
      normal: int
      opaque: OpaqueInt

  const c = default(ContainsImportc)
```

Resulting C code:

```c
// Invalid C - cannot aggregate-init opaque type
NIM_CONST ContainsImportc c = {((NI) 0), {}};
                                         ^^ error: illegal initializer type
```

## Solution

Fix in `ccgexprs.nim`:
1. Skip opaque importc fields when building aggregate initializers
2. Use "designated initializers" (`siNamedStruct`) when opaque fields
are present to avoid positional misalignment

```c
// Valid C:
//  - opaque field is omitted and implicitly zero-initialized by C
//  - other fields are explitly named and initialized
NIM_CONST ContainsImportc c = {.normal = ((NI) 0)};
```

This correctly handles the case where the opaque fields might be in any
order.

A field is considered "opaque importc" if:
- Has `sfImportc` flag
- Does NOT have `tfCompleteStruct` flag
- Either has `tfIncompleteStruct` OR is an object with no visible fields

The `containsOpaqueImportcField` proc recursively checks all object
fields, including nested objects and variant branches.

Anonymous unions (from variant objects) are handled by passing an empty
field name, which skips the `.fieldname = ` prefix since C anonymous
unions have no field name.

Note that initialization for structs without opaque importc fields
remains the same as before this changeset.

## Test Coverage

`tests/ccgbugs/timportc_field_init.nim` covers:
- Simple struct with one importc field
- Nested struct containing struct with importc field
- Variant object (case object) with importc field in a branch
- Array of structs with importc fields
- Tuple containing struct with importc field
- `completeStruct` importc types (still use aggregate init)
- Sandwich case (opaque field between two non-opaque fields)
- Fields with different C names (`{.importc: "c_name".}`, `{.exportc.}`)
- `{.packed.}` structs with opaque fields
- `{.union.}` types with opaque fields
- Deep nesting (3+ levels)
- Multiple opaque fields with renamed fields between them
2026-01-05 15:21:59 +01:00
..
2025-12-29 00:20:33 +01:00
2023-12-15 10:20:57 +01:00
2025-12-31 13:33:57 +01:00
2025-12-01 22:59:12 +01:00
2025-12-31 13:33:57 +01:00
2025-12-11 18:22:38 +01:00
2025-12-07 13:07:44 +01:00
2025-12-29 10:23:46 +01:00
2025-12-29 13:52:22 +01:00
2025-12-11 18:22:38 +01:00
2025-12-31 13:33:57 +01:00
2025-12-11 18:22:38 +01:00
2025-12-31 13:33:57 +01:00
2017-01-07 22:35:09 +01:00
2025-12-31 13:33:57 +01:00
2025-12-31 13:33:57 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-31 13:33:57 +01:00
2025-12-18 18:54:03 +01:00
2025-11-25 12:49:23 +01:00
2024-12-27 19:42:18 +01:00
2025-12-11 18:22:38 +01:00
2025-12-31 13:33:57 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-06 11:45:01 +01:00
2025-12-11 18:22:38 +01:00
2021-01-12 09:36:51 +01:00
2025-12-31 13:33:57 +01:00
2025-12-07 13:07:44 +01:00
2025-12-31 13:33:57 +01:00
2025-12-20 11:27:46 +01:00
2025-12-29 13:52:22 +01:00
2025-12-11 18:22:38 +01:00
2025-12-07 13:07:44 +01:00
2025-12-11 18:22:38 +01:00
2026-01-05 19:36:33 +08:00
2025-11-13 21:31:24 +01:00
2025-12-31 13:33:57 +01:00
2025-12-11 18:22:38 +01:00
2023-07-02 22:36:05 +02:00
2023-11-06 18:33:28 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-31 13:33:57 +01:00
2025-12-29 13:52:22 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-29 10:23:46 +01:00
2025-12-11 18:22:38 +01:00
2024-03-16 08:35:18 +08:00
2025-12-11 18:22:38 +01:00
2025-12-29 13:52:22 +01:00
2025-12-31 13:33:57 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2025-12-11 18:22:38 +01:00
2023-12-25 07:12:54 +01:00

Nim Compiler

  • This directory contains the Nim compiler written in Nim.
  • Note that this code has been translated from a bootstrapping version written in Pascal.
  • So the code is not a poster child of good Nim code.

See Internals of the Nim Compiler for more information.