Issue 149188
Summary [Clang] Error on an `enable_if` attribute in `ADT/StringRef.h` in C++23 mode
Labels clang:frontend, regression, rejects-valid
Assignees
Reporter 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`:
```c++
    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):
```c++
template <__SIZE_TYPE__ N>
constexpr void foo(const char (&Str)[N])
    __attribute((enable_if(__builtin_strlen(Str) == N - 1,
                            "invalid string literal")))
    {}

void x() {
    foo("1234");
}
```
which causes the following error:
```console
<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. 
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to