]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/reflection: odr-used inline var of consteval-only type [PR124770]
authorMarek Polacek <polacek@redhat.com>
Tue, 21 Apr 2026 19:54:33 +0000 (15:54 -0400)
committerMarek Polacek <polacek@redhat.com>
Thu, 7 May 2026 21:04:19 +0000 (17:04 -0400)
wrapup_namespace_globals gives errors for code like

  extern inline int i;
  int &r = i; // odr-used inline variable is not defined

but because we mark consteval-only vars DECL_EXTERNAL, we also wrongly
emit the error for:

  inline constexpr info value{};
  static_assert(value == info{});

where value clearly is defined.  This patch strenghtens the check to
also check !DECL_INITIAL.  We can't only check DECL_THIS_EXTERN because

  extern constexpr inline info v2{};

is OK.

PR c++/124770

gcc/cp/ChangeLog:

* decl.cc (wrapup_namespace_globals): Give the odr-used
inline variable error only when !DECL_INITIAL.

gcc/testsuite/ChangeLog:

* g++.dg/reflect/init18.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/decl.cc
gcc/testsuite/g++.dg/reflect/init18.C [new file with mode: 0644]

index f8e9223e43e9628bcbe8916795a43e9fe1b896fe..21a57d01deeb7abe50f013b2295242fa63ea9b26 100644 (file)
@@ -1023,6 +1023,10 @@ wrapup_namespace_globals ()
 
          if (VAR_P (decl)
              && DECL_EXTERNAL (decl)
+             /* We mark consteval-only variables DECL_EXTERNAL, but
+                 extern constexpr inline std::meta::info i{};
+                is a definition (the extern is redundant).  */
+             && !DECL_INITIAL (decl)
              && DECL_INLINE_VAR_P (decl)
              && DECL_ODR_USED (decl))
            error_at (DECL_SOURCE_LOCATION (decl),
diff --git a/gcc/testsuite/g++.dg/reflect/init18.C b/gcc/testsuite/g++.dg/reflect/init18.C
new file mode 100644 (file)
index 0000000..f13125b
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/124770
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+using info = decltype(^^::);
+
+constexpr inline info v{};
+static_assert(v == info{});
+extern constexpr inline info v2{};
+static_assert(v2 == info{});
+constexpr inline info v3;
+static_assert(v3 == info{});
+
+extern constexpr inline info v4; // { dg-error "not a definition|consteval-only" }