public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105846] New: constexpr unique_ptr doesn't work with virtual types
@ 2022-06-04 16:15 benni at stuerz dot xyz
  2022-06-04 17:07 ` [Bug c++/105846] " benni at stuerz dot xyz
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: benni at stuerz dot xyz @ 2022-06-04 16:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105846
           Summary: constexpr unique_ptr doesn't work with virtual types
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: benni at stuerz dot xyz
  Target Milestone: ---

I stumbled on this bug while creating my own constexpr smart pointers.
This does NOT happen with clang + libstdc++!

A short demonstration/explanation of the problem:
https://godbolt.org/z/va4nYjhbe

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

* [Bug c++/105846] constexpr unique_ptr doesn't work with virtual types
  2022-06-04 16:15 [Bug c++/105846] New: constexpr unique_ptr doesn't work with virtual types benni at stuerz dot xyz
@ 2022-06-04 17:07 ` benni at stuerz dot xyz
  2022-06-04 21:36 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: benni at stuerz dot xyz @ 2022-06-04 17:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Benjamin Stürz <benni at stuerz dot xyz> ---
This bug goes (at least) as far back as GCC 10.1.0. It also happens on
ARM/ARM64 btw.

Improved Example:
https://godbolt.org/z/PPfG9Eqvc

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

* [Bug c++/105846] constexpr unique_ptr doesn't work with virtual types
  2022-06-04 16:15 [Bug c++/105846] New: constexpr unique_ptr doesn't work with virtual types benni at stuerz dot xyz
  2022-06-04 17:07 ` [Bug c++/105846] " benni at stuerz dot xyz
@ 2022-06-04 21:36 ` pinskia at gcc dot gnu.org
  2022-06-04 21:39 ` [Bug c++/105846] [C++2b] " pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-04 21:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 53083
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53083&action=edit
test.cc

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

* [Bug c++/105846] [C++2b] constexpr unique_ptr doesn't work with virtual types
  2022-06-04 16:15 [Bug c++/105846] New: constexpr unique_ptr doesn't work with virtual types benni at stuerz dot xyz
  2022-06-04 17:07 ` [Bug c++/105846] " benni at stuerz dot xyz
  2022-06-04 21:36 ` pinskia at gcc dot gnu.org
@ 2022-06-04 21:39 ` pinskia at gcc dot gnu.org
  2022-06-04 21:40 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-04 21:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 53084
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53084&action=edit
Second example -std=c++20

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

* [Bug c++/105846] [C++2b] constexpr unique_ptr doesn't work with virtual types
  2022-06-04 16:15 [Bug c++/105846] New: constexpr unique_ptr doesn't work with virtual types benni at stuerz dot xyz
                   ` (2 preceding siblings ...)
  2022-06-04 21:39 ` [Bug c++/105846] [C++2b] " pinskia at gcc dot gnu.org
@ 2022-06-04 21:40 ` pinskia at gcc dot gnu.org
  2022-06-06 12:57 ` redi at gcc dot gnu.org
  2022-07-13 22:45 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-04 21:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Can you please attach (or put the testcases inline) instead of linking to
godbolt next time?

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

* [Bug c++/105846] [C++2b] constexpr unique_ptr doesn't work with virtual types
  2022-06-04 16:15 [Bug c++/105846] New: constexpr unique_ptr doesn't work with virtual types benni at stuerz dot xyz
                   ` (3 preceding siblings ...)
  2022-06-04 21:40 ` pinskia at gcc dot gnu.org
@ 2022-06-06 12:57 ` redi at gcc dot gnu.org
  2022-07-13 22:45 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-06-06 12:57 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-06-06

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Reduced to remove the library dependency:

// This is just a simple (incomplete) unique_ptr implementation that should
work with C++20/GCC >= 10.1.
// The details of this class don't matter. It also fails with std::unique_ptr
with GCC=12.1/trunk
template<class T>
class simple_pointer {
private:
    T *ptr{};
public:
    constexpr simple_pointer() noexcept = default;
    constexpr simple_pointer(T *p) noexcept : ptr{p} {}
    simple_pointer(const simple_pointer &) = delete;
    constexpr simple_pointer(simple_pointer &&p) noexcept : ptr{p.ptr} {}
    constexpr ~simple_pointer() noexcept { delete ptr; }

    void operator=(const simple_pointer &) = delete;
    constexpr simple_pointer &operator=(simple_pointer &&p) noexcept {
        delete ptr;
        ptr = p.ptr;
        p.ptr = nullptr;
        return *this;
    }

    constexpr T &operator*() const noexcept { return *ptr; }
    constexpr T *operator->() const noexcept { return ptr; }
};

template<class T>
constexpr simple_pointer<T> make_simple() {
    return simple_pointer<T>(new T());
}

// Just some random struct that has a vtable.
struct S {
    int x{42};

    // Removing this virtual...
    constexpr virtual ~S() noexcept = default;
};

// ... or this consteval makes it work again.
consteval int run() {
    auto p = make_simple<S>();
    return p->x;
}

int main() {
    return run();
}



105846.C: In function ‘int main()’:
105846.C:46:15:   in ‘constexpr’ expansion of ‘run()’
105846.C:43:1:   in ‘constexpr’ expansion of ‘(&
p)->simple_pointer<S>::~simple_pointer()’
105846.C:12:44: error: ‘virtual constexpr S::~S()’ used before its definition
   12 |     constexpr ~simple_pointer() noexcept { delete ptr; }
      |                                            ^~~~~~~~~~

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

* [Bug c++/105846] [C++2b] constexpr unique_ptr doesn't work with virtual types
  2022-06-04 16:15 [Bug c++/105846] New: constexpr unique_ptr doesn't work with virtual types benni at stuerz dot xyz
                   ` (4 preceding siblings ...)
  2022-06-06 12:57 ` redi at gcc dot gnu.org
@ 2022-07-13 22:45 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-13 22:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 93413.

*** This bug has been marked as a duplicate of bug 93413 ***

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

end of thread, other threads:[~2022-07-13 22:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-04 16:15 [Bug c++/105846] New: constexpr unique_ptr doesn't work with virtual types benni at stuerz dot xyz
2022-06-04 17:07 ` [Bug c++/105846] " benni at stuerz dot xyz
2022-06-04 21:36 ` pinskia at gcc dot gnu.org
2022-06-04 21:39 ` [Bug c++/105846] [C++2b] " pinskia at gcc dot gnu.org
2022-06-04 21:40 ` pinskia at gcc dot gnu.org
2022-06-06 12:57 ` redi at gcc dot gnu.org
2022-07-13 22:45 ` pinskia 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).