From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id BAEE63858D28; Sat, 4 Mar 2023 17:51:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BAEE63858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677952280; bh=WdI5i5qyTJLP5cY/VwfV204uDX5VmbzWP9pe8r4Baic=; h=From:To:Subject:Date:From; b=duBqWDS/JqUJ2PGEPvO93vKmpRZ+KwGIEhu0/WmzVe4q3x8laKog8Lo+OMsMupsLO DQkkZXmjXB4CuxypIK96wn1TVAGlWJxe7lEGubCACezQZDDyVIl6TvnsabFN/eNxCE uo9+UEHDadSGkHmpUUAolEmpwiKYtQSj2D5PxaKk= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9216] c++: can't eval PTRMEM_CST in incomplete class [PR107574] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 445082ff3cb7f9d30021adf338e9ab6038c3e412 X-Git-Newrev: b7b50918fe52206e188c8c60c60e0d6a516e8572 Message-Id: <20230304175120.BAEE63858D28@sourceware.org> Date: Sat, 4 Mar 2023 17:51:20 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b7b50918fe52206e188c8c60c60e0d6a516e8572 commit r12-9216-gb7b50918fe52206e188c8c60c60e0d6a516e8572 Author: Marek Polacek 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. (cherry picked from commit de81e06273c613d7e06cbe2c8d9e72826c638056) 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 6ac1b33d41d..468a58f95cc 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7310,7 +7310,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, "");