<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Clang incorrectly rejects code during constant evaluation because it confused return by reference with taking address of stack object"
href="https://bugs.llvm.org/show_bug.cgi?id=49342">49342</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Clang incorrectly rejects code during constant evaluation because it confused return by reference with taking address of stack object
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++2a
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>mschellenbergercosta@googlemail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=24568" name="attach_24568" title="reproducer">attachment 24568</a> <a href="attachment.cgi?id=24568&action=edit" title="reproducer">[details]</a></span>
reproducer
This bug has been encountered when testing MSVC STL shiny new constexpr string
implementation with clang 12.x. A full reproducer is attached.
The code in question is:
```cpp
#include <string>
constexpr bool test() {
auto meow = "Hiss";
[[maybe_unused]] auto it = std::string::const_iterator{meow + 4, nullptr} -
2;
return true;
}
int main() {
static_assert(test());
}
```
This explicitly creates a string iterator and call `operator-`. Now clangs
gives the following error:
```
C:\STL\stl\inc\xstring(1964,16): warning: address of stack memory associated
with local variable '_Tmp' returned
[-Wreturn-stack-address]
return _Tmp -= _Off;
```
This stems from the conventional implementation of `operator+=`
```cpp
_CONSTEXPR20_CONTAINER _String_const_iterator& operator+=(const
difference_type _Off) noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
_Verify_offset(_Off);
#endif // _ITERATOR_DEBUG_LEVEL >= 1
_Ptr += _Off;
return *this;
}
```
Here a reference is returned that seems to be mistaken of taking the address?
The same problem happens with `operator+=`
Now the real fun part is, that I was fully unable to reproduce this in any
meaningfull way. This is the furthest I came
```cpp
#include <string>
constexpr bool test() {
auto meow = "Hiss";
[[maybe_unused]] auto it =
std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>{meow +
4, nullptr} - 2;
return true;
}
int main() {
static_assert(test());
}
```
Even simply copying the content of `_String_const_iterator` into the file and
using it instead of the real thing makes the bug go away. So there seems to be
some highly unusual problem around</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>