Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler error when proc refers to type with unused generic params. #19531

Open
etan-status opened this issue Feb 15, 2022 · 0 comments
Open

Comments

@etan-status
Copy link
Contributor

When a type is defined to be generic on an object type, but is not actually using that type in its definition, as in Foo[T] = object (T is not used in the body of Foo[T]), there are compiler errors when trying to access the generic type in a proc. It seems that the only a single implementation is generated for the different T in that case, leading to C compilation errors when multiple different implementations are requested in a single compilation unit.

Example

type
  Foo[T] = object

  Bar[F: Foo] = object
    c: proc(t: F.T)

proc cb[F](v: Bar[F]) =
  v.c(default(F.T))

type
  X = object
    x: uint32
  Y = object
    x: uint32
proc cbX(v: X) = discard
proc cbY(v: Y) = discard

let
  x = Bar[Foo[X]](c: cbX)
  y = Bar[Foo[Y]](c: cbY)
x.cb()
y.cb()

Current Output

error: passing 'tyObject_Y__U7uoXYJsXAmQ0DnkqRcTeA' (aka 'struct tyObject_Y__U7uoXYJsXAmQ0DnkqRcTeA') to parameter of incompatible type 'tyObject_X__nhXs9ay3Yu0ODg9bF6LB8cRA' (aka 'struct tyObject_X__nhXs9ay3Yu0ODg9bF6LB8cRA')
        nimZeroMem((void*)(&T1_), sizeof(tyObject_Y__U7uoXYJsXAmQ0DnkqRcTeA));  v.c.ClE_0? v.c.ClP_0(T1_, v.c.ClE_0):((TM__cz1FP2xNs9aBzwtJ0lyd4Jg_8)(v.c.ClP_0))(T1_);
                                                                                                     ^~~
1 error generated.

Expected Output

0
0

Possible Solution

  • As a workaround, use T in the definition of Foo, e.g., Foo[T] = (T,) instead of Foo[T] = object.
  • Second workaround is to only ever use a single instantiation in a compilation unit, e.g., put x and y into separate programs. The problem only occurs when there are multiple instantiations of different types.

Additional Information

% nim -v
Nim Compiler Version 1.2.16 [MacOSX: amd64]
Compiled at 2022-02-14
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: c6a9f27b3e36bae9aacec1bd6c37893fb98fd33f
active boot switches: -d:release
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants