Closed melton1968 closed 2 months ago
I've narrowed the issue down to the following. The expression StringLiteral("")
is not constexpr
on MacOs. The error message indicates that a subobject is not being initialized.
#include <algorithm>
#include <array>
#include <string>
#include <string_view>
#include <iostream>
template <size_t N>
struct StringLiteral {
constexpr StringLiteral(const char (&_str)[N]) {
std::copy_n(_str, N - 1, std::data(arr_));
}
std::array<char, N - 1> arr_;
};
int main(int argc, char **argv) {
constexpr auto a = StringLiteral("abc");
constexpr auto b = StringLiteral(""); // This line is an error on MacOs because the value is not constexpr.
}
$ clang-mp-16 -std=c++20 ../src/tools/test.cpp
../src/tools/test.cpp:44:20: error: constexpr variable 'b' must be
initialized by a constant expression
constexpr auto b = StringLiteral("");
^ ~~~~~~~~~~~~~~~~~
../src/tools/test.cpp:44:20: note: subobject of type '_CharType'
(aka 'char') is not initialized
/opt/local/libexec/llvm-16/bin/../include/c++/v1/array:272:46: note:
subobject declared here
_ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
^
1 error generated.
Adding default initialization to the _arr
member fixes the issue.
I am unsure if this is a feature or bug for the clang/Darwin platform? Is it related to the arm
architecture? A latent bug? I have no way to test for x86/Darwin or arm/Ubuntu.
template <size_t N>
struct StringLiteral {
constexpr StringLiteral(const char (&_str)[N]) {
std::copy_n(_str, N - 1, std::data(arr_));
}
std::array<char, N - 1> arr_{};
};
Interesting, thank you so much for figuring this out. I will merge the PR later.
I think the issue may be MacOS vs Linux. My own private computer is a Mac with Apple Silicon chips as well, but I always use a Linux VM for development (so that would be Ubuntu/arm). I compile this using Clang as well as GCC on my computer, so it is very unlikely to be related to the arm architecture. The GitHub Actions pipeline we have set up also tests on Clang, but on Linux as well. We have naively assumed that testing on Clang is fine, but it appears we have to find a way to specifically test on clang/macOS.
I'm still quite confused as to how this can even happen. Isn't clang supposed to have the same front-end on different platforms?
@melton1968 , I have merged your PR. But I want to keep the issue open, because I want to figure out what exactly has caused this. I agree with @ChemiCalChems that this is all still a bit of a mystery.
I have added two different runners for macOS clang to the automated testing pipeline. I think if the issue somehow reappears we will very likely catch it. So I think it is now time to close this issue.
I get the following error when trying to build the tests using clang 16.0.6 on MacOs 14.2.1.
main
,v0.6.0
,v0.5.0
,v0.4.0
andv0.3.0
all exhibit the same issue.Am I missing something in the configuration or compiler? The details of the clang invocation are included below.