aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/constexpr.cc
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2023-02-02 18:15:37 -0500
committerMarek Polacek <polacek@redhat.com>2023-03-01 18:32:04 -0500
commitde81e06273c613d7e06cbe2c8d9e72826c638056 (patch)
treea859a5c0470b098821d412ff4301cc63af29800a /gcc/cp/constexpr.cc
parenta764d3df8bf289beef2c4305b504a51d5a0718a2 (diff)
downloadgcc-de81e06273c613d7e06cbe2c8d9e72826c638056.zip
gcc-de81e06273c613d7e06cbe2c8d9e72826c638056.tar.gz
gcc-de81e06273c613d7e06cbe2c8d9e72826c638056.tar.bz2
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.
Diffstat (limited to 'gcc/cp/constexpr.cc')
-rw-r--r--gcc/cp/constexpr.cc13
1 files changed, 12 insertions, 1 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index bcae1cb..0770cdc 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)
{