-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 var T #10219
Comments
IMO, proc unsafeIndex[T](this: var Vector[T], i: csize): var T
{.importcpp: "(&#[#])", header: "vector".}
`` |
Thank for your help. I tried using a pointer like you suggested because it seems to be the thing to do but the problem is that I get another invalid code generation error: import strformat
type
Vector* {.importcpp: "std::vector", header: "vector".}[T] = object
proc initVector*[T](n: csize): Vector[T]
{.importcpp: "std::vector<'*0>(@)", header: "vector".}
proc size*[T](this: Vector[T]): csize
{.noSideEffect, importcpp: "size", header: "vector".}
proc checkIndex[T](this: Vector[T], i: csize) : csize {.inline.} =
if 0 > i or i > this.size:
raise newException(IndexError,
&"index out of bounds: 0 <= (i:{i}) <= (n:{this.size})")
result = i
proc unsafeIndex[T](this: var Vector[T], i: csize): var T
{.importcpp: "&#[#]", header: "vector".}
proc `[]`*[T](this: var Vector[T], i: Natural): var T {.inline, noinit.} =
result = this.unsafeIndex(this.checkIndex i)
proc `[]=`*[T](this: var Vector[T], i: Natural, val: T) {.inline, noinit.} =
this.unsafeIndex(this.checkIndex i) = val
var v1 = initVector[int](10)
v1[0] = 100 # <-- wrong code generated for this line
echo v1[0] which gives the following error:
|
No, we try to map it to |
No, why should there be a |
Because |
I think we can und should map |
Might be related to #8053 as I also get a null pointer dereference with a var T result. |
This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions. |
Shoudlnt this be closed after the test case? |
The test case hasn't been merged cc @ringabout |
For the following example, the code that is generated is wrong and induce a segmentation fault.
Example
The error comes from the following generated lines:
Current Output
Expected Output
Workarounds
Here is the code generated:
Additional Information
nim --version
The text was updated successfully, but these errors were encountered: