zeek / spicy

C++ parser generator for dissecting protocols & files.
https://docs.zeek.org/projects/spicy
Other
243 stars 37 forks source link

Unrolling of big collection constructors fails to compile for `const` variables #1760

Closed bbannier closed 2 months ago

bbannier commented 2 months ago

With #1744 we introduced code which unrolls constructors of huge containers. The code we now generate is incorrect for const variables which do not live in a scope (it does work for globals though which are managed more thightly by the module).

module foo;

# Ctrs for collections of more than 10 elements get unrolled.
const CRC16_TABLE: vector<uint16> = [
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
];
$ spicyc -dj foo.spicy
foo_198ff91772b68fab-628dbe1417976318.cc:12:84: error: non-local lambda expression cannot have a capture-default
    const ::hilti::rt::Vector<::hilti::rt::integer::safe<uint16_t>> CRC16_TABLE = [&]() { ...
                                                                                   ^

We probably need to adjust or even drop the capture for declarations emitted outside of a proper C++ scope. ~We still need to make sure that a constructor can reference other const variables though.~ A declaration of a const can only refer to other const variables; since const variables are emitted as non-local variables we do not need to capture them.