Files
Nim/tests/ccgbugs
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
..
2018-08-12 20:43:30 +02:00
2018-03-23 13:28:22 +01:00
2021-11-12 11:19:24 +01:00
2021-02-08 09:46:07 +01:00
2018-12-11 21:23:22 +01:00
2018-12-11 21:23:22 +01:00
2018-09-28 09:59:45 +02:00
2019-05-06 09:34:03 +02:00
2021-01-11 18:02:32 +01:00
2021-09-14 19:40:42 +02:00
2020-11-18 07:56:31 +00:00
2021-03-02 19:41:55 +08:00
2019-11-05 11:05:46 +01:00
2015-04-11 10:01:13 +02:00
2016-08-09 14:16:26 +02:00
2024-12-13 19:30:53 +08:00
2016-06-02 17:23:39 +02:00
2022-09-23 13:05:05 +02:00
2021-09-14 19:39:55 +02:00
2016-09-07 08:29:45 +02:00
2018-12-11 21:23:22 +01:00
2022-09-23 13:05:05 +02:00
2021-01-22 13:14:28 +01:00
2021-03-11 14:03:25 +01:00
2016-01-30 15:45:45 +01:00
2020-11-26 20:00:31 +01:00
2022-09-23 13:05:05 +02:00
2019-06-26 23:15:58 +02:00
2017-02-09 01:16:43 +01:00
2015-03-24 23:07:19 +01:00
2017-11-19 00:37:36 +01:00
2018-03-16 15:32:01 +01:00
2024-11-21 23:50:49 +08:00
2016-03-29 21:44:42 +02:00
2019-05-05 16:08:16 +02:00
2016-07-19 15:40:21 +02:00
2016-04-19 12:10:48 +02:00
2015-10-06 15:51:00 +02:00
2017-02-26 17:41:00 +01:00
2016-08-24 11:48:21 +02:00
2019-05-10 11:43:58 +02:00
2019-05-06 09:34:03 +02:00
2019-05-06 09:34:03 +02:00
2019-05-06 09:34:03 +02:00
2019-05-06 09:34:03 +02:00
2019-05-06 09:34:03 +02:00