https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123354

Peter Damianov <peter0x44 at disroot dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |peter0x44 at disroot dot org

--- Comment #3 from Peter Damianov <peter0x44 at disroot dot org> ---
>From what I can tell, maybe_push_to_top_level() calls push_function_context()
This calls:
  set_cfun (NULL);
which in this case:
  set_function_decl (new_cfun, new_cfun ? new_cfun->decl : NULL_TREE, force);

so current_function_decl is NULL_TREE when enclosing_instantiation_of gets
called, which causes the loop to do nothing and hit the gcc_unreachable() at
the end of that function.

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 759418c344e..37150e11e76 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -15520,7 +15520,7 @@ enclosing_instantiation_of (tree tctx)
   for (; fn; fn = decl_function_context (fn))
     if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
       return fn;
-  gcc_unreachable ();
+  return NULL_TREE;
 }

 /* Substitute the ARGS into the T, which is a _DECL.  Return the
@@ -15938,6 +15938,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t
complain,
            if (TREE_STATIC (t))
              {
                tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
+               if (fn == NULL_TREE)
+                 fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
                if (fn != current_function_decl)
                  ctx = fn;
              }


This patch solves this particular ICE but I feel like it's a hack and this
issue could turn up in other contexts. For sure looks like some tricky code to
get right.

Reply via email to