diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 14e937d81cb82cb1e3f4a533ac5cd60ac6929b2e..dfa6fb406751bb21e2240dd9c1f7e79589915501 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -8596,6 +8596,13 @@ push_namespace (tree name, bool make_inline)
 	      /* This should find the slot created by pushdecl.  */
 	      gcc_checking_assert (slot && *slot == ns);
 	    }
+	  else
+	    {
+	      /* pushdecl could have expanded the hash table, so
+		 slot might be invalid.  */
+	      slot = find_namespace_slot (current_namespace, name);
+	      gcc_checking_assert (slot);
+	    }
 	  make_namespace_finish (ns, slot);
 
 	  /* Add the anon using-directive here, we don't do it in