Skip to content

Invalid C++ code generation when returning discardable var T (variant of #10241) #17982

@n0bra1n3r

Description

@n0bra1n3r

The issue #10241 still occurs, but with slightly different code.

Example

Modified test case for #10322 has the same issue as reported:

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
    # bug seems to trigger when `#`, `@`, or `$1` is used inside `importcpp`
    {.importcpp: "#.append(@)", header: "string", discardable.} # <- changed from `importcpp: "append"`

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

$ ./src/Nim/bin/nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-05-09
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: d84a3b10b5540d77a3501b9269dabeaedad542de
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