hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.23k stars 224 forks source link

[BUG] constexpr initialization for std::array #1104

Closed MaxSagebaum closed 1 week ago

MaxSagebaum commented 3 weeks ago

It seems that constexpr initalization is generated the wrong way.

var : std::array<char, 5> == "abcd";

is lowered to

std::array<char,5> constexpr var = "abcd";

which results into the error:

main.cpp2:8:34: error: no viable conversion from 'const char[5]' to 'const std::array<char, 5>'

The correct lowering in this case would be:

std::array<char, 5> constexpr var {"abcd"};

this version, without the constexpr, is used for var : std::array<char, 5> = "abcd";.

Reproducer: https://cpp2.godbolt.org/z/KbPvxPdoq

bluetarpmedia commented 3 weeks ago

I'm not sure if converting from a string literal "abcd" to std::array<char> is intentionally supported or not. My guess is this syntax for non-constexpr init:

var2 : std::array<char, 5> = "abcd";

only happens to work because of the cppfront rules of turning that into {"abcd"}.

But I think the Cpp2 idiom for initialising a std::array<char> is:

var1 : std::array<char, 5> =  ('a', 'b', 'c', 'd', '\0');
var2 : std::array<char, 5> == ('a', 'b', 'c', 'd', '\0');  // constexpr

That second var2 line lowers to:

std::array<char,5> constexpr var2{ 'a', 'b', 'c', 'd', '\0' };
hsutter commented 1 week ago

Thanks!

only happens to work because of the cppfront rules of turning that into {"abcd"}.

Then == should be consistent with that, I think. Fixing...

MaxSagebaum commented 1 week ago

Thanks.