From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6F7553858D20; Wed, 12 Jul 2023 13:52:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6F7553858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689169949; bh=YvOwkVUiJf1txZEex+IHz5qKjk9G5jMCa3K0qrI6wzY=; h=From:To:Subject:Date:From; b=GQeloXsj9WX80yuDEBsf0xdCbTx8fVde2TF7gdlADc7rc5Ig7XNcbn+qdaKJf4Xqb c3ZSG0nsdkHO4856CGzCybhjza6ikxaAU9zS3qzY1P13LxQ3EHFo4ihF5SeubtfDrx mIBYlIMoR6ekyXapXWscIhmwrCsfploysI96MO6c= From: "fchelnokov at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/110642] New: Undefined behavior in same constant expression is found not always Date: Wed, 12 Jul 2023 13:52:28 +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: fchelnokov 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=3D110642 Bug ID: 110642 Summary: Undefined behavior in same constant expression is found not always Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: fchelnokov at gmail dot com Target Milestone: --- This lambda auto f =3D [](int i) { const auto y =3D i; return [&] () { return y; }; }; returns another lambda that makes undefined behavior: it accesses a stack variable (y) after the end of its life. And it is successfully caught during constant expression evaluation by GCC = and other compilers, if verified like this: constexpr auto g =3D f(3)(); static_assert( f(3)() =3D=3D 3 ); Online demo: https://gcc.godbolt.org/z/3Y4cGMG15 But if one changes the order of lines: static_assert( f(3)() =3D=3D 3 ); constexpr auto g =3D f(3)(); then GCC stops detecting undefined behavior. Online demo: https://gcc.godbolt.org/z/dojhKasPs Could you please review why this happens?=