I wasn't properly setting LOOKUP_CONSTINIT in grokfield and so we didn't detect a non-const initializer.
Bootstrapped/regtested on x86_64-linux, ok for trunk? 2019-10-24 Marek Polacek <pola...@redhat.com> PR c++/92134 - constinit malfunction in static data member. * decl2.c (grokfield): Set LOOKUP_CONSTINIT. * g++.dg/cpp2a/constinit14.C: New test. diff --git gcc/cp/decl2.c gcc/cp/decl2.c index 6d5e973b487..a630ee31397 100644 --- gcc/cp/decl2.c +++ 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: diff --git gcc/testsuite/g++.dg/cpp2a/constinit14.C gcc/testsuite/g++.dg/cpp2a/constinit14.C new file mode 100644 index 00000000000..72bfab667b8 --- /dev/null +++ gcc/testsuite/g++.dg/cpp2a/constinit14.C @@ -0,0 +1,13 @@ +// PR c++/92134 - constinit malfunction in static data member. +// { dg-do compile { target c++2a } } + +struct Value { + Value() : v{new int{42}} {} + int* v; +}; + +struct S { + static constinit inline Value v{}; // { dg-error "variable .S::v. does not have a constant initializer|call to non-.constexpr. function" } +}; + +int main() { return *S::v.v; }