c++: spec_hasher and TYPENAME_TYPE resolution [PR95223]

After enabling sanitization of the specialization tables, we are
triggering one of the hash table sanity checks in the below testcase.

The reason is that when looking up the specialization j<int> in the
type_specializations table, the sanity check finds that the existing
entry j<n<t>::m> compares equal to j<int> but hashes differently.

The discrepancy is due to structural_comptypes looking through
TYPENAME_TYPEs (via resolve_typename_type), something which
iterative_hash_template_arg doesn't do.  So the TYPENAME_TYPE n<t>::m is
considered equal to int, but the hashes of these two template arguments
are different.

It seems wrong for the result of a specialization table lookup to depend
on the current scope, so this patch makes structural_comptypes avoid
calling resolve_typename_type when comparing_specializations.

In order for the below testcase to deterministically trigger the
sanitization error without this patch, we also need to fix the location
of the call to hash_table::verify within hash_table::find_with_hash.

gcc/ChangeLog:

	PR c++/95223
	* hash-table.h (hash_table::find_with_hash): Move up the call to
	hash_table::verify.

gcc/cp/ChangeLog:

	PR c++/95223
	* typeck.c (structural_comptypes): Don't perform
	context-dependent resolution of TYPENAME_TYPEs when
	comparing_specializations.

gcc/testsuite/ChangeLog:

	PR c++/95223
	* g++.dg/template/typename23.C: New test.
This commit is contained in:
Patrick Palka 2020-05-20 09:15:48 -04:00
parent 053dc901e0
commit 610ae2dbbf
6 changed files with 44 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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

View File

@ -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(); }