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

Constructor with parameters imported from C++ is not called when --exceptions:goto #22686

Open
demotomohiro opened this issue Sep 11, 2023 · 3 comments

Comments

@demotomohiro
Copy link
Contributor

Description

When constructor with parameters imported from C++ is called, default constructor is called instead.
This happens only when compiled with --exceptions:goto option, doesn't happen with --exceptions:cpp and exceptions:quirky.

Example:
testcpp.nim

{.emit:"""/*TYPESECTION*/
struct CppClass {
  int x;

  CppClass(): x(0) {}
  CppClass(int x): x(x) {}
};
""".}

type
  CppClass {.importcpp.} = object
    x: cint

proc constructCppClass(x: cint): CppClass {.importcpp: "CppClass(@)", constructor.}

proc main =
  let cppClass = constructCppClass(123)
  echo cppClass.x

main()

Compile it with nim cpp -r --exceptions:goto testcpp.nim.

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-09-11
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: fbb5ac5
active boot switches: -d:release

Current Output

0

Expected Output

123

Possible Solution

No response

Additional Information

Nim generated C++ code compiled with --exceptions:goto:

N_LIB_PRIVATE N_NIMCALL(void, main__testcpp_u5)(void) {
	NimStringV2 colontmpD_;
	CppClass cppClass{};

Nim generated C++ code compiled with --exceptions:cpp:

N_LIB_PRIVATE N_NIMCALL(void, main__testcpp_u5)(void) {
	...
	try {
	CppClass cppClass(((int)123));
@demotomohiro
Copy link
Contributor Author

Calling constructor with parameter after declaring the variable can workaround this problem as long as it has default constructor.
Following code works correctly.

Example:

{.emit:"""/*TYPESECTION*/
struct CppClass {
  int x;

  CppClass(): x(0) {}
  CppClass(int x): x(x) {}
};
""".}

type
  CppClass {.importcpp.} = object
    x: cint

proc constructCppClass(x: cint): CppClass {.importcpp: "CppClass(@)", constructor.}

proc main =
  var cppClass: CppClass
  cppClass = constructCppClass(123)
  echo cppClass.x

main()

In Nim generated C++ code:

N_LIB_PRIVATE N_NIMCALL(void, main__testcpp_u5)(void) {
	CppClass cppClass{};
	...
	cppClass = CppClass(((int)123));

@jmgomez
Copy link
Collaborator

jmgomez commented Sep 11, 2023

Unfortunately --exceptions:goto break in a few places the C++ backend

@jmgomez
Copy link
Collaborator

jmgomez commented Oct 19, 2023

I dont think there is a solution for it other than dont use goto or use an initializer:

proc constructCppClass(x: cint = 123): CppClass {.importcpp: "CppClass(@)", constructor.}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants