Bug 102623 - Failure to detect destructed scalar objects in consteval function
Summary: Failure to detect destructed scalar objects in consteval function
Status: RESOLVED DUPLICATE of bug 71093
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2021-10-05 19:41 UTC by Gabriel Ravier
Modified: 2024-08-06 07:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 14.1.0
Known to fail:
Last reconfirmed: 2022-01-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gabriel Ravier 2021-10-05 19:41:02 UTC
using T = int;
consteval bool f()
{
  T t = 42;
  t.~T();
  return (t == 42);
}

bool x = f();

This code should not compile, as `f` invokes undefined behavior during constant evaluation, as Clang diagnoses:

<source>:9:10: error: call to consteval function 'f' is not a constant expression
bool x = f();
         ^
<source>:6:11: note: read of object outside its lifetime is not allowed in a constant expression
  return (t == 42);
          ^

The specific usage with `int` here was not UB until C++20 (although pseudo-destructors weren't allowed in constant expressions until C++20 anyway), but as of http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0593r6.html#pseudo-destructor-calls pseudo-destructors end the lifetime of the operand.
Comment 1 Drea Pinski 2021-10-05 19:55:48 UTC
I suspect this is basically PR 71093.
Comment 2 Drea Pinski 2022-01-17 10:35:27 UTC
Confirmed. I thought I saw another issue related to this one where the variable goes out of scope instead.
Comment 3 Gabriel Ravier 2024-07-13 21:50:25 UTC
Seems fixed on trunk ?
Comment 4 Drea Pinski 2024-07-13 22:34:02 UTC
(In reply to Gabriel Ravier from comment #3)
> Seems fixed on trunk ?

It seems to have been fixed for GCC 14 even.
Comment 5 Drea Pinski 2024-08-06 07:49:43 UTC
Dup.

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