llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.81k stars 11.91k forks source link

Clang compiles valid (?) program incorrectly #105643

Open socketpair opened 2 months ago

socketpair commented 2 months ago

https://godbolt.org/z/z4szqa4rT

#include <iostream>
#include <string>
using namespace std;

namespace {

template <class T>
consteval const char* type2str() = delete;

template <>
consteval const char* type2str<int>() {
    return "int";
};

template <typename T, const char* Name>
consteval const char* fun() {
    static constexpr auto mmm = string(type2str<T>()) + " " + Name + "\x00";
    return mmm.data();
}

}  // namespace

int main() {
    static constexpr char ZZZ[]{"qwe"}; // changing constexpr => const makes clang fail to compile (gcc eats both cases normally)

    cout << fun<int, ZZZ>();

    return 0;
}
llvmbot commented 2 months ago

@llvm/issue-subscribers-clang-frontend

Author: Коренберг Марк (socketpair)

https://godbolt.org/z/z4szqa4rT ```cpp #include <iostream> #include <string> using namespace std; namespace { template <class T> consteval const char* type2str() = delete; template <> consteval const char* type2str<int>() { return "int"; }; template <typename T, const char* Name> consteval const char* fun() { static constexpr auto mmm = string(type2str<T>()) + " " + Name + "\x00"; return mmm.data(); } } // namespace int main() { static constexpr char ZZZ[]{"qwe"}; // changing constexpr => const makes clang fail to compile (gcc eats both cases normally) cout << fun<int, ZZZ>(); return 0; } ```
MitalAshok commented 2 months ago

This looks like a dup of #82994

shafik commented 2 months ago

Currently only gcc accepts this: https://godbolt.org/z/5W53nW5rz

cor3ntin commented 2 months ago

This looks like a dup of #82994

Yes it is a Duplicate of #82994

cor3ntin commented 2 months ago

Duplicate of #82994

shafik commented 2 months ago

I don't think this is a duplicate, as I noted in the other bug clang does not have the same behavior as edg/MSVC and so if these are the same bug a more detailed explanation is needed to establish the link.

cor3ntin commented 2 months ago

There are 2 bugs here.

one with static constexpr char ZZZ[]{"qwe"} - which is definitively a clang bug and one with static const char ZZZ[]{"qwe"} - which needs a reduction