Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Invalid C codegen `refc` with generic types containing gc memory

Open tersec opened this issue 8 months ago • 1 comments

Nim Version

Nim Compiler Version 2.0.14 [Linux: amd64]
Compiled at 2025-04-03
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: bf4de6a394e040d9810cba8c69fb2829ff04dcc6
active boot switches: -d:release
Nim Compiler Version 2.2.3 [Linux: amd64]
Compiled at 2025-04-03
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: a2a6565e23229b7415d28ef2859c6c593db49989
active boot switches: -d:release
Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-04-03
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: 4352fa2ef0cbba953d9a90b90873e8dd0364b72e
active boot switches: -d:release
gcc (Debian 14.2.0-19) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Description

The main module:

import "."/[r, v]
type M = ref object
type E = distinct int
method j(_: M) {.base.} = u()
discard p[E]()

r.nim:

import "."/v
proc u*() =
  iterator c(): int {.closure.} = raiseAssert ""
  discard A[int]()
  let _ = c

v.nim:

type
  B = ref object of W
  W = object of RootObj
    y: proc(_: int)
    n: B
    k: iterator(): int {.closure.}
  S*[T] = ref object of B
    r: T
  A*[T] = ref object of S[T]

proc p*[T](): S[T] = S[T]()
iterator t(): int {.closure.} = discard

Current Output

Hint: used config file '/tmp/nimdevel/config/nim.cfg' [Conf]
Hint: used config file '/tmp/nimdevel/config/config.nims' [Conf]
..........................................................................
/tmp/v.nim(12, 10) Hint: 't' is declared but not used [XDeclaredButNotUsed]
CC: nimdevel/lib/system/exceptions.nim
CC: nimdevel/lib/std/private/digitsutils.nim
CC: nimdevel/lib/std/assertions.nim
CC: nimdevel/lib/system/dollars.nim
CC: nimdevel/lib/std/typedthreads.nim
CC: nimdevel/lib/pure/collections/sharedlist.nim
CC: nimdevel/lib/system.nim
CC: v.nim
CC: r.nim
CC: f.nim
/tmp/tmp.BBAWLItgpC/@mf.nim.c: In function ‘Marker_tyRef__NBQRyu0xvXLho06pe9bfbog’:
/tmp/tmp.BBAWLItgpC/@mf.nim.c:115:25: error: invalid use of incomplete typedef ‘tyObject_ScolonObjectType___PsOs7wPZadaq8vGxWAk0CA’
  115 | nimGCvisit(((void*) (*a).Sup.Sup.y.ClE_0), op);
      |                         ^
/tmp/tmp.BBAWLItgpC/@mf.nim.c:116:25: error: invalid use of incomplete typedef ‘tyObject_ScolonObjectType___PsOs7wPZadaq8vGxWAk0CA’
  116 | nimGCvisit(((void*) (*a).Sup.Sup.n), op);
      |                         ^
/tmp/tmp.BBAWLItgpC/@mf.nim.c:117:25: error: invalid use of incomplete typedef ‘tyObject_ScolonObjectType___PsOs7wPZadaq8vGxWAk0CA’
  117 | nimGCvisit(((void*) (*a).Sup.Sup.k.ClE_0), op);
      |                         ^
Error: execution of an external compiler program 'gcc -c  -w -fmax-errors=3 -pthread -fno-omit-frame-pointer   -I/tmp/nimdevel/lib -I/tmp -o /tmp/tmp.BBAWLItgpC/@mf.nim.c.o /tmp/tmp.BBAWLItgpC/@mf.nim.c' failed with exit code: 1

Expected Output


Known Workarounds

No response

Additional Information

No response

tersec avatar Apr 04 '25 05:04 tersec

Reduced

# test.nim
type
  S*[T] = ref object of RootObj
    k: string
  A*[T] = ref object of S[T]

proc p*[T](): S[T] = S[T]()

discard A[int]()
# main.nim
import test

u()

type E = distinct int
discard p[E]()

ringabout avatar May 27 '25 15:05 ringabout

Reduced

test.nim

type S*[T] = ref object of RootObj k: string A*[T] = ref object of S[T]

proc p*T: S[T] = ST

discard Aint

main.nim

import test

u()

type E = distinct int discard pE

main.nim(3, 1) Error: undeclared identifier: 'u'

?

tersec avatar Jul 19 '25 18:07 tersec

Fixing the 2-file test case slightly by readding u: test.nim:

type
  S*[T] = ref object of RootObj
    k: string
  A*[T] = ref object of S[T]

proc p*[T](): S[T] = S[T]()
proc u*() = discard A[int]()
discard A[int]()

main.nim:

import test

u()

type E = distinct int
discard p[E]()

Which still exists as of

Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-07-19
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: 08d51e5c881e7d2d0c192832c3a235cb6a3be425
active boot switches: -d:release

tersec avatar Jul 19 '25 18:07 tersec