From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 7B4CF3858C5F for ; Fri, 3 Feb 2023 00:28:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B4CF3858C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675384127; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=VmDnVtN2e9jWYVU1S9Q/6b3NYFq62B0K83XbMLgxuC0=; b=Gs0+liya1i3SU7ujk+l0L80z/UNZx1v5tRiaT2Cs24sxKbgjLfTcqLdUjYLYI2LIX+g1B0 DPb33oL67/2AJLdEo02a2jdrBZ8kaNyTZEHtFzV0kiz7jGN4eyYW1ke/QxhNyQtufBPrdd NkK0eKUSNnLYw9RNi7R2Zz+JzoE8zFI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-479-6itbGQZqP_CooKMKRiMxWg-1; Thu, 02 Feb 2023 19:28:35 -0500 X-MC-Unique: 6itbGQZqP_CooKMKRiMxWg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BD19380006E for ; Fri, 3 Feb 2023 00:28:34 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.9.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E351C15BA0; Fri, 3 Feb 2023 00:28:34 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: can't eval PTRMEM_CST in incomplete class [PR107574] Date: Thu, 2 Feb 2023 19:28:25 -0500 Message-Id: <20230203002825.398939-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? 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. --- gcc/cp/constexpr.cc | 9 +++++++++ gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C | 11 +++++++++++ 2 files changed, 20 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp0x/ptrmem-cst1.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 5b31f9c27d1..2c03988b097 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7691,6 +7691,15 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) && !can_convert_qual (type, op)) op = cplus_expand_constant (op); + if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) + { + 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; + } return cp_fold_convert (type, op); } 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, ""); base-commit: 07c87fce63541846ca2951e22dac04fcaa66475f -- 2.39.1