Skip to content

Codegen error when using methods with var parameters #18437

Open
@alistairkeys

Description

@alistairkeys

A codegen error can be triggered by creating a subclass and calling a method with 'var' parameters.

The following snippet produces an error when it gets to obj3. If you remove 'var' from the method parameters then it compiles.

Example

type
  BaseClass = ref object of RootObj
  SubClass = ref object of BaseClass
  
method foo(x: var BaseClass) {.base.} = echo "base" 
method foo(x: var SubClass) = echo "sub"
  
var obj1 = BaseClass()
obj1.foo()

var obj2: BaseClass = SubClass()
obj2.foo()

var obj3 = SubClass()
obj3.foo()

https://play.nim-lang.org/#ix=3s68

I'm not sure whether I'd expect this to work or to give a 'nice' error but a codegen error doesn't seem right.

Current Output

Without 'var' params:
base
sub
sub

With 'var' params (output taken from the Nim playground snippet mentioned earlier):

Hint: used config file '/playground/nim/config/nim.cfg' [Conf]
Hint: used config file '/playground/nim/config/config.nims' [Conf]
....
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_io.nim.c.o /usercode/nimcache/stdlib_io.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_system.nim.c.o /usercode/nimcache/stdlib_system.nim.c [Exec]
Hint: gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c [Exec]
/usercode/nimcache/@min.nim.c: In function 'NimMainModule':
/usercode/nimcache/@min.nim.c:537:39: error: expected ';' before '->' token
  T4_ = &&obj3__RMWab9bcqUlZuOCGx7H85cA->Sup;
                                       ^~
/usercode/nimcache/@min.nim.c:537:2: error: label 'obj3__RMWab9bcqUlZuOCGx7H85cA' used but not defined
  T4_ = &&obj3__RMWab9bcqUlZuOCGx7H85cA->Sup;
  ^~~
Error: execution of an external program failed: 'gcc -c  -w -fmax-errors=3   -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c'

If only obj1 and obj2 are in the example, it compiles and gives output. It seems related to creating a subclass specifically and calling the method with var parameters.

Version

Windows 10 (Version 20H2, build 19042.1052)

Nim Compiler Version 1.4.8 [Windows: amd64]
Compiled at 2021-05-25
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release

I tried yesterday's nightly (1.5.1) and that gave an error too.

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