Issue 176945
Summary [C++] Constant evaluation error backtrace shows incorrect value for function call arguments if they were modified in the function body
Labels new issue
Assignees
Reporter GregTheMadMonk
    Hello!

If some function modifies its arguments during constant evaluation and then has a constant evaluation error, the backtrace printed by the compiler could report that the function was called with the new argument value instead of the one it was actually called with. For
```c++
char c[3] = "Hi";

constexpr char g(int i) { i *= 2; return c[i]; }
constexpr char f(int i, int j) {
    auto line = i * 4;
    return g(line + j);
}

static_assert(f(1, 1));
```

the error is:
```
<source>:12:15: error: static assertion _expression_ is not an integral constant _expression_
   12 | static_assert(s.f(1, 1));
      | ^~~~~~~~~
<source>:4:52: note: cannot refer to element 10 of array of 3 elements in a constant _expression_
    4 |     constexpr char g(int i) const { i *= 2; return c[i]; }
      | ^
<source>:7:16: note: in call to 'this->g(10)'
    7 | return g(line + j);
      |                ^~~~~~~~~~~
<source>:12:15: note: in call to 's.f(1, 1)'
   12 | static_assert(s.f(1, 1));
      | ^~~~~~~~~
1 error generated.
```

`clang++` reports that `g` was called with `i = 10`, despite it actually being called with `i = 5`.

[Godbolt with reproduction](https://godbolt.org/z/sTMhfqv4n)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to