[llvm-bugs] [Bug 45847] New: Incorrect non-constant expression error for std::string_view::find("abc") in static_assert

via llvm-bugs llvm-bugs at lists.llvm.org
Fri May 8 10:58:35 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45847

            Bug ID: 45847
           Summary: Incorrect non-constant expression error for
                    std::string_view::find("abc") in static_assert
           Product: clang
           Version: 9.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++17
          Assignee: unassignedclangbugs at nondot.org
          Reporter: eracpp at eml.cc
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

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
(https://gcc.godbolt.org/z/m3fCMg):

    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
(https://gcc.godbolt.org/z/C6fcfi).
Compiling with libc++ produces no error (https://gcc.godbolt.org/z/2B-JMa).

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"` (https://gcc.godbolt.org/z/WeoPdA):

    #include <string_view>

    static_assert(
      std::string_view("abc").find("a") != std::string_view::npos,
      "this is completely insane"
    );

Reordering the inequality expression (https://gcc.godbolt.org/z/iQAYre):

    #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
(https://gcc.godbolt.org/z/7sDjWV):

    #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
(https://gcc.godbolt.org/z/zdh7kz):

    #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`
(https://gcc.godbolt.org/z/GCp_yn), or the flag is removed altogether
(https://gcc.godbolt.org/z/dDCpEW).

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".

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200508/b73f6111/attachment.html>


More information about the llvm-bugs mailing list