Skip to content

gcc fails to compile: expected 't1' but argument is of type 't2' #17200

@avdva

Description

@avdva

gcc fails to compile badType proc, while the same code in when isMainModule block works fine.
If you declare s as a global variable, this also compiles fine.

Example

proc use[T](g: proc(): var T) =
  echo(g())

proc badType() =
  var s: seq[int] = @[1, 2, 3]
  var closure = proc(): var seq[int] =
    return s
  use(closure)

when isMainModule:
  #[ this commented code works fine
  var s: seq[int] = @[1, 2, 3]
  var closure = proc(): var seq[int] =
    return s
  use(closure) ]#
  badType() # this call produces C compilation error

Current Output

  Verifying dependencies for nimtest@0.1.0
   Building nimtest/nimtest using c backend
/home/avd/dev/nimtest/src/nimtest.nim: In function ‘badType__Wk9cdQgkZI877fwIj5IfjPg’:
/home/avd/dev/nimtest/src/nimtest.nim:8:32: error: incompatible type for argument 1 of ‘use__qzG4Clh4pbLeBwUaK9c9aX0w’
    8 |   use(closure)
      |                                ^      
      |                                |
      |                                tyProc__HQtG9aC8WaXzy88YlUKUG5Q
/home/avd/dev/nimtest/src/nimtest.nim:1:93: note: expected ‘tyProc__P83sEOIv6ixRRzxTZPWsMw’ but argument is of type ‘tyProc__HQtG9aC8WaXzy88YlUKUG5Q’
    1 | proc use[T](g: proc(): var T) =
      |                                                                                             ^
Error: execution of an external compiler program 'gcc -c  -w -fmax-errors=3 -g3 -Og   -I/home/avd/nim/lib -I/home/avd/dev/nimtest/src -o /home/avd/.cache/nim/nimtest_d/@mnimtest.nim.c.o /home/avd/.cache/nim/nimtest_d/@mnimtest.nim.c' failed with exit code: 1


       Tip: 1 messages have been suppressed, use --verbose to show them.
     Error: Build failed for package: nimtest
        ... Execution failed with exit code 1
        ... Command: /home/avd/nim/bin/nim c --colors:on --noNimblePath --debugger:native -d:NimblePkgVersion=0.1.0 --hints:off -o:/home/avd/dev/nimtest/nimtest /home/avd/dev/nimtest/src/nimtest.nim

Expected Output

Successful compilation, or nim compiler error.

Nim Compiler Version 1.4.4 [Linux: amd64]
Compiled at 2021-02-23
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 2ff517462bf8609b30e6134c96658aa7912b628a

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions