https://gcc.gnu.org/g:3058891d5c6f114d86a9a8c87fd52f202decff53

commit r16-8880-g3058891d5c6f114d86a9a8c87fd52f202decff53
Author: Marek Polacek <[email protected]>
Date:   Tue Apr 21 15:54:33 2026 -0400

    c++/reflection: odr-used inline var of consteval-only type [PR124770]
    
    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 <[email protected]>
    (cherry picked from commit 6d035aa99c3d1197167e085302d8ce015b950b35)

Diff:
---
 gcc/cp/decl.cc                        |  4 ++++
 gcc/testsuite/g++.dg/reflect/init18.C | 14 ++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 679a8007ca6f..9924c0fc1725 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -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
index 000000000000..f13125be66e8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/init18.C
@@ -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" }

Reply via email to