<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 - Incorrect non-constant expression error for std::string_view::find("abc") in static_assert"
href="https://bugs.llvm.org/show_bug.cgi?id=45847">45847</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Incorrect non-constant expression error for std::string_view::find("abc") in static_assert
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>9.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</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++17
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>eracpp@eml.cc
</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>The following code:
#include <string_view>
static_assert(
std::string_view("abc").find("b") != std::string_view::npos,
"this is completely insane"
);
Fails to compile with the following error message
(<a href="https://gcc.godbolt.org/z/m3fCMg">https://gcc.godbolt.org/z/m3fCMg</a>):
error: static_assert expression is not an integral constant expression
std::string_view("abc").find("b") != std::string_view::npos,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: null passed to a callee that requires a non-null argument
Conditions seem to require:
- Clang 9
- libstdc++ (not libc++)
- C++17 (for `std::string_view`)
Clang 5, 6, 7, 8, 10, and trunk produce no error
(<a href="https://gcc.godbolt.org/z/C6fcfi">https://gcc.godbolt.org/z/C6fcfi</a>).
Compiling with libc++ produces no error (<a href="https://gcc.godbolt.org/z/2B-JMa">https://gcc.godbolt.org/z/2B-JMa</a>).
This snippet is *very* fragile.
Modifications that should have no relevance to the assertion can effect whether
the error occurs.
For example, all of the following do *not* produce the error:
Searching for `"a"` instead of `"b"` (<a href="https://gcc.godbolt.org/z/WeoPdA">https://gcc.godbolt.org/z/WeoPdA</a>):
#include <string_view>
static_assert(
std::string_view("abc").find("a") != std::string_view::npos,
"this is completely insane"
);
Reordering the inequality expression (<a href="https://gcc.godbolt.org/z/iQAYre">https://gcc.godbolt.org/z/iQAYre</a>):
#include <string_view>
static_assert(
std::string_view::npos != std::string_view("abc").find("b"),
"this is completely insane"
);
Moving the closing parenthesis to the same line as the assertion message
(<a href="https://gcc.godbolt.org/z/7sDjWV">https://gcc.godbolt.org/z/7sDjWV</a>):
#include <string_view>
static_assert(
std::string_view("abc").find("b") != std::string_view::npos,
"this is completely insane");
Adding a comment on the line above the assertion
(<a href="https://gcc.godbolt.org/z/zdh7kz">https://gcc.godbolt.org/z/zdh7kz</a>):
#include <string_view>
// Why?! How?!
static_assert(
std::string_view("abc").find("b") != std::string_view::npos,
"this is completely insane"
);
... unless `-pedantic` is used instead of `-pedantic-errors`
(<a href="https://gcc.godbolt.org/z/GCp_yn">https://gcc.godbolt.org/z/GCp_yn</a>), or the flag is removed altogether
(<a href="https://gcc.godbolt.org/z/dDCpEW">https://gcc.godbolt.org/z/dDCpEW</a>).
In short, the conditions required to produce the error seems to be completely
arbitrary and nonsensical (whitespacing, comments, compiler flags, etc.) beyond
"Clang 9 with libstdc++ and C++17 mode".</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>