diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 12c8a95e2e7..a56523b04a5 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -4641,7 +4641,7 @@ tsubst_friend_class (friend_tmpl, args) Here, in the scope of (say) S, `S' is bound to a TYPE_DECL for `S', not the TEMPLATE_DECL. */ - if (!DECL_CLASS_TEMPLATE_P (tmpl)) + if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) { tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); tmpl = maybe_get_template_decl_from_type_decl (tmpl); diff --git a/gcc/testsuite/g++.old-deja/g++.pt/friend42.C b/gcc/testsuite/g++.old-deja/g++.pt/friend42.C new file mode 100644 index 00000000000..dcbfd462bac --- /dev/null +++ b/gcc/testsuite/g++.old-deja/g++.pt/friend42.C @@ -0,0 +1,10 @@ +// Build don't link: +// Origin: Mark Mitchell + +template +struct S { + template + friend struct S2; +}; + +template struct S;