Bug 113041 - misleading diagnostic for variable of non-literal type in constexpr function in C++20 mode
Summary: misleading diagnostic for variable of non-literal type in constexpr function ...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2023-12-16 03:03 UTC by Patrick Palka
Modified: 2023-12-16 21:21 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-12-16 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Patrick Palka 2023-12-16 03:03:51 UTC
Here f<int>() is not constexpr in C++20 (but is in C++23 after P2242R3) due to the local variable a of non-literal type A, so the testcase is invalid:

struct A { ~A(); };

void g();

template<class T>
constexpr int f() {
  if (__builtin_is_constant_evaluated())
    return 42;
  g();
  A a;
}

constexpr int n = f<int>();

But our diagnostic incorrectly blames the call to the non-constexpr g() for f<int>() being non-constexpr, rather than the variable a:

constexpr-nonliteral.C:13:25: error: ‘constexpr int f() [with T = int]’ called in a constant expression
   13 | constexpr int n = f<int>();
      |                   ~~~~~~^~
constexpr-nonliteral.C:6:15: note: ‘constexpr int f() [with T = int]’ is not usable as a ‘constexpr’ function because:
    6 | constexpr int f() {
      |               ^
constexpr-nonliteral.C:9:4: error: call to non-‘constexpr’ function ‘void g()’
    9 |   g();
      |   ~^~
constexpr-nonliteral.C:3:6: note: ‘void g()’ declared here
    3 | void g();
      |      ^
Comment 1 Drea Pinski 2023-12-16 21:21:39 UTC
Confirmed. there might be others like this too.