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

cpp code gen bug in nim-glm #6455

Closed
dom96 opened this issue Sep 30, 2017 · 4 comments
Closed

cpp code gen bug in nim-glm #6455

dom96 opened this issue Sep 30, 2017 · 4 comments
Assignees

Comments

@dom96
Copy link
Contributor

dom96 commented Sep 30, 2017

https://github.com/stavenko/nim-glm/blob/master/tests/quaternion.nim

nim cpp tests/quaternion.nim
CC: glm_mat
Error: execution of an external compiler program 'clang++ -c  -w  -I/Users/dom/projects/nim/lib -o /Users/dom/other_projects/nim-glm/tests/nimcache/glm_vec.o /Users/dom/other_projects/nim-glm/tests/nimcache/glm_vec.cpp' failed with exit code: 1

/Users/dom/other_projects/nim-glm/tests/nimcache/glm_vec.cpp:399:13: error: declaration of reference variable 'c' requires an initializer
                NIM_CHAR& c;
                          ^
/Users/dom/other_projects/nim-glm/tests/nimcache/glm_vec.cpp:400:7: error: C-style cast from rvalue to reference type 'NIM_CHAR &' (aka 'char &')
                c = (NIM_CHAR&)0;
                    ^~~~~~~~~~~~
2 errors generated.
@mratsim
Copy link
Collaborator

mratsim commented Oct 7, 2017

Same issue in Arraymancer mratsim/Arraymancer#91.

In Arraymancer, the codegen issue was introduced by replacing higher-order functions that accepts closures by templates that uses {.inject.}

Edit:

N_NIMCALL(void, slicerMut_5HNBiHajzTdmOUheqCgr2A)(tyObject_Tensor_8gG034a4DEBvD9ctN8nyxaw& t, tyObject_SteppedSlice_rJTlbLcKC9bpAJbFSrlwOrQ* slices, NI slicesLen_0, NI val$
        tyObject_Tensor_8gG034a4DEBvD9ctN8nyxaw sliced;
        tyObject_Tensor_8gG034a4DEBvD9ctN8nyxaw T1_;
        memset((void*)(&sliced), 0, sizeof(sliced));
        memset((void*)(&T1_), 0, sizeof(T1_));
        unsafeSlicer_SRZrYFq46KhcUYobS9bdI9cg_2((&t), slices, slicesLen_0, (&T1_));
        sliced.shape = T1_.shape;
        sliced.strides = T1_.strides;
        sliced.offset = T1_.offset;
        sliced.data = T1_.data;
        {
                NI& old_val;        // <------- here, needs initializer
                old_val = (NI&)0;  // <------- here, needs initializer
                NI* data = dataArray_v3eUw6FwA4Vs9cV5O3TkzvQtest_init((&sliced));
                {
                        if (!is_C_contiguous_dDfDc3pbZUoFoNv6AVz18Atest_init((&sliced))) goto LA5_;
{                       {
                                NI i;
                                NI colontmp_;
                                NI T8_;
                                ...

@mratsim
Copy link
Collaborator

mratsim commented Oct 7, 2017

Reproduced with a small test case

import sequtils

import future


type CustomSeq[T] = object
  data: seq[T]


iterator mitems[T](cs: var CustomSeq[T]): var T {.inline,noSideEffect.} =
  for val in cs.data.mitems:
    yield val

template map[T](cs: CustomSeq[T], op:untyped): untyped =
  var dest = cs

  for x {.inject.} in dest.mitems:
    x = op
  dest

let a = CustomSeq[int](data: toSeq(1..10))

let b = a.map(x * 2)

echo b

Error:

Error: execution of an external compiler program 'clang++ -c  -w  -I/Users/tesuji/.choosenim/toolchains/nim-0.17.2/lib -o ./nimcache/arraymancer_issue_6455.o ./nimcache/arraymancer_issue_6455.cpp' failed with exit code: 1

./nimcache/arraymancer_issue_6455.cpp:260:5: error: declaration of reference variable 'x_AFjtzFoe8mPqwuVFjBw4hg_2' requires an initializer
NI& x_AFjtzFoe8mPqwuVFjBw4hg_2;
    ^~~~~~~~~~~~~~~~~~~~~~~~~~
./nimcache/arraymancer_issue_6455.cpp:538:8: error: declaration of reference variable 'val' requires an initializer
                        NI& val;
                            ^~~
./nimcache/arraymancer_issue_6455.cpp:540:10: error: C-style cast from rvalue to reference type 'NI &' (aka 'long long &')
                        val = (NI&)0;
                              ^~~~~~
3 errors generated.

There is no issue with the C backend

@mratsim
Copy link
Collaborator

mratsim commented Oct 7, 2017

Probably duplicate of #4910 and #4048 "mitems doesn't work with C++"

@krux02 krux02 self-assigned this Oct 25, 2018
@krux02
Copy link
Contributor

krux02 commented Feb 26, 2019

Just tested it. It works now.

@krux02 krux02 closed this as completed Feb 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants