public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function
@ 2021-01-14 11:53 redi at gcc dot gnu.org
  2021-01-14 14:31 ` [Bug c++/98675] " mpolacek at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2021-01-14 11:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98675
           Summary: Accessing member of temporary outside its lifetime
                    allowed in constexpr function
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

struct X {
    int i = 0;

    constexpr int& get() { return i; }
};

constexpr int g()
{
    const auto& i = X{}.get();
    return i;  // undefined
}

constexpr auto i = g();


G++ fails to diagnose the error here.

Clang says:

<source>:13:16: error: constexpr variable 'i' must be initialized by a constant
expression
constexpr auto i = g();
               ^   ~~~
<source>:10:12: note: read of object outside its lifetime is not allowed in a
constant expression
    return i;  // undefined
           ^
<source>:13:20: note: in call to 'g()'
constexpr auto i = g();
                   ^


Intel says:

<source>(13): error: expression must have a constant value
  constexpr auto i = g();
                     ^
<source>(10): note: attempt to access expired storage
      return i;  // undefined
             ^

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98675] Accessing member of temporary outside its lifetime allowed in constexpr function
  2021-01-14 11:53 [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function redi at gcc dot gnu.org
@ 2021-01-14 14:31 ` mpolacek at gcc dot gnu.org
  2022-01-31  4:23 ` tobi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-01-14 14:31 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-01-14
                 CC|                            |mpolacek at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Confirmed.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98675] Accessing member of temporary outside its lifetime allowed in constexpr function
  2021-01-14 11:53 [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function redi at gcc dot gnu.org
  2021-01-14 14:31 ` [Bug c++/98675] " mpolacek at gcc dot gnu.org
@ 2022-01-31  4:23 ` tobi at gcc dot gnu.org
  2022-01-31  4:35 ` tobi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: tobi at gcc dot gnu.org @ 2022-01-31  4:23 UTC (permalink / raw)
  To: gcc-bugs

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

Tobias Schlüter <tobi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tobi at gcc dot gnu.org

--- Comment #2 from Tobias Schlüter <tobi at gcc dot gnu.org> ---
Here's another testcase (derived from code in the Eigen library ) that seems to
illustrate the same issue. Please follow the compiler explorer link to see a
non-constexpr version and its gimple that makes clear that there indeed is a
lifetime issue and also clang's error message https://godbolt.org/z/4zoKaoKa5

struct B;

#define CONSTEXPR constexpr
#define CONSTEVAL consteval

struct A {
public:
    CONSTEXPR A() { m_val = 1; }
    CONSTEXPR A(const A& other) { m_val = other.m_val;}
    CONSTEXPR ~A() {};

    CONSTEXPR int val() { return m_val; }

    int m_val;

    CONSTEXPR B operator<<(int);
};

struct B {
    A& m_a;
    CONSTEXPR B(A& ref) : m_a(ref) {}
    CONSTEXPR B& operator,(int i) { m_a.m_val = i; return *this; }
    CONSTEXPR ~B() { finished(); }
    CONSTEXPR A finished() { return m_a; }
};

CONSTEXPR B A::operator<<(int i) {
    m_val = i;
    return B(*this);
}

CONSTEVAL int f()
{
    A a = (A() << 1, 2, 3, 4).finished();
    return a.val();
}

int g()
{
    return f();
}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98675] Accessing member of temporary outside its lifetime allowed in constexpr function
  2021-01-14 11:53 [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function redi at gcc dot gnu.org
  2021-01-14 14:31 ` [Bug c++/98675] " mpolacek at gcc dot gnu.org
  2022-01-31  4:23 ` tobi at gcc dot gnu.org
@ 2022-01-31  4:35 ` tobi at gcc dot gnu.org
  2022-01-31  4:43 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: tobi at gcc dot gnu.org @ 2022-01-31  4:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Tobias Schlüter <tobi at gcc dot gnu.org> ---
Sorry, in my example, I think actually clang is wrong.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98675] Accessing member of temporary outside its lifetime allowed in constexpr function
  2021-01-14 11:53 [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-01-31  4:35 ` tobi at gcc dot gnu.org
@ 2022-01-31  4:43 ` pinskia at gcc dot gnu.org
  2022-01-31 11:44 ` tobi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-31  4:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Tobias Schlüter from comment #3)
> Sorry, in my example, I think actually clang is wrong.

What is the order of destruction of tempories here in the following statement:
A() << 1

Is A() destoryed before the temp B that is returned from operator <<.

Note MSVC accepts the code so it might be a bug in clang.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98675] Accessing member of temporary outside its lifetime allowed in constexpr function
  2021-01-14 11:53 [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function redi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-01-31  4:43 ` pinskia at gcc dot gnu.org
@ 2022-01-31 11:44 ` tobi at gcc dot gnu.org
  2023-05-26 15:00 ` pinskia at gcc dot gnu.org
  2023-07-26  1:45 ` cvs-commit at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: tobi at gcc dot gnu.org @ 2022-01-31 11:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Tobias Schlüter <tobi at gcc dot gnu.org> ---
I've submitted this to clang's bug tracker as well. 
https://github.com/llvm/llvm-project/issues/53494

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98675] Accessing member of temporary outside its lifetime allowed in constexpr function
  2021-01-14 11:53 [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function redi at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2022-01-31 11:44 ` tobi at gcc dot gnu.org
@ 2023-05-26 15:00 ` pinskia at gcc dot gnu.org
  2023-07-26  1:45 ` cvs-commit at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-26 15:00 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |igkper at gmail dot com

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 109991 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98675] Accessing member of temporary outside its lifetime allowed in constexpr function
  2021-01-14 11:53 [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function redi at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-05-26 15:00 ` pinskia at gcc dot gnu.org
@ 2023-07-26  1:45 ` cvs-commit at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-26  1:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
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>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-07-26  1:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-14 11:53 [Bug c++/98675] New: Accessing member of temporary outside its lifetime allowed in constexpr function redi at gcc dot gnu.org
2021-01-14 14:31 ` [Bug c++/98675] " mpolacek at gcc dot gnu.org
2022-01-31  4:23 ` tobi at gcc dot gnu.org
2022-01-31  4:35 ` tobi at gcc dot gnu.org
2022-01-31  4:43 ` pinskia at gcc dot gnu.org
2022-01-31 11:44 ` tobi at gcc dot gnu.org
2023-05-26 15:00 ` pinskia at gcc dot gnu.org
2023-07-26  1:45 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).