public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marek Polacek <mpolacek@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-6400] c++: can't eval PTRMEM_CST in incomplete class [PR107574]
Date: Wed,  1 Mar 2023 23:33:15 +0000 (GMT)	[thread overview]
Message-ID: <20230301233315.D49363858C39@sourceware.org> (raw)

https://gcc.gnu.org/g:de81e06273c613d7e06cbe2c8d9e72826c638056

commit r13-6400-gde81e06273c613d7e06cbe2c8d9e72826c638056
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Feb 2 18:15:37 2023 -0500

    c++: can't eval PTRMEM_CST in incomplete class [PR107574]
    
    Here we're attempting to evaluate a PTRMEM_CST in a class that hasn't
    been completed yet, but that doesn't work:
    
            /* We can't lower this until the class is complete.  */
            if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
              return cst;
    
    and then this unlowered PTRMEM_CST is used as EXPR in
    
        tree op1 = build_nop (ptrdiff_type_node, expr);
    
    and we crash in a subsequent cp_fold_convert which gets type=ptrdiff_type_node,
    expr=PTRMEM_CST and does
    
      else if (TREE_CODE (expr) == PTRMEM_CST
               && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
                               PTRMEM_CST_CLASS (expr)))
    
    where TYPE_PTRMEM_CLASS_TYPE (type) is going to crash since the type
    is ptrdiff_type_node.  We could just add a TYPE_PTRMEM_P check before
    accessing TYPE_PTRMEM_CLASS_TYPE but I think it's nicer to explain why
    we couldn't evaluate the expression.
    
            PR c++/107574
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (cxx_eval_constant_expression): Emit an error when
            a PTRMEM_CST cannot be evaluated.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/ptrmem-cst1.C: New test.

Diff:
---
 gcc/cp/constexpr.cc                      | 13 ++++++++++++-
 gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C | 11 +++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index bcae1cbd973..0770cdcd768 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7662,7 +7662,18 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
 	  }
 
 	if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
-	  op = cplus_expand_constant (op);
+	  {
+	    op = cplus_expand_constant (op);
+	    if (TREE_CODE (op) == PTRMEM_CST)
+	      {
+		if (!ctx->quiet)
+		  error_at (loc, "%qE is not a constant expression when the "
+			    "class %qT is still incomplete", op,
+			    PTRMEM_CST_CLASS (op));
+		*non_constant_p = true;
+		return t;
+	      }
+	  }
 
 	if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
 	  {
diff --git a/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C b/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C
new file mode 100644
index 00000000000..0d6a6b6445d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C
@@ -0,0 +1,11 @@
+// PR c++/107574
+// { dg-do compile { target c++11 } }
+
+struct A { int i; };
+struct B:A { int j; };
+struct C:B {
+  int k;
+  static_assert((int B::*) &C::k, ""); // { dg-error "non-constant|still incomplete" }
+};
+
+static_assert((int B::*) &C::k, "");

                 reply	other threads:[~2023-03-01 23:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230301233315.D49363858C39@sourceware.org \
    --to=mpolacek@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).