diff mbox series

[2/2] c++: TYPENAME_TYPE lookup ignoring non-types [PR107773]

Message ID 20230213172340.849204-2-ppalka@redhat.com
State New
Headers show
Series [1/2] c++: factor out TYPENAME_TYPE substitution | expand

Commit Message

Patrick Palka Feb. 13, 2023, 5:23 p.m. UTC
[N.B. this is a corrected version of
https://gcc.gnu.org/pipermail/gcc-patches/2022-November/607443.html ]

Currently when resolving a TYPENAME_TYPE for 'typename T::m' via
make_typename_type, we consider only type bindings of 'm' and ignore
non-type ones.  But [temp.res.general]/3 says, in a note, "the usual
qualified name lookup ([basic.lookup.qual]) applies even in the presence
of 'typename'", and qualified name lookup doesn't discriminate between
type and non-type bindings.  So when resolving such a TYPENAME_TYPE
we want the lookup to consider all bindings.

An exception is when we have a TYPENAME_TYPE corresponding to the
qualifying scope appearing before the :: scope resolution operator, such
as 'T::type' in 'typename T::type::m'.  In that case, [basic.lookup.qual]/1
applies, and lookup for such a TYPENAME_TYPE must ignore non-type
bindings.  So in order to correctly handle all cases, make_typename_type
needs an additional flag controlling whether lookup should ignore
non-types or not.

To that end this patch adds a type_only flag to make_typename_type and
defaults it to false (do not ignore non-types).  In contexts where we do
want to ignore non-types (when substituting into the scope of a
TYPENAME_TYPE, SCOPE_REF, USING_DECL) we call tsubst_typename_type
directly with type_only=true.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

	PR c++/107773

gcc/cp/ChangeLog:

	* cp-tree.h (make_typename_type): Add another boolean parameter
	that defaults to false.
	* decl.cc (make_typename_type): Use lookup_member instead of
	lookup_field.  Pass want_type=type_only instead of =false to
	lookup_member.  Generalize format specifier in diagnostic to
	handle both type and non-type bindings.
	* pt.cc (tsubst_typename_type): Add another boolean parameter
	that defaults to false and pass it to make_typename_type.  If
	TYPE_CONTEXT is a TYPENAME_TYPE recurse with type_only=true
	instead of substituting it via tsubst.
	(tsubst_decl) <case USING_DECL>: If the scpoe is a TYPENAME_TYPE
	call tsubst_typename_type directly with type_only=true instead
	of substituting it via tsubst.
	(tsubst_qualified_id): Likewise.
	* search.cc (lookup_member): Document default argument.

gcc/testsuite/ChangeLog:

	* g++.dg/template/typename24.C: New test.
	* g++.dg/template/typename25.C: New test.
	* g++.dg/template/typename26.C: New test.
---
 gcc/cp/cp-tree.h                           |  2 +-
 gcc/cp/decl.cc                             | 14 ++++-----
 gcc/cp/pt.cc                               | 24 +++++++++++----
 gcc/cp/search.cc                           |  2 +-
 gcc/testsuite/g++.dg/template/typename24.C | 18 ++++++++++++
 gcc/testsuite/g++.dg/template/typename25.C | 34 ++++++++++++++++++++++
 gcc/testsuite/g++.dg/template/typename26.C | 20 +++++++++++++
 7 files changed, 100 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/typename24.C
 create mode 100644 gcc/testsuite/g++.dg/template/typename25.C
 create mode 100644 gcc/testsuite/g++.dg/template/typename26.C

Comments

Jason Merrill Feb. 14, 2023, 10:16 p.m. UTC | #1
On 2/13/23 09:23, Patrick Palka wrote:
> [N.B. this is a corrected version of
> https://gcc.gnu.org/pipermail/gcc-patches/2022-November/607443.html ]
> 
> Currently when resolving a TYPENAME_TYPE for 'typename T::m' via
> make_typename_type, we consider only type bindings of 'm' and ignore
> non-type ones.  But [temp.res.general]/3 says, in a note, "the usual
> qualified name lookup ([basic.lookup.qual]) applies even in the presence
> of 'typename'", and qualified name lookup doesn't discriminate between
> type and non-type bindings.  So when resolving such a TYPENAME_TYPE
> we want the lookup to consider all bindings.
> 
> An exception is when we have a TYPENAME_TYPE corresponding to the
> qualifying scope appearing before the :: scope resolution operator, such
> as 'T::type' in 'typename T::type::m'.  In that case, [basic.lookup.qual]/1
> applies, and lookup for such a TYPENAME_TYPE must ignore non-type
> bindings.  So in order to correctly handle all cases, make_typename_type
> needs an additional flag controlling whether lookup should ignore
> non-types or not.
> 
> To that end this patch adds a type_only flag to make_typename_type and
> defaults it to false (do not ignore non-types).  In contexts where we do
> want to ignore non-types (when substituting into the scope of a
> TYPENAME_TYPE, SCOPE_REF, USING_DECL) we call tsubst_typename_type
> directly with type_only=true.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
> 
> 	PR c++/107773
> 
> gcc/cp/ChangeLog:
> 
> 	* cp-tree.h (make_typename_type): Add another boolean parameter
> 	that defaults to false.
> 	* decl.cc (make_typename_type): Use lookup_member instead of
> 	lookup_field.  Pass want_type=type_only instead of =false to
> 	lookup_member.  Generalize format specifier in diagnostic to
> 	handle both type and non-type bindings.
> 	* pt.cc (tsubst_typename_type): Add another boolean parameter
> 	that defaults to false and pass it to make_typename_type.  If
> 	TYPE_CONTEXT is a TYPENAME_TYPE recurse with type_only=true
> 	instead of substituting it via tsubst.
> 	(tsubst_decl) <case USING_DECL>: If the scpoe is a TYPENAME_TYPE
> 	call tsubst_typename_type directly with type_only=true instead
> 	of substituting it via tsubst.
> 	(tsubst_qualified_id): Likewise.
> 	* search.cc (lookup_member): Document default argument.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/typename24.C: New test.
> 	* g++.dg/template/typename25.C: New test.
> 	* g++.dg/template/typename26.C: New test.
> ---
>   gcc/cp/cp-tree.h                           |  2 +-
>   gcc/cp/decl.cc                             | 14 ++++-----
>   gcc/cp/pt.cc                               | 24 +++++++++++----
>   gcc/cp/search.cc                           |  2 +-
>   gcc/testsuite/g++.dg/template/typename24.C | 18 ++++++++++++
>   gcc/testsuite/g++.dg/template/typename25.C | 34 ++++++++++++++++++++++
>   gcc/testsuite/g++.dg/template/typename26.C | 20 +++++++++++++
>   7 files changed, 100 insertions(+), 14 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/template/typename24.C
>   create mode 100644 gcc/testsuite/g++.dg/template/typename25.C
>   create mode 100644 gcc/testsuite/g++.dg/template/typename26.C
> 
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index a7c5765fc33..1241dbf8037 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -6845,7 +6845,7 @@ extern tree define_label			(location_t, tree);
>   extern void check_goto				(tree);
>   extern bool check_omp_return			(void);
>   extern tree make_typename_type			(tree, tree, enum tag_types, tsubst_flags_t,
> -						 bool = false, bool = false);
> +						 bool = false, bool = false, bool = false);
>   extern tree build_typename_type			(tree, tree, tree, tag_types);
>   extern tree make_unbound_class_template		(tree, tree, tree, tsubst_flags_t);
>   extern tree make_unbound_class_template_raw	(tree, tree, tree);
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index 430533606b0..c741dc23d99 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -4232,13 +4232,14 @@ build_typename_type (tree context, tree name, tree fullname,
>      return that, rather than the _TYPE it corresponds to, in other
>      cases we look through the type decl.  If TEMPLATE_OK is true and
>      we found a TEMPLATE_DECL then we return a CTAD placeholder for the
> -   TEMPLATE_DECL.  If TF_ERROR is set, complain about errors, otherwise
> -   be quiet.  */
> +   TEMPLATE_DECL.  If TYPE_ONLY is true, lookup of NAME in CONTEXT
> +   ignores non-type bindings.  If TF_ERROR is set, complain about errors,
> +   otherwise be quiet.  */
>   
>   tree
>   make_typename_type (tree context, tree name, enum tag_types tag_type,
>   		    tsubst_flags_t complain, bool keep_type_decl /* = false */,
> -		    bool template_ok /* = false */)
> +		    bool template_ok /* = false */, bool type_only /* = false */)
>   {
>     tree fullname;
>     tree t;
> @@ -4308,9 +4309,8 @@ make_typename_type (tree context, tree name, enum tag_types tag_type,
>        member of the current instantiation or a non-dependent base;
>        lookup will stop when we hit a dependent base.  */
>     if (!dependent_scope_p (context))
> -    /* We should only set WANT_TYPE when we're a nested typename type.
> -       Then we can give better diagnostics if we find a non-type.  */
> -    t = lookup_field (context, name, 2, /*want_type=*/true);
> +    t = lookup_member (context, name, /*protect=*/2,
> +		       /*want_type=*/type_only, complain);
>     else
>       t = NULL_TREE;
>   
> @@ -4362,7 +4362,7 @@ make_typename_type (tree context, tree name, enum tag_types tag_type,
>         else
>   	{
>   	  if (complain & tf_error)
> -	    error ("%<typename %T::%D%> names %q#T, which is not a type",
> +	    error ("%<typename %T::%D%> names %q#D, which is not a type",
>   		   context, name, t);
>   	  return error_mark_node;
>   	}
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index bc47bf15d38..27fc0b93484 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -13949,12 +13949,12 @@ tsubst_aggr_type_1 (tree t,
>       return t;
>   }
>   
> -/* Substitute ARGS into the TYPENAME_TYPE T.  The flag TEMPLATE_OK
> -   is passed to make_typename_type.  */
> +/* Substitute ARGS into the TYPENAME_TYPE T.  The flags TEMPLATE_OK and
> +   TYPE_ONLY are passed to make_typename_type.  */
>   
>   static tree
>   tsubst_typename_type (tree t, tree args, tsubst_flags_t complain, tree in_decl,
> -		      bool template_ok = false)
> +		      bool template_ok = false, bool type_only = false)
>   {
>     tree ctx = TYPE_CONTEXT (t);
>     if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
> @@ -13972,6 +13972,10 @@ tsubst_typename_type (tree t, tree args, tsubst_flags_t complain, tree in_decl,
>   	}
>         ctx = TREE_VEC_ELT (ctx, 0);
>       }
> +  else if (TREE_CODE (ctx) == TYPENAME_TYPE
> +	   && !typedef_variant_p (ctx))
> +    ctx = tsubst_typename_type (ctx, args, complain, in_decl,
> +				/*template_ok=*/false, /*type_only=*/true);
>     else
>       ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
>   			    /*entering_scope=*/1);
> @@ -14004,7 +14008,7 @@ tsubst_typename_type (tree t, tree args, tsubst_flags_t complain, tree in_decl,
>       }
>   
>     f = make_typename_type (ctx, f, typename_type, complain,
> -			  /*keep_type_decl=*/true, template_ok);
> +			  /*keep_type_decl=*/true, template_ok, type_only);
>     if (f == error_mark_node)
>       return f;
>     if (TREE_CODE (f) == TYPE_DECL)
> @@ -15094,6 +15098,11 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
>   	      scope = tsubst_pack_expansion (scope, args, complain, in_decl);
>   	      variadic_p = true;
>   	    }
> +	  else if (TREE_CODE (scope) == TYPENAME_TYPE
> +		   && !typedef_variant_p (scope))
> +	    scope = tsubst_typename_type (scope, args, complain, in_decl,
> +					  /*template_ok=*/false,
> +					  /*type_only=*/true);
>   	  else
>   	    scope = tsubst_copy (scope, args, complain, in_decl);
>   
> @@ -16885,7 +16894,12 @@ tsubst_qualified_id (tree qualified_id, tree args,
>     scope = TREE_OPERAND (qualified_id, 0);
>     if (args)
>       {
> -      scope = tsubst (scope, args, complain, in_decl);
> +      if (TREE_CODE (scope) == TYPENAME_TYPE
> +	  && !typedef_variant_p (scope))
> +	scope = tsubst_typename_type (scope, args, complain, in_decl,
> +				      /*template_ok=*/false, /*type_only=*/true);
> +      else
> +	scope = tsubst (scope, args, complain, in_decl);

Maybe we want a tsubst_scope function?

>         expr = tsubst_copy (name, args, complain, in_decl);
>       }
>     else
> diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc
> index f3f19cafec6..e472a97679d 100644
> --- a/gcc/cp/search.cc
> +++ b/gcc/cp/search.cc
> @@ -1109,7 +1109,7 @@ build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
>   
>   tree
>   lookup_member (tree xbasetype, tree name, int protect, bool want_type,
> -	       tsubst_flags_t complain, access_failure_info *afi)
> +	       tsubst_flags_t complain, access_failure_info *afi /* = NULL */)
>   {
>     tree rval, rval_binfo = NULL_TREE;
>     tree type = NULL_TREE, basetype_path = NULL_TREE;
> diff --git a/gcc/testsuite/g++.dg/template/typename24.C b/gcc/testsuite/g++.dg/template/typename24.C
> new file mode 100644
> index 00000000000..8b2b3718442
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/typename24.C
> @@ -0,0 +1,18 @@
> +// PR c++/107773
> +// Verify lookup for a non-neste TYPENAME_TYPE correctly considers
> +// non-types.
> +
> +struct a {
> +  typedef void get;
> +};
> +
> +struct b : a {
> +  int get(int i) const;
> +};
> +
> +template<class T>
> +void f() {
> +  typedef typename T::get type; // { dg-error "'int b::get\\(int\\) const', which is not a type" }
> +}
> +
> +template void f<b>();
> diff --git a/gcc/testsuite/g++.dg/template/typename25.C b/gcc/testsuite/g++.dg/template/typename25.C
> new file mode 100644
> index 00000000000..924330ee8d4
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/typename25.C
> @@ -0,0 +1,34 @@
> +// PR c++/107773
> +// Verify lookup for TYPENAME_TYPE appearing to the left of the scope (::)
> +// operator in various contexts correctly ignores non-types.
> +
> +struct a {
> +  typedef void type;
> +};
> +
> +struct c {
> +  struct b : a {
> +    typedef b self;
> +    static int m;
> +  };
> +  int b;
> +};
> +
> +template<class T>
> +void f() {
> +  // T::b::type is a TYPENAME_TYPE whose TYPE_CONTEXT is a nested
> +  // TYPENAME_TYPE.
> +  typedef typename T::b::type type;
> +  // T::b::m is a SCOPE_REF whose first operand is a TYPENAME_TYPE.
> +  int m = T::b::m;
> +}
> +
> +template void f<c>();
> +
> +template<class T>
> +struct d : T::b::self {
> +  // The using is a USING_DECL whose USING_DECL_SCOPE is a TYPENAME_TYPE.
> +  using typename T::b::type;
> +};
> +
> +template struct d<c>;
> diff --git a/gcc/testsuite/g++.dg/template/typename26.C b/gcc/testsuite/g++.dg/template/typename26.C
> new file mode 100644
> index 00000000000..4e6b764a97b
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/typename26.C
> @@ -0,0 +1,20 @@
> +// Example 4 from [temp.res.general]/3.
> +
> +struct A {
> +  struct X { };
> +  int X;
> +};
> +struct B {
> +  struct X { };
> +};
> +template<class T> void f(T t) {
> +  typename T::X x; // { dg-error "'int A::X', which is not a type" }
> +}
> +void foo() {
> +  A a;
> +  B b;
> +  f(b); // OK, T::X refers to B::X
> +  // { dg-bogus "" "" { target *-*-* } .-1 }
> +  f(a); // error: T::X refers to the data member A::X not the struct A::X
> +  // { dg-message "required from here" "" { target *-*-* } .-1 }
> +}
diff mbox series

Patch

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index a7c5765fc33..1241dbf8037 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6845,7 +6845,7 @@  extern tree define_label			(location_t, tree);
 extern void check_goto				(tree);
 extern bool check_omp_return			(void);
 extern tree make_typename_type			(tree, tree, enum tag_types, tsubst_flags_t,
-						 bool = false, bool = false);
+						 bool = false, bool = false, bool = false);
 extern tree build_typename_type			(tree, tree, tree, tag_types);
 extern tree make_unbound_class_template		(tree, tree, tree, tsubst_flags_t);
 extern tree make_unbound_class_template_raw	(tree, tree, tree);
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 430533606b0..c741dc23d99 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -4232,13 +4232,14 @@  build_typename_type (tree context, tree name, tree fullname,
    return that, rather than the _TYPE it corresponds to, in other
    cases we look through the type decl.  If TEMPLATE_OK is true and
    we found a TEMPLATE_DECL then we return a CTAD placeholder for the
-   TEMPLATE_DECL.  If TF_ERROR is set, complain about errors, otherwise
-   be quiet.  */
+   TEMPLATE_DECL.  If TYPE_ONLY is true, lookup of NAME in CONTEXT
+   ignores non-type bindings.  If TF_ERROR is set, complain about errors,
+   otherwise be quiet.  */
 
 tree
 make_typename_type (tree context, tree name, enum tag_types tag_type,
 		    tsubst_flags_t complain, bool keep_type_decl /* = false */,
-		    bool template_ok /* = false */)
+		    bool template_ok /* = false */, bool type_only /* = false */)
 {
   tree fullname;
   tree t;
@@ -4308,9 +4309,8 @@  make_typename_type (tree context, tree name, enum tag_types tag_type,
      member of the current instantiation or a non-dependent base;
      lookup will stop when we hit a dependent base.  */
   if (!dependent_scope_p (context))
-    /* We should only set WANT_TYPE when we're a nested typename type.
-       Then we can give better diagnostics if we find a non-type.  */
-    t = lookup_field (context, name, 2, /*want_type=*/true);
+    t = lookup_member (context, name, /*protect=*/2,
+		       /*want_type=*/type_only, complain);
   else
     t = NULL_TREE;
 
@@ -4362,7 +4362,7 @@  make_typename_type (tree context, tree name, enum tag_types tag_type,
       else
 	{
 	  if (complain & tf_error)
-	    error ("%<typename %T::%D%> names %q#T, which is not a type",
+	    error ("%<typename %T::%D%> names %q#D, which is not a type",
 		   context, name, t);
 	  return error_mark_node;
 	}
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index bc47bf15d38..27fc0b93484 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -13949,12 +13949,12 @@  tsubst_aggr_type_1 (tree t,
     return t;
 }
 
-/* Substitute ARGS into the TYPENAME_TYPE T.  The flag TEMPLATE_OK
-   is passed to make_typename_type.  */
+/* Substitute ARGS into the TYPENAME_TYPE T.  The flags TEMPLATE_OK and
+   TYPE_ONLY are passed to make_typename_type.  */
 
 static tree
 tsubst_typename_type (tree t, tree args, tsubst_flags_t complain, tree in_decl,
-		      bool template_ok = false)
+		      bool template_ok = false, bool type_only = false)
 {
   tree ctx = TYPE_CONTEXT (t);
   if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
@@ -13972,6 +13972,10 @@  tsubst_typename_type (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 	}
       ctx = TREE_VEC_ELT (ctx, 0);
     }
+  else if (TREE_CODE (ctx) == TYPENAME_TYPE
+	   && !typedef_variant_p (ctx))
+    ctx = tsubst_typename_type (ctx, args, complain, in_decl,
+				/*template_ok=*/false, /*type_only=*/true);
   else
     ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
 			    /*entering_scope=*/1);
@@ -14004,7 +14008,7 @@  tsubst_typename_type (tree t, tree args, tsubst_flags_t complain, tree in_decl,
     }
 
   f = make_typename_type (ctx, f, typename_type, complain,
-			  /*keep_type_decl=*/true, template_ok);
+			  /*keep_type_decl=*/true, template_ok, type_only);
   if (f == error_mark_node)
     return f;
   if (TREE_CODE (f) == TYPE_DECL)
@@ -15094,6 +15098,11 @@  tsubst_decl (tree t, tree args, tsubst_flags_t complain)
 	      scope = tsubst_pack_expansion (scope, args, complain, in_decl);
 	      variadic_p = true;
 	    }
+	  else if (TREE_CODE (scope) == TYPENAME_TYPE
+		   && !typedef_variant_p (scope))
+	    scope = tsubst_typename_type (scope, args, complain, in_decl,
+					  /*template_ok=*/false,
+					  /*type_only=*/true);
 	  else
 	    scope = tsubst_copy (scope, args, complain, in_decl);
 
@@ -16885,7 +16894,12 @@  tsubst_qualified_id (tree qualified_id, tree args,
   scope = TREE_OPERAND (qualified_id, 0);
   if (args)
     {
-      scope = tsubst (scope, args, complain, in_decl);
+      if (TREE_CODE (scope) == TYPENAME_TYPE
+	  && !typedef_variant_p (scope))
+	scope = tsubst_typename_type (scope, args, complain, in_decl,
+				      /*template_ok=*/false, /*type_only=*/true);
+      else
+	scope = tsubst (scope, args, complain, in_decl);
       expr = tsubst_copy (name, args, complain, in_decl);
     }
   else
diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc
index f3f19cafec6..e472a97679d 100644
--- a/gcc/cp/search.cc
+++ b/gcc/cp/search.cc
@@ -1109,7 +1109,7 @@  build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
 
 tree
 lookup_member (tree xbasetype, tree name, int protect, bool want_type,
-	       tsubst_flags_t complain, access_failure_info *afi)
+	       tsubst_flags_t complain, access_failure_info *afi /* = NULL */)
 {
   tree rval, rval_binfo = NULL_TREE;
   tree type = NULL_TREE, basetype_path = NULL_TREE;
diff --git a/gcc/testsuite/g++.dg/template/typename24.C b/gcc/testsuite/g++.dg/template/typename24.C
new file mode 100644
index 00000000000..8b2b3718442
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/typename24.C
@@ -0,0 +1,18 @@ 
+// PR c++/107773
+// Verify lookup for a non-neste TYPENAME_TYPE correctly considers
+// non-types.
+
+struct a {
+  typedef void get;
+};
+
+struct b : a {
+  int get(int i) const;
+};
+
+template<class T>
+void f() {
+  typedef typename T::get type; // { dg-error "'int b::get\\(int\\) const', which is not a type" }
+}
+
+template void f<b>();
diff --git a/gcc/testsuite/g++.dg/template/typename25.C b/gcc/testsuite/g++.dg/template/typename25.C
new file mode 100644
index 00000000000..924330ee8d4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/typename25.C
@@ -0,0 +1,34 @@ 
+// PR c++/107773
+// Verify lookup for TYPENAME_TYPE appearing to the left of the scope (::)
+// operator in various contexts correctly ignores non-types.
+
+struct a {
+  typedef void type;
+};
+
+struct c {
+  struct b : a {
+    typedef b self;
+    static int m;
+  };
+  int b;
+};
+
+template<class T>
+void f() {
+  // T::b::type is a TYPENAME_TYPE whose TYPE_CONTEXT is a nested
+  // TYPENAME_TYPE.
+  typedef typename T::b::type type;
+  // T::b::m is a SCOPE_REF whose first operand is a TYPENAME_TYPE.
+  int m = T::b::m;
+}
+
+template void f<c>();
+
+template<class T>
+struct d : T::b::self {
+  // The using is a USING_DECL whose USING_DECL_SCOPE is a TYPENAME_TYPE.
+  using typename T::b::type;
+};
+
+template struct d<c>;
diff --git a/gcc/testsuite/g++.dg/template/typename26.C b/gcc/testsuite/g++.dg/template/typename26.C
new file mode 100644
index 00000000000..4e6b764a97b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/typename26.C
@@ -0,0 +1,20 @@ 
+// Example 4 from [temp.res.general]/3.
+
+struct A {
+  struct X { };
+  int X;
+};
+struct B {
+  struct X { };
+};
+template<class T> void f(T t) {
+  typename T::X x; // { dg-error "'int A::X', which is not a type" }
+}
+void foo() {
+  A a;
+  B b;
+  f(b); // OK, T::X refers to B::X
+  // { dg-bogus "" "" { target *-*-* } .-1 }
+  f(a); // error: T::X refers to the data member A::X not the struct A::X
+  // { dg-message "required from here" "" { target *-*-* } .-1 }
+}