Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
n0bra1n3r opened this issue May 9, 2021 · 5 comments · Fixed by #21169
Open

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

n0bra1n3r opened this issue May 9, 2021 · 5 comments · Fixed by #21169

Comments

@n0bra1n3r
Copy link

n0bra1n3r commented May 9, 2021

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
@n0bra1n3r
Copy link
Author

I'm having a go at fixing this thinking this could be a good newbie issue, but maybe it's not? Basing on the previous fix, seems like discardCheck is where I should look, but that proc has changed significantly since the fix was merged. Now looks like I should do something in the implicitlyDiscardable if-branch?

@Araq
Copy link
Member

Araq commented May 10, 2021

I would fix the C++ code generator instead.

@bung87
Copy link
Collaborator

bung87 commented Dec 23, 2022

where's T1_ comming from? it seems its related code shouldn't be generated. but I dont know where the part in compiler generate it.

@Araq
Copy link
Member

Araq commented Dec 23, 2022

cgen.getTemp, most likely called by ccgcalls.nim

@bung87
Copy link
Collaborator

bung87 commented Dec 23, 2022

dont know what to do , var String can be simply change to String here.

bung87 added a commit to bung87/Nim that referenced this issue Dec 24, 2022
bung87 added a commit to bung87/Nim that referenced this issue Dec 28, 2022
Araq pushed a commit that referenced this issue Jan 27, 2023
…efault definition would be ill-formed (#21169)

* add test

* fix #17982 Invalid C++ code generation when returning discardable var T

* fix #13093

* cpp atomic good example

* clearify the condition
@ringabout ringabout reopened this Jan 31, 2023
survivorm pushed a commit to survivorm/Nim that referenced this issue Feb 28, 2023
…se the default definition would be ill-formed (nim-lang#21169)

* add test

* fix nim-lang#17982 Invalid C++ code generation when returning discardable var T

* fix nim-lang#13093

* cpp atomic good example

* clearify the condition
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
…se the default definition would be ill-formed (nim-lang#21169)

* add test

* fix nim-lang#17982 Invalid C++ code generation when returning discardable var T

* fix nim-lang#13093

* cpp atomic good example

* clearify the condition
bung87 added a commit to bung87/Nim that referenced this issue Apr 23, 2023
bung87 added a commit to bung87/Nim that referenced this issue Jul 29, 2023
…se the default definition would be ill-formed (nim-lang#21169)

* add test

* fix nim-lang#17982 Invalid C++ code generation when returning discardable var T

* fix nim-lang#13093

* cpp atomic good example

* clearify the condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants