c++/reflection: wrong lookup of template in spliced ns [PR127349]

In

  template<class> struct Y {};
  namespace N { }

  template<auto ns>
  void f () {
    typename [:ns:]::template Y<int> b;   // #1
  }
  void g () { f<^^N>(); }

no error is given for #1.  The problem is that we do lookup_template_class
first which does:

      push_decl_namespace (context);
      templ = lookup_name (d1, LOOK_where::NAMESPACE, LOOK_want::NORMAL);
      pop_decl_namespace ();

which is an unqualified lookup that goes out through the enclosing
namespaces.  We should do a qualified lookup in N only.  This patch
makes us use lookup_qualified_name to find the TEMPLATE_DECL and then
pass that to lookup_template_class with a null context.

We already have a call to lookup_qualified_name so I just shuffled
the code a bit.  Also guard the qualified_name_lookup_error call
with a complain check.  And give an error if we didn't get a type
for the cp_build_qualified_type call.

Note that the UNBOUND_CLASS_TEMPLATE case will also need something
similar.

The diagnostic for t9 is currently technically imprecise because
fn is a function template; we should say that it isn't a type template
instead.

	PR c++/127349

gcc/cp/ChangeLog:

	* pt.cc (lookup_template_class): Print %qE if d1 is not a type.
	(tsubst) <case TYPENAME_TYPE>: Do lookup_qualified_name first
	before lookup_template_class.  Guard qualified_name_lookup_error
	with a complain check.  Use gcc_checking_assert instead of
	gcc_assert.  Use identifier_p.  Give an error if we didn't get
	a type.

gcc/testsuite/ChangeLog:

	* g++.dg/reflect/ns7.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
2 files changed