nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
277 stars 39 forks source link

cgen generates the same struct twice when importing a distinct object #1417

Closed starsiderfox closed 2 months ago

starsiderfox commented 2 months ago

Specification

When using importc and nodecl for an object that is just a distinct of another object, the compiler erroneously writes the same struct twice. When I edit the .c file to remove the duplicate, it works properly.

Example

Compile with --passL:-latomic

type int128* = object
        hi*{.align:16.}, lo*: uint64
type cint128 {.importc: "__int128",nodecl.} = distinct int128

let x = int128(hi: 123'u64, lo: 321'u64)
let y = cast[cint128](x)
echo cast[int128](y)

Actual Output

/home/user/.cache/nimskull/dupli_d/@mdupli.nim.c:42:8: error: redefinition of 'struct _G6int128_M5dupli'
   42 | struct _G6int128_M5dupli {
      |        ^~~~~~~~~~~~~~~~~
/home/user/.cache/nimskull/dupli_d/@mdupli.nim.c:38:8: note: originally defined here
   38 | struct _G6int128_M5dupli {
      |        ^~~~~~~~~~~~~~~~~

execution of an external program 'gcc -c  -w -fmax-errors=3   -I/home/user/nimskull/lib -I/tmp -o /home/user/.cache/nimskull/dupli_d/@mdupli.nim.c.o /home/user/.cache/nimskull/dupli_d/@mdupli.nim.c' failed with exit code '1'

Expected Output

(hi: 123, lo: 321)
zerbina commented 2 months ago

A regression introduced by #1382. The way imported types are handled here: https://github.com/nim-works/nimskull/blob/a1decd81b2f34e4c00b92c0182fac3f443244abf/compiler/mir/mirtypes.nim#L1088-L1092

leads to duplicate type mappings being introduced for distinct or alias types.