public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation
@ 2021-09-10 20:57 johelegp at gmail dot com
  2021-09-10 21:23 ` [Bug c++/102284] " johelegp at gmail dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2021-09-10 20:57 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102284
           Summary: Can access object outside of its lifetime during
                    constant evaluation
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: johelegp at gmail dot com
                CC: johelegp at gmail dot com
  Target Milestone: ---

This is prohibited at https://eel.is/c++draft/basic.life#7.1.
See https://godbolt.org/z/exKnnT935.
```C++
#include <memory>
struct X { int x; };
int main() {
  []() consteval {
    {
      X x{};
      x.~X();
      ++x.x;
      std::construct_at(&x);
    }
    {
      union { X x{}; };
      x.~X();
      ++x.x;
    }
  }();
}
```

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

* [Bug c++/102284] Can access object outside of its lifetime during constant evaluation
  2021-09-10 20:57 [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation johelegp at gmail dot com
@ 2021-09-10 21:23 ` johelegp at gmail dot com
  2021-09-10 21:24 ` johelegp at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2021-09-10 21:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
There's also https://eel.is/c++draft/basic.life#9.sentence-1 to consider.
See https://godbolt.org/z/P97Kaqhv8.
```C++
struct X { int x; };
int main() {
  []() consteval {
    X x{};
    x.~X();
  }();
}
```

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

* [Bug c++/102284] Can access object outside of its lifetime during constant evaluation
  2021-09-10 20:57 [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation johelegp at gmail dot com
  2021-09-10 21:23 ` [Bug c++/102284] " johelegp at gmail dot com
@ 2021-09-10 21:24 ` johelegp at gmail dot com
  2021-09-10 21:26 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2021-09-10 21:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
Fixed that: https://godbolt.org/z/YGf4GTP5P.
```C++
struct X { constexpr ~X() { } };
int main() {
  []() consteval {
    X x{};
    x.~X();
  }();
}
```

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

* [Bug c++/102284] Can access object outside of its lifetime during constant evaluation
  2021-09-10 20:57 [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation johelegp at gmail dot com
  2021-09-10 21:23 ` [Bug c++/102284] " johelegp at gmail dot com
  2021-09-10 21:24 ` johelegp at gmail dot com
@ 2021-09-10 21:26 ` pinskia at gcc dot gnu.org
  2021-09-11  1:04 ` johelegp at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-10 21:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think there might be a dup of this bug.

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

* [Bug c++/102284] Can access object outside of its lifetime during constant evaluation
  2021-09-10 20:57 [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation johelegp at gmail dot com
                   ` (2 preceding siblings ...)
  2021-09-10 21:26 ` pinskia at gcc dot gnu.org
@ 2021-09-11  1:04 ` johelegp at gmail dot com
  2022-01-17 11:12 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2021-09-11  1:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
If there is, I confirmed it's not in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55004.

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

* [Bug c++/102284] Can access object outside of its lifetime during constant evaluation
  2021-09-10 20:57 [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation johelegp at gmail dot com
                   ` (3 preceding siblings ...)
  2021-09-11  1:04 ` johelegp at gmail dot com
@ 2022-01-17 11:12 ` pinskia at gcc dot gnu.org
  2022-09-24 20:11 ` johelegp at gmail dot com
  2022-09-27  1:40 ` johelegp at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-17 11:12 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-01-17
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=102623

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

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

* [Bug c++/102284] Can access object outside of its lifetime during constant evaluation
  2021-09-10 20:57 [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation johelegp at gmail dot com
                   ` (4 preceding siblings ...)
  2022-01-17 11:12 ` pinskia at gcc dot gnu.org
@ 2022-09-24 20:11 ` johelegp at gmail dot com
  2022-09-27  1:40 ` johelegp at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2022-09-24 20:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
Another example.

Simplified: https://godbolt.org/z/z781c56PM.

```C++
struct ratio {
  int numerator;
};

template<auto> struct constant;

template<auto F> concept constant_invocable = requires { typename
constant<(F(), 0)>; };

static_assert(not constant_invocable<[] {
  ratio r;
  (void)int{r.*&ratio::numerator};
}>);
```

```
<source>:9:15: error: static assertion failed
    9 | static_assert(not constant_invocable<[] {
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~
   10 |   ratio r;
      |   ~~~~~~~~     
   11 |   (void)int{r.*&ratio::numerator};
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   12 | }>);
      | ~~             
Compiler returned: 1
```

Found while opening https://github.com/llvm/llvm-project/issues/57958:
https://godbolt.org/z/KWfvEnaae.

```C++
#include <concepts>
#include <cstdint>
#include <functional>
#include <type_traits>

struct ratio {
  std::intmax_t numerator;
  std::intmax_t denominator{1};
};

template<auto F> concept constant_invocable = requires { typename
std::integral_constant<int, (F(), 0)>; };

template<auto P> requires std::is_member_object_pointer_v<decltype(P)>
inline constexpr bool default_initialization_leaves_uninitialized =
  []<class T, class M>(M T::*) {
    return not constant_invocable<[]() {
      T v;
      (void)M{v.*P};
    }>;
  }(P);

static_assert(default_initialization_leaves_uninitialized<&ratio::numerator>);
static_assert(not
default_initialization_leaves_uninitialized<&ratio::denominator>);
```

```
<source>:22:15: error: static assertion failed
   22 |
static_assert(default_initialization_leaves_uninitialized<&ratio::numerator>);
      |              
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 1
```

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

* [Bug c++/102284] Can access object outside of its lifetime during constant evaluation
  2021-09-10 20:57 [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation johelegp at gmail dot com
                   ` (5 preceding siblings ...)
  2022-09-24 20:11 ` johelegp at gmail dot com
@ 2022-09-27  1:40 ` johelegp at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2022-09-27  1:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
This may be related. Assigning to another member doesn't end the lifetime of
the active one: https://godbolt.org/z/eMGY5ehnb.

```C++
#include <memory>

struct symbol { };

constexpr symbol one{};

struct unit {
  const struct symbol* symbol = &one;

  struct not_one_t { };

  union to_the {
    not_one_t not_one = {}; // Omits ¹ from NTTP.
    int i;

    to_the() = default;
    constexpr to_the(int i) : i{i} {
      if (i == 1) {
        // std::destroy_at(&this->i); // On GCC, doesn't help.
        not_one = {};
        // std::construct_at(&not_one); // On GCC, initializes `not_one`,
doesn't end the lifetime of `i`.
      }
    }
  } exponent = {};

  constexpr unit(const struct symbol& s)
    : symbol{&s} { }
  constexpr unit(const struct symbol& s, auto e)
    : symbol{&s}, exponent{e} { }
};

template<auto> void print() { }

int main() {
  constexpr auto U = unit{one, 1};
  print<U>();
  print<unit{one}>();
  static_assert(&U.exponent.not_one);
  // static_assert(U.exponent.i == 1); // Works on GCC (and MSVC).
}
```

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

end of thread, other threads:[~2022-09-27  1:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-10 20:57 [Bug c++/102284] New: Can access object outside of its lifetime during constant evaluation johelegp at gmail dot com
2021-09-10 21:23 ` [Bug c++/102284] " johelegp at gmail dot com
2021-09-10 21:24 ` johelegp at gmail dot com
2021-09-10 21:26 ` pinskia at gcc dot gnu.org
2021-09-11  1:04 ` johelegp at gmail dot com
2022-01-17 11:12 ` pinskia at gcc dot gnu.org
2022-09-24 20:11 ` johelegp at gmail dot com
2022-09-27  1:40 ` johelegp at gmail dot com

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).