Skip to content

[Clang] Error on an enable_if attribute in ADT/StringRef.h in C++23 mode #149188

@Sirraide

Description

@Sirraide

Attempting to create an llvm::StringLiteral in C++23 mode currently causes an error. Specifically, the code we complain about is this constructor in ADT/StringRef.h:

    template <size_t N>
    constexpr StringLiteral(const char (&Str)[N])
#if defined(__clang__) && __has_attribute(enable_if)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgcc-compat"
        __attribute((enable_if(__builtin_strlen(Str) == N - 1,
                               "invalid string literal")))
#pragma clang diagnostic pop
#endif
        : StringRef(Str, N - 1) {
    }

which can be reduced to (https://godbolt.org/z/xa3zs5qj6):

template <__SIZE_TYPE__ N>
constexpr void foo(const char (&Str)[N])
__attribute((enable_if(__builtin_strlen(Str), ""))) {}

void x() {
    foo("1234");
}

which causes the following error:

<source>:3:14: error: 'enable_if' attribute expression never produces a constant expression
    3 | __attribute((enable_if(__builtin_strlen(Str), ""))) {}
      |              ^
<source>:6:5: note: in instantiation of function template specialization 'foo<5UL>' requested here
    6 |     foo("1234");
      |     ^
<source>:3:41: note: read of variable 'Str' whose value is not known
    3 | __attribute((enable_if(__builtin_strlen(Str), ""))) {}
      |                                         ^
<source>:2:33: note: declared here
    2 | constexpr void foo(const char (&Str)[N])
      |        

The enable_if attribute has been there since 2016. Interestingly, we compile this just fine in C++20 mode, and Clang 20 doesn’t complain about it at all even in C++23 mode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions