Files
Nim/tests
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
..
2024-06-06 00:52:01 +02:00
2021-08-21 08:22:00 +02:00
2022-07-06 13:06:41 +02:00
2025-11-12 20:33:26 +08:00
2025-02-06 23:19:53 +01:00
2023-07-22 21:11:08 +02:00
2024-10-06 06:33:44 +02:00
2025-05-16 09:44:13 +02:00
2025-12-18 18:54:03 +01:00
2025-12-05 15:27:38 +01:00
2024-11-23 13:39:26 +01:00
2025-05-11 06:40:46 +02:00
2025-03-29 13:28:28 +01:00
2022-12-22 08:32:12 +01:00
2013-03-16 23:53:07 +01:00
2020-12-28 14:13:21 +01:00

This directory contains the test cases.

Each test must have a filename of the form: t*.nim

Note: Testament is only aware of tests under a directory (eg tests/foo/) and will ignore top-level tests like tests/tbar.nim.

Specs

Each test can contain a spec in a discard """ ... """ block.

Check out the parseSpec procedure in the specs module for a full and reliable reference

action

Specifies what action this test should take.

Default: run

Options:

  • compile - compiles the module and fails the test if compilations fails.
  • run - compiles and runs the module, fails the test if compilation or execution of test code fails.
  • reject - compiles the module and fails the test if compilation succeeds.

There are certain spec keys that imply run, including output and outputsub.

Categories

Each folder under this directory represents a test category, which can be tested by running koch tests pcat <category> (or cat to avoid parallel testing, which is slower).

The folder dll contains simple DLL tests.

The folder realtimeGC contains a test for validating that the realtime GC can run properly without linking against the nimrtl.dll/so.