+2018-04-03  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/84768
+       * pt.c (rewrite_template_parm): If the first argument is
+       error_mark_node return it immediately.
+       (build_deduction_guide): Check the return value of the
+       latter for error_mark_node.
+       (do_class_deduction): Check the return value of the latter.
+
 2018-04-03  Jason Merrill  <jason@redhat.com>
 
        * semantics.c (finish_if_stmt_cond): Use
 
 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
                       tree tsubst_args, tsubst_flags_t complain)
 {
+  if (olddecl == error_mark_node)
+    return error_mark_node;
+
   tree oldidx = get_template_parm_index (olddecl);
 
   tree newtype;
   else
     {
       ++processing_template_decl;
+      bool ok = true;
 
       fn_tmpl
        = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
              tree olddecl = TREE_VALUE (oldelt);
              tree newdecl = rewrite_template_parm (olddecl, index, level,
                                                    tsubst_args, complain);
+             if (newdecl == error_mark_node)
+               ok = false;
              tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
                                                 tsubst_args, complain, ctor);
              tree list = build_tree_list (newdef, newdecl);
 
          current_template_parms = save_parms;
        }
+
       --processing_template_decl;
+      if (!ok)
+       return error_mark_node;
     }
 
   if (!memtmpl)
   for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
     {
       tree guide = build_deduction_guide (*iter, outer_args, complain);
+      if (guide == error_mark_node)
+       return error_mark_node;
       if ((flags & LOOKUP_ONLYCONVERTING)
          && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
        elided = true;
       if (gtype)
        {
          tree guide = build_deduction_guide (gtype, outer_args, complain);
+         if (guide == error_mark_node)
+           return error_mark_node;
          cands = lookup_add (guide, cands);
        }
     }