Bug 92134 - static constinit members incorrectly compile
Summary: static constinit members incorrectly compile
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2019-10-17 08:13 UTC by Vincent Hamp
Modified: 2019-10-30 18:51 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-10-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Hamp 2019-10-17 08:13:01 UTC
The new C++20 constinit specifier currently doesn't seem to work with static data members.

The following snippet compiles although the constructor of Value calls new.

struct Value {
  Value() : v{new int{42}} {}
  int* v;
};

struct S {
  static constinit inline Value v{};
};

int main() { return *S::v.v; }
Comment 1 Marek Polacek 2019-10-24 22:08:22 UTC
Untested fix:

--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -990,6 +990,9 @@ grokfield (const cp_declarator *declarator,
   else
     flags = LOOKUP_IMPLICIT;
 
+  if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
+    flags |= LOOKUP_CONSTINIT;
+
   switch (TREE_CODE (value))
     {
     case VAR_DECL:
Comment 2 Marek Polacek 2019-10-30 18:50:34 UTC
Author: mpolacek
Date: Wed Oct 30 18:49:59 2019
New Revision: 277636

URL: https://gcc.gnu.org/viewcvs?rev=277636&root=gcc&view=rev
Log:
	PR c++/92134 - constinit malfunction in static data member.

I wasn't properly setting LOOKUP_CONSTINIT in grokfield and so we didn't
detect a non-const initializer.

	* decl2.c (grokfield): Set LOOKUP_CONSTINIT.

	* g++.dg/cpp2a/constinit14.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp2a/constinit14.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl2.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Marek Polacek 2019-10-30 18:51:44 UTC
Fixed.