From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 028863858D37; Fri, 28 Apr 2023 17:48:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 028863858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682704089; bh=XqA9ytDA2pCqOcjC35rvrDgrB0UqJyDERO+RRvZCKzc=; h=From:To:Subject:Date:From; b=cXcixJXXHhueblkNjvu9XBKKSVh9I849VjVnA/S7ozalGQg2UJvVfWDCTAzE9t0A/ +YI9wYjWF5Vwzyfq0/F5nJqq2oKceW28+0Ll9QyN4XrWz56brw0BpYa5kmgfpmio4j se7K2t+8VM8v7adi4YVFFFEmrbwFgJXNtP9x+/LI= From: "lopresti at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/109671] New: Spurious dangling reference warning in GCC 13 Date: Fri, 28 Apr 2023 17:48:08 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: lopresti at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109671 Bug ID: 109671 Summary: Spurious dangling reference warning in GCC 13 Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lopresti at gmail dot com Target Milestone: --- #include struct Foo; extern Foo &get_foo_by_name(const std::string &name); const Foo &bug(bool x) { const Foo &f =3D get_foo_by_name(x ? "x" : "y"); return f; } --- Compile with "-O2 -Wall" to get the incorrect warning: : In function 'const Foo& bug(bool)': :9:14: warning: possibly dangling reference to a temporary [-Wdangling-reference] 9 | const Foo &f =3D get_foo_by_name(x ? "x" : "y"); | ^ :9:33: note: the temporary was destroyed at the end of the full expression 'get_foo_by_name(std::__cxx11::basic_string(((const char*)= (x ? "x" : "y")), std::allocator()))' 9 | const Foo &f =3D get_foo_by_name(x ? "x" : "y"); | ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~ -- Godbolt: https://godbolt.org/z/cn4W7ohGb The code is fine. (And no other compiler warns about this, including earlier GCC versions.)=