Bug 96630 - dangling reference accepted in constexpr evaluation
Summary: dangling reference accepted in constexpr evaluation
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2020-08-16 10:53 UTC by Björn Fahller
Modified: 2023-07-26 01:45 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-08-17 00:00:00


Attachments
Example code (162 bytes, text/plain)
2020-08-16 10:53 UTC, Björn Fahller
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Björn Fahller 2020-08-16 10:53:33 UTC
Created attachment 49065 [details]
Example code

This program uses a reference to an out-of-scope local variable in constexpr context.

struct ii
{
    int v;
    constexpr const int& get() const { return v;}
    constexpr ii& inc() { ++v; return *this;}
};

struct wrap
{
    ii i;
    constexpr const int& getnext() const { auto copy = i; return copy.inc().get();}
};

template <int> struct S {};
S<wrap{{3}}.getnext()> s;

It is, however, accepted and compiles. If used in runtime evaluation, the undefined behaviour causes wildly different results depending on optimization level.

bf :-) confuciusornis /tmp> g++-10 --version
g++-10 (Ubuntu 10-20200411-0ubuntu1) 10.0.1 20200411 (experimental) [master revision bb87d5cc77d:75961caccb7:f883c46b4877f637e0fa5025b4d6b5c9040ec566]
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

bf :-) confuciusornis /tmp> g++-10 -std=c++20 -c -Wall -Wextra -pedantic t.cpp
bf :-) confuciusornis /tmp> 


Godbolt link: https://godbolt.org/z/8q91dv
Comment 1 GCC Commits 2023-07-26 01:45:41 UTC
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:9fdbd7d6fa5e0a76898dd66658934e3184111680

commit r14-2773-g9fdbd7d6fa5e0a76898dd66658934e3184111680
Author: Nathaniel Shead <nathanieloshead@gmail.com>
Date:   Sun Jul 23 01:15:14 2023 +1000

    c++: Track lifetimes in constant evaluation [PR70331,PR96630,PR98675]
    
    This adds rudimentary lifetime tracking in C++ constexpr contexts,
    allowing the compiler to report errors with using values after their
    backing has gone out of scope. We don't yet handle other ways of
    accessing values outside their lifetime (e.g. following explicit
    destructor calls).
    
            PR c++/96630
            PR c++/98675
            PR c++/70331
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (constexpr_global_ctx::is_outside_lifetime): New
            function.
            (constexpr_global_ctx::get_value): Don't return expired values.
            (constexpr_global_ctx::get_value_ptr): Likewise.
            (constexpr_global_ctx::remove_value): Mark value outside
            lifetime.
            (outside_lifetime_error): New function.
            (cxx_eval_call_expression): No longer track save_exprs.
            (cxx_eval_loop_expr): Likewise.
            (cxx_eval_constant_expression): Add checks for outside lifetime
            values. Remove local variables at end of bind exprs, and
            temporaries after cleanup points.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1y/constexpr-lifetime1.C: New test.
            * g++.dg/cpp1y/constexpr-lifetime2.C: New test.
            * g++.dg/cpp1y/constexpr-lifetime3.C: New test.
            * g++.dg/cpp1y/constexpr-lifetime4.C: New test.
            * g++.dg/cpp1y/constexpr-lifetime5.C: New test.
            * g++.dg/cpp1y/constexpr-lifetime6.C: New test.
    
    Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>