Message ID | 20230203002825.398939-1-polacek@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 020DC3858C74 for <patchwork@sourceware.org>; Fri, 3 Feb 2023 00:29:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 020DC3858C74 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675384167; bh=VmDnVtN2e9jWYVU1S9Q/6b3NYFq62B0K83XbMLgxuC0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ZkP+eQrN17mL/n+fg/HPEoOh+csSUy3jzFQuzlKgNybR6W0pQ3+pjxYoZj27f67Vg +k7SS4aZv9j4oNSMYxo3VtEBsttjHqXqSXA7O9P0MhXp18yyLUp421hkhd69F6pZ28 FazENuebM1FYEMii5DyAfYGme0JMEMVQS53ccskw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 <gcc-patches@gcc.gnu.org>; Fri, 3 Feb 2023 00:28:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B4CF3858C5F 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 <gcc-patches@gcc.gnu.org>; 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) To: Jason Merrill <jason@redhat.com>, GCC Patches <gcc-patches@gcc.gnu.org> 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Marek Polacek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Marek Polacek <polacek@redhat.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
c++: can't eval PTRMEM_CST in incomplete class [PR107574]
|
|
Commit Message
Marek Polacek
Feb. 3, 2023, 12:28 a.m. UTC
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 base-commit: 07c87fce63541846ca2951e22dac04fcaa66475f
Comments
Ping. On Thu, Feb 02, 2023 at 07:28:25PM -0500, Marek Polacek via Gcc-patches wrote: > 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 > Marek
On 2/2/23 19:28, Marek Polacek wrote: > 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; > + } Hmm, maybe handle this a few lines higher, in this existing if: > if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) > op = cplus_expand_constant (op); ? OK with that change.
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, "");