Skip to content

Spicy with reserved C++ keywords can lead to C++ errors #2116

@evantypanski

Description

@evantypanski

when writing IDs in CXX codegen, there are a number of reserved keywords. These then have an underscore appended. But, we never check if that underscore then conflicts with another definition, making code like this fail to compile at C++:

module Test;

function f() {
    local and = 0;
    local and_ = 1;
    print(and);
}

f();

error:

/private/var/folders/w6/h7tg594x0yng0_yzvcsn6mv00000gn/T/Test_635187733ef11e2c-299a3d1ebdc6edf4.cc:34:42: error: redefinition of 'and_'
   34 |     ::hilti::rt::integer::safe<uint64_t> and_ = ::hilti::rt::integer::safe<std::uint64_t>{1U};
      |                                          ^
/private/var/folders/w6/h7tg594x0yng0_yzvcsn6mv00000gn/T/Test_635187733ef11e2c-299a3d1ebdc6edf4.cc:32:42: note: previous definition is here
   32 |     ::hilti::rt::integer::safe<uint64_t> and_ = ::hilti::rt::integer::safe<std::uint64_t>{0U};
      |                                          ^

Simple fix would just be to check if it starts with the reserved identifier and has only underscores after, then add another underscore. I tried to find random reserved words that would break it, but our list seems exhaustive until C++26 contract_assert :)

note that there is actually some unintuitive behavior that doesn't result in a compilation error: this program will print 1!

module Test;

global and = 0;
global and_ = 1;

print(and);

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions