https://gcc.gnu.org/g:2d7a0d38e2f8e281ab2269cfe6c048410fa3c886

commit r16-357-g2d7a0d38e2f8e281ab2269cfe6c048410fa3c886
Author: Jason Merrill <ja...@redhat.com>
Date:   Fri May 2 08:35:38 2025 -0400

    c++: CTAD and constexpr ctor [PR115207]
    
    Here we failed to constant-evaluate the A<int> constructor because DECL_SIZE
    wasn't set on 'a' yet, so compare_address thinks we can't be sure it isn't
    at the same address as 'i'.
    
    Normally DECL_SIZE is set by build_decl calling layout_decl, but that
    doesn't happen here because we don't have a type yet.  So we need to
    layout_decl again after deduction.
    
            PR c++/115207
    
    gcc/cp/ChangeLog:
    
            * decl.cc (cp_finish_decl): Call layout_decl after CTAD.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1z/class-deduction118.C: New test.

Diff:
---
 gcc/cp/decl.cc                                  |  3 +++
 gcc/testsuite/g++.dg/cpp1z/class-deduction118.C | 11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 84398e5952a1..03e8c98d4b68 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -8899,6 +8899,9 @@ cp_finish_decl (tree decl, tree init, bool 
init_const_expr_p,
          TREE_TYPE (decl) = error_mark_node;
          return;
        }
+
+      /* Now that we have a type, try these again.  */
+      layout_decl (decl, 0);
       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
 
       /* Update the type of the corresponding TEMPLATE_DECL to match.  */
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction118.C 
b/gcc/testsuite/g++.dg/cpp1z/class-deduction118.C
new file mode 100644
index 000000000000..64d814beb7d6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction118.C
@@ -0,0 +1,11 @@
+// PR c++/115207
+// { dg-do compile { target c++17 } }
+
+template <class T>
+struct A {
+  T t;
+  constexpr A(T* p): t (p != &t) { }
+};
+
+const int i = 42;
+constexpr A a = &i;

Reply via email to