nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.57k stars 1.47k forks source link

--gc:arc is freely using incomplete structs #18355

Open kasperk81 opened 3 years ago

kasperk81 commented 3 years ago

Type FILE is imported as incomplete structs in two places: https://github.com/nim-lang/Nim/blob/9d3a813fb96950631c69858e8e26b6503abd02fe/lib/system/io.nim#L18-L20

https://github.com/nim-lang/Nim/blob/fb86271556b4ea4485195febaa02b972149ca088/lib/system/ansi_c.nim#L117-L119

However the compiler/cgen does not make distinction when choosing the ctypes to emit. This assumption that FILE "can be" complete struct (regardless of what C standard says and how nim itself imports it; abstract / incomplete struct) naturally does NOT hold in non-GNU environments which abide by the meaning of "incomplete struct".

Consequently, a test fails on Alpine Linux, musl-libc:

FAIL: tests/arc/trepr.nim C
Test "tests/arc/trepr.nim" in category "arc"
Failure: reNimcCrash
$ bin/nim c --gc:arc tests/arc/trepr.nim
Hint: used config file '/builds/alpine/aports/community/nim/src/nim-1.4.8/config/nim.cfg' [Conf]
Hint: used config file '/builds/alpine/aports/community/nim/src/nim-1.4.8/config/config.nims' [Conf]
Hint: used config file '/builds/alpine/aports/community/nim/src/nim-1.4.8/tests/config.nims' [Conf]
Hint: used config file '/builds/alpine/aports/community/nim/src/nim-1.4.8/tests/arc/nim.cfg' [Conf]
..........
(a: true, n: doAssert)
Table[system.string, trepr.MyType](data: @[], counter: 0)
nil
..........
../../lib/system/repr_v2.nim(96, 7) Hint: 'firstElement' is declared but not used [XDeclaredButNotUsed]
trepr.nim(43, 8) Warning: imported and not used: 'os' [UnusedImport]
CC: stdlib_assertions.nim
CC: stdlib_dollars.nim
CC: stdlib_repr_v2.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: stdlib_times.nim
CC: stdlib_os.nim
CC: trepr.nim
/home/buildozer/.cache/nim/trepr_d/stdlib_repr_v2.nim.c:237:92: error: parameter 2 ('x') has incomplete type
  237 | N_LIB_PRIVATE N_NIMCALL(void, reprObject__oUb9a0hqZE8qgrd3cWM89cvg)(NimStringV2* res, FILE x) {
      |                                                                                       ~~~~~^
/home/buildozer/.cache/nim/trepr_d/stdlib_repr_v2.nim.c: In function 'repr__jIXMkVs9aUgCuk4CQ63oMmw':
/home/buildozer/.cache/nim/trepr_d/stdlib_repr_v2.nim.c:270:51: error: invalid use of incomplete typedef 'FILE'
  270 |  reprObject__oUb9a0hqZE8qgrd3cWM89cvg((&result), (*x));
      |                                                  ~^~~
/home/buildozer/.cache/nim/trepr_d/stdlib_repr_v2.nim.c:270:50: error: type of formal parameter 2 is incomplete
  270 |  reprObject__oUb9a0hqZE8qgrd3cWM89cvg((&result), (*x));
      |                                                  ^~~~
Error: execution of an external compiler program 'gcc -c  -w -fmax-errors=3   -I/builds/alpine/aports/community/nim/src/nim-1.4.8/lib -I/builds/alpine/aports/community/nim/src/nim-1.4.8/tests/arc -o /home/buildozer/.cache/nim/trepr_d/stdlib_repr_v2.nim.c.o /home/buildozer/.cache/nim/trepr_d/stdlib_repr_v2.nim.c' failed with exit code: 1
kasperk81 commented 3 years ago

@timotheecour do you have any ideas where the fix of this one should go?

richfelker commented 3 years ago

Note: I looked at this a bit yesterday but, not being familiar with Nim, I was unable to figure out whether the problem was coming just from the tests, or from part of the Nim standard library that could also affect applications.