SciNim / flambeau

Nim bindings to libtorch
85 stars 3 forks source link

Problems with ArrayRef in the C++ compiler #9

Closed HugoGranstrom closed 3 years ago

HugoGranstrom commented 3 years ago

On the latest master commit running poc03_arrayrefs.nim using nim r --cc:vcc --backend:cpp --gc:arc --experimental:views poc03_arrayrefs.nim fails in the c++ compiler with the following error message:

C:\Users\hugog\nimcache\poc03_arrayrefs_d\@mpoc03_arrayrefs.nim.cpp(263): error C2440: '=': cannot convert from 'const T *' to 'NI64 *'
        with
        [
            T=int64_t
        ]
C:\Users\hugog\nimcache\poc03_arrayrefs_d\@mpoc03_arrayrefs.nim.cpp(263): note: Conversion loses qualifiers
C:\Users\hugog\nimcache\poc03_arrayrefs_d\@mpoc03_arrayrefs.nim.cpp(282): error C2440: '=': cannot convert from 'const T *' to 'NI64 *'
        with
        [
            T=int64_t
        ]
C:\Users\hugog\nimcache\poc03_arrayrefs_d\@mpoc03_arrayrefs.nim.cpp(282): note: Conversion loses qualifiers

Lines 262 and 263 are:

NI64* T4_;
...
...
...
T4_ = (NI64*)0; // 262
T4_ = t__9bjO9cCSag10Z9adt9cTzQW9a4A.sizes().data(); //263

And they are generated from line 7 in the nim file:

echo t.sizes().asNimView() # line 7

The definitions of sizes and data is:

func sizes*(a: Tensor): IntArrayRef {.importcpp:"#.sizes()".}

func data*[T](ar: ArrayRef[T]): ptr UncheckedArray[T] {.importcpp: "#.data()".}

To me, it looks like perhaps the compiler can't convert between NI64* and int64_t*. Edit: Or it probably has something to do with const. Is it because T4_ is initialized that it complains? Tried adding noInit everywhere I could but it still initialized it to 0 on the line above :/ Otherwise we could go around this by using const_cast from C++ somehow.

HugoGranstrom commented 3 years ago

fixed by 86f25d40dd5b4d8059c5ed1c081dd9a2118ab1a8