https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88388

            Bug ID: 88388
           Summary: GCC falsely determines string literal to be unused
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Kimon.Hoffmann at lawo dot com
  Target Milestone: ---

I have recently run into an issue with the (minimized) example below.

Test.cpp:
    #include <string_view>

    constexpr std::string_view test_func() noexcept
    {
        constexpr auto const CHARACTER_SOUP = std::string_view{"somechars"};
        return CHARACTER_SOUP;
    }

    void f(std::string_view);

    void call_f()
    {
        f(test_func());
    }

Compiled with:
    g++ -std=c++17 -Wall -Wextra -c -S Test.cpp

Expected result:
    The generated assembly should contain the string literal "somechars" and
pass it from call_f() to f() in the form of a std::string_view instance.

Actual result:
    The generated assembly does not contain the string literal and a default
constructed std::string_view instance is passed from call_f() to f() instead.

Notes:
    * Newer versions of GCC produce the expected result.
    * Please note that this error happens on any optimization level including
-O0. But using -O3 makes thee generated assembly more easy to follow.
    * While this issue has some similarity to the issue I (erroneously)
reported as bug #85873, the major difference here is that string literals have
static storage duration.

Link:
    Here is a the example on godbolt.org: https://godbolt.org/z/iKxQga
    Please note that this example uses -O3 to make the generate assembly as
concise as possible.

Reply via email to