diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 2fe9f0dfdf58e6180497373c043084cba7f597d4..cef2e7bfd031670d7b1b306b6f7fe7b55efdfe0b 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2020-05-20  Patrick Palka  <ppalka@redhat.com>
+
+	PR c++/95223
+	* hash-table.h (hash_table::find_with_hash): Move up the call to
+	hash_table::verify.
+
 2020-05-20  Martin Liska  <mliska@suse.cz>
 
 	* lto-compress.c (lto_compression_zstd): Fill up
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index a81a6200ff59acd223a2e11c82adf886a3809e5c..c59137faa3f16800767dd4a447ea3d681aaa68f5 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,10 @@
+2020-05-20  Patrick Palka  <ppalka@redhat.com>
+
+	PR c++/95223
+	* typeck.c (structural_comptypes): Don't perform
+	context-dependent resolution of TYPENAME_TYPEs when
+	comparing_specializations.
+
 2020-05-19  Nathan Sidwell  <nathan@acm.org>
 
 	* pt.c (lookup_template_class_1): Do not reinit template_info of an
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index d2e6c90762206644ea5f996c3a028450c131151c..0181984bb99578cd1c9c9f24e2f8e2da8707277a 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -1256,13 +1256,16 @@ structural_comptypes (tree t1, tree t2, int strict)
 
   gcc_assert (TYPE_P (t1) && TYPE_P (t2));
 
-  /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
-     current instantiation.  */
-  if (TREE_CODE (t1) == TYPENAME_TYPE)
-    t1 = resolve_typename_type (t1, /*only_current_p=*/true);
+  if (!comparing_specializations)
+    {
+      /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
+	 current instantiation.  */
+      if (TREE_CODE (t1) == TYPENAME_TYPE)
+	t1 = resolve_typename_type (t1, /*only_current_p=*/true);
 
-  if (TREE_CODE (t2) == TYPENAME_TYPE)
-    t2 = resolve_typename_type (t2, /*only_current_p=*/true);
+      if (TREE_CODE (t2) == TYPENAME_TYPE)
+	t2 = resolve_typename_type (t2, /*only_current_p=*/true);
+    }
 
   if (TYPE_PTRMEMFUNC_P (t1))
     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
diff --git a/gcc/hash-table.h b/gcc/hash-table.h
index a1423c781125fedef803a4083771cd5c5b081f19..32f3a634e1ed7bef91741cdc7327b383516fa0db 100644
--- a/gcc/hash-table.h
+++ b/gcc/hash-table.h
@@ -912,6 +912,12 @@ hash_table<Descriptor, Lazy, Allocator>
 
   if (Lazy && m_entries == NULL)
     m_entries = alloc_entries (size);
+
+#if CHECKING_P
+  if (m_sanitize_eq_and_hash)
+    verify (comparable, hash);
+#endif
+
   value_type *entry = &m_entries[index];
   if (is_empty (*entry)
       || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
@@ -928,13 +934,7 @@ hash_table<Descriptor, Lazy, Allocator>
       entry = &m_entries[index];
       if (is_empty (*entry)
           || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
-	{
-#if CHECKING_P
-	  if (m_sanitize_eq_and_hash)
-	    verify (comparable, hash);
-#endif
-	  return *entry;
-	}
+	return *entry;
     }
 }
 
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 49f8cd7cf9c9ede7184f3d5843f9d50ecd0b941c..3594d01cb93802e564eaf342344f591ba1f2d0ed 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2020-05-20  Patrick Palka  <ppalka@redhat.com>
+
+	PR c++/95223
+	* g++.dg/template/typename23.C: New test.
+
 2020-05-20  Srinath Parvathaneni  <srinath.parvathaneni@arm.com>
 
 	PR target/94959
diff --git a/gcc/testsuite/g++.dg/template/typename23.C b/gcc/testsuite/g++.dg/template/typename23.C
new file mode 100644
index 0000000000000000000000000000000000000000..d2fb0ca72f52d3069ff4bf4ce4caba7eaa1935f7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/typename23.C
@@ -0,0 +1,10 @@
+// PR c++/95223
+// { dg-do compile }
+// { dg-additional-options "--param=hash-table-verification-limit=10000" }
+
+template <typename> struct j {};
+template <typename t> struct n {
+  typedef int m;
+  j<n<t>::m> p();
+};
+template <typename o> j<typename n<o>::m> n<o>::p() { return o::f(); }