Skip to content

Invalid C++ code generation when returning discardable var T #10241

@BigEpsilon

Description

@BigEpsilon

Here is a bug that affected me multiple times in the past, it also seems to be different than #10219.

Nim generate C++ code with uninitiated references.

Example

import strformat

type
  String* {.importcpp: "std::string", header: "string".} = object

proc initString*(): String
    {.importcpp: "std::string()", header: "string".}

proc append*(this: var String, str: String): var String
    {.importcpp: "append", header: "string", discardable.}

var
  s1 = initString()
  s2 = initString()

s1.append s2

Current Output

Error: execution of an external compiler program 'g++ -c  -w -w -fpermissive  -I'/home/user/.choosenim/toolchains/nim-#devel/lib' -I/home/user/dev/nim/tests/play/src -o /home/user/.cache/nim/play_d/play_play.cpp.o /home/user/.cache/nim/play_d/play_play.cpp' failed with exit code:
1

/home/user/.cache/nim/play_d/play_play.cpp: In function ‘void NimMainModule()’:
/home/user/.cache/nim/play_d/play_play.cpp:149:15: error: ‘T1_’ declared as reference but not initialized
  std::string& T1_;
               ^
/home/user/.cache/nim/play_d/play_play.cpp:156:22: error: invalid cast of an rvalue expression of type ‘int’ to type ‘std::__cxx11::string& {aka std::__cxx11::basic
_string<char>&}’
  T1_ = (std::string&)0;

Expected Output

no compilation error

Additional Information

$ nim -v
Nim Compiler Version 0.19.9 [Linux: amd64]
Compiled at 2019-01-03
Copyright (c) 2006-2018 by Andreas Rumpf

active boot switches: -d:release

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