Skip to content

Generic type alias for a union type causes gcc error #25346

@emiljanQ3

Description

@emiljanQ3

Nim Version

Nim Compiler Version 2.2.0 [Windows: amd64]

Description

I'm trying to do a simple union type in nim as such:

type SeqLike[T] = ptr UncheckedArray[T] | seq[T]

proc getFirst[T](list: SeqLike[T]): T =
    list[0]

proc main() =
    let test1 = @[1, 2, 3, 4, 5]
    let test2 = @['1', '2', '3', '4', '5']
    let test3 = cast[ptr UncheckedArray[int]]( addr(test1[0]) )
    let test4 = cast[ptr UncheckedArray[char]]( addr(test2[0]) )

    echo "test1"
    echo getFirst(test1)    
    echo "test2"
    echo getFirst(test2)    
    echo "test3"
    echo getFirst(test3)  
    echo "test4"
    echo getFirst(test4)

if isMainModule:
    main()

Am I doing something wrong here? Either way the nim compiler should catch it before the c-compiler does right? Do you know of any workarounds?

Current Output

CC: [redacted]/Nim/nim-2.2.0/lib/system.nim
CC: test.nim
C:\[redacted]\nimcache\test_d\@mtest.nim.c: In function 'main__test_u7':
C:\[redacted]\nimcache\test_d\@mtest.nim.c:206:34: error: incompatible type for argument 1 of 'getFirst__test_u20'
  206 |         T7_ = getFirst__test_u20(test3);
      |                                  ^~~~~
      |                                  |
      |                                  NI * {aka long long int *}
C:\[redacted]\nimcache\test_d\@mtest.nim.c:120:84: note: expected 'tySequence__qwqHTkRvwhrRyENtudHQ7g' but argument is of type 'NI *' {aka 'long long int *'}
  120 | N_LIB_PRIVATE N_NIMCALL(NI, getFirst__test_u20)(tySequence__qwqHTkRvwhrRyENtudHQ7g list_p0) {
      |                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
C:\[redacted]\nimcache\test_d\@mtest.nim.c:214:34: error: incompatible type for argument 1 of 'getFirst__test_u24'
  214 |         T9_ = getFirst__test_u24(test4);
      |                                  ^~~~~
      |                                  |
      |                                  NIM_CHAR * {aka char *}
C:\[redacted]\nimcache\test_d\@mtest.nim.c:130:91: note: expected 'tySequence__lBgZ7a89beZGYPl8PiANMTA' but argument is of type 'NIM_CHAR *' {aka 'char *'}
  130 | N_LIB_PRIVATE N_NIMCALL(NIM_CHAR, getFirst__test_u24)(tySequence__lBgZ7a89beZGYPl8PiANMTA list_p0) {
      |                                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
Error: execution of an external compiler program 'C:\[redacted]\Nim\nim-2.2.0\dist\mingw64\bin\gcc.exe -c  -w -fmax-errors=3 -mno-ms-bitfields   -IC:\[redacted]\Nim\nim-2.2.0\lib -Ic:\[redacted] -o C:\[redacted]\nimcache\test_d\@mtest.nim.c.o C:\[redacted]\nimcache\test_d\@mtest.nim.c' failed with exit code: 1

Expected Output

Code compiles or nim compiler catches error.

Known Workarounds

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions