Skip to content

Runtime index on const array (of converted obj) causes C-compiler error #10514

@zargot

Description

@zargot

Indexing with a runtime index, into a const-array of const-objects (constructed via a converter), causes a C-compile error.

In the example below, bar is a const array consisting of b, which is a const object constructed with the int-to-object converter toObj. Access into bar with the runtime index i_runtime, causes a compile error in the C backend compiler.

Example

type    
    Obj = object    
        x: int    
    
    Tup = tuple    
        x: int    
    
converter toObj(x: int): Obj = Obj(x: x)
converter toTup(x: int): Tup = (x,)
                                   
const
    a = Obj(x: 0)    # uses normal object constructor
    b: Obj = 1       # uses object converter             
    b_tuple: Tup = 1 # uses tuple converter     
    foo = [a]                                  
    bar = [b]    
    bar_tuple = [b_tuple]
                             
let 
    bar_runtime = [b]
                         
proc main() =
    const i = 0  
    let i_runtime = 0
                         
    # works
    echo foo[i]
    echo bar[i]    

    # works                                                         
    echo foo[i_runtime]   

    # compile error in C-compiler
    echo bar[i_runtime]
           
    # works (with tuple) 
    echo bar_tuple[i_runtime] 
               
    # works (with runtime constructed array)
    echo bar_runtime[i_runtime]                                            

main() 

Current Output

$ nim c -r const_array.nim
Hint: used config file '/home/zargot/projects/thirdparty/Nim/config/nim.cfg' [Conf]
Hint: used config file '/home/zargot/projects/thirdparty/Nim/config/config.nims' [Conf]
Hint: system [Processing]
Hint: const_array [Processing]
CC: const_array
Error: execution of an external compiler program 'gcc -c  -w  -I/home/zargot/projects/thirdparty/Nim/lib -I/home/zargot/projects/thirdparty/nim_bugs -o /home/zargot/.cache/nim/const_array_d/c
onst_array.c.o /home/zargot/.cache/nim/const_array_d/const_array.c' failed with exit code: 1

/home/zargot/.cache/nim/const_array_d/const_array.c:82:72: error: ‘T1_’ undeclared here (not in a function)
 NIM_CONST tyArray_eZVKehVP1XnECVJepajLtw bar_Iyn5d2bOFHV6BLKifeGnHw = {T1_}
                                                                        ^~~

Additional Information

$ nim -v
Nim Compiler Version 0.19.9 [Linux: amd64]
Compiled at 2019-01-31
Copyright (c) 2006-2018 by Andreas Rumpf

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

$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp --enable-cet=auto
Thread model: posix
gcc version 8.2.1 20181127 (GCC)

Edit:

  • cleanup
  • add gcc version

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions