https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128386
Bug ID: 128386
Summary: `template for` over range of reflections promotes
constexpr function to immediate when built with ubsan
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: ivan.lazaric.gcc at gmail dot com
Target Milestone: ---
```cpp
using info = decltype(^^int);
struct refl_span {
const info* data;
int length;
consteval auto begin() const { return data; }
consteval auto end() const { return data + length; }
};
constexpr info arr[] = {^^int, ^^float, ^^char};
constexpr refl_span sp{arr, 3};
template<typename = void>
constexpr int foo(int x) {
template for (constexpr auto _ : sp) {}
return x;
}
int main() {
int x = 1;
(void)foo(x);
}
```
gcc trunk, flags: "-freflection -std=c++26 -fsanitize=undefined":
Fails to compile, error:
```
<source>: In function 'int main()':
<source>:21:12: error: call to consteval function 'foo<>(x)' is not a constant
expression
21 | (void)foo(x);
| ~~~^~~
<source>:21:12: error: the value of 'x' is not usable in a constant expression
<source>:20:7: note: 'int x' is not const
20 | int x = 1;
| ^
<source>:21:12: note: 'constexpr int foo(int) [with <template-parameter-1-1> =
void]' was promoted to an immediate function
21 | (void)foo(x);
| ~~~^~~
```
https://godbolt.org/z/jG1Ye6fEq
gcc trunk, flags: "-freflection -std=c++26":
Works.
https://godbolt.org/z/1r3jPsMzK
This code worked with ubsan in gcc 16.2, so this is a regression.
gcc 16.2, flags: "-freflection -std=c++26 -fsanitize=undefined":
Works.
https://godbolt.org/z/EPs39PTbE