build_m_component_ref checks if either datum/component it got are erroneous but
they can be turned into the error_mark_node by mark_use as in this case: datum
is "a" before the call to mark_lvalue_use, but that emits an error and returns
the error_mark_node, which then crashes.

We can just move the checks after calling mark_[lr]value_use; those just
return when they get the error_mark_node.  But I tweaked mark_use to also
handle the case when the type is erroneous.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-09-23  Marek Polacek  <pola...@redhat.com>

        PR c++/91845 - ICE with invalid pointer-to-member.
        * expr.c (mark_use): Use error_operand_p.
        * typeck2.c (build_m_component_ref): Check error_operand_p after
        calling mark_[lr]value_use.

        * g++.dg/cpp1y/pr91845.C: New test.

diff --git gcc/cp/expr.c gcc/cp/expr.c
index 212a7f93c5a..d488912d5db 100644
--- gcc/cp/expr.c
+++ gcc/cp/expr.c
@@ -96,7 +96,7 @@ mark_use (tree expr, bool rvalue_p, bool read_p,
 {
 #define RECUR(t) mark_use ((t), rvalue_p, read_p, loc, reject_builtin)
 
-  if (expr == NULL_TREE || expr == error_mark_node)
+  if (expr == NULL_TREE || error_operand_p (expr))
     return expr;
 
   if (reject_builtin && reject_gcc_builtin (expr, loc))
diff --git gcc/cp/typeck2.c gcc/cp/typeck2.c
index d5098fa24bb..58fa54f40af 100644
--- gcc/cp/typeck2.c
+++ gcc/cp/typeck2.c
@@ -2068,12 +2068,12 @@ build_m_component_ref (tree datum, tree component, 
tsubst_flags_t complain)
   tree binfo;
   tree ctype;
 
-  if (error_operand_p (datum) || error_operand_p (component))
-    return error_mark_node;
-
   datum = mark_lvalue_use (datum);
   component = mark_rvalue_use (component);
 
+  if (error_operand_p (datum) || error_operand_p (component))
+    return error_mark_node;
+
   ptrmem_type = TREE_TYPE (component);
   if (!TYPE_PTRMEM_P (ptrmem_type))
     {
diff --git gcc/testsuite/g++.dg/cpp1y/pr91845.C 
gcc/testsuite/g++.dg/cpp1y/pr91845.C
new file mode 100644
index 00000000000..cb80dd7a8a7
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp1y/pr91845.C
@@ -0,0 +1,14 @@
+// PR c++/91845 - ICE with invalid pointer-to-member.
+// { dg-do compile { target c++14 } }
+
+void non_const_mem_ptr() {
+  struct A {
+  };
+  constexpr A a = {1, 2}; // { dg-error "too many initializers" }
+  struct B {
+    int A::*p;
+    constexpr int g() const {
+      return a.*p; // { dg-error "use of local variable" }
+    };
+  };
+}

Reply via email to