Bug 87497 - constexprs involving non-literal const variables are incorrectly accepted
Summary: constexprs involving non-literal const variables are incorrectly accepted
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.2.1
: P3 normal
Target Milestone: 13.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2018-10-03 11:32 UTC by Richard Sandiford
Modified: 2022-11-21 17:23 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 13.0
Known to fail: 12.2.0
Last reconfirmed: 2021-07-23 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Sandiford 2018-10-03 11:32:59 UTC
G++ accepts:

struct s { volatile int x; int y; };
constexpr int foo (const s &s1) { return s1.y; }
void g () { const s local_s = { 1, 2 }; constexpr int a = foo (local_s); }

even though "local_s" is only const, not constexpr (and couldn't be constexpr due to having a non-literal type).

From Jonathan on IRC:

  I think for foo(local_s) to be a constant expression, local_s needs to be a constexpr variable, or a non-volatile const-qualified integer, or a reference
  http://eel.is/c++draft/expr.const#8.7
Comment 1 Drea Pinski 2021-07-23 23:45:59 UTC
Confirmed.
Comment 2 Drea Pinski 2022-11-21 07:58:49 UTC
Seems to be fixed on the trunk.
I get now:
<source>: In function 'void g()':
<source>:3:63:   in 'constexpr' expansion of 'foo(local_s)'
<source>:3:71: error: the value of 'local_s' is not usable in a constant expression
    3 | void g () { const s local_s = { 1, 2 }; constexpr int a = foo (local_s); }
      |                                                                       ^
<source>:3:21: note: 'local_s' was not declared 'constexpr'
    3 | void g () { const s local_s = { 1, 2 }; constexpr int a = foo (local_s); }
      |                     ^~~~~~~
Comment 3 Jakub Jelinek 2022-11-21 09:17:42 UTC
r13-766-g6209009df65ff68482ef66951856f50cf362d990
Comment 4 Drea Pinski 2022-11-21 17:23:11 UTC
The referenced change explictly added the check for this so closing as fixed. It includes a testcase already too.