Index: gcc/testsuite/g++.dg/template/using21.C
===================================================================
--- gcc/testsuite/g++.dg/template/using21.C	(revision 0)
+++ gcc/testsuite/g++.dg/template/using21.C	(revision 0)
@@ -0,0 +1,28 @@
+// PR c++/52126
+// { dg-do compile }
+
+template<typename T>
+struct A
+{
+    int foo;
+
+    struct B : A<T>
+    {
+        using A::foo;
+    };
+
+    struct C : A
+    {
+        using A::foo;
+    };
+
+    struct D : A<T>
+    {
+	using A<T>::foo;
+    };
+
+    struct E : A
+    {
+	using A<T>::foo;
+    };
+};
Index: gcc/testsuite/g++.dg/template/using22.C
===================================================================
--- gcc/testsuite/g++.dg/template/using22.C	(revision 0)
+++ gcc/testsuite/g++.dg/template/using22.C	(revision 0)
@@ -0,0 +1,33 @@
+// PR c++/52126
+// { dg-do compile }
+
+template <class T> struct Z {};
+
+template<typename T>
+struct A
+{
+    struct B : A<T>
+    {
+        using A::nonexist; // { dg-error "no members matching" }
+    };
+
+    struct C : A
+    {
+        using A::nonexist; // { dg-error "no members matching" }
+    };
+
+    struct D : A<T>
+    {
+    	using A<T>::nonexist; // { dg-error "no members matching" }
+    };
+
+    struct E : A
+    {
+    	using A<T>::nonexist; // { dg-error "no members matching" }
+    };
+
+    struct F : Z<T>
+    {
+	using Z<T>::nonexist;
+    };
+};
Index: gcc/cp/search.c
===================================================================
--- gcc/cp/search.c	(revision 184313)
+++ gcc/cp/search.c	(working copy)
@@ -1,7 +1,7 @@
 /* Breadth-first and depth-first routines for
    searching multiple-inheritance lattice for GNU C++.
    Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
+   1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
    Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
@@ -98,7 +98,8 @@ dfs_lookup_base (tree binfo, void *data_
 {
   struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
 
-  if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
+  tree binfo_type = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
+  if (SAME_BINFO_TYPE_P (binfo_type, data->base))
     {
       if (!data->binfo)
 	{
@@ -1019,7 +1020,7 @@ static tree
 lookup_field_r (tree binfo, void *data)
 {
   struct lookup_field_info *lfi = (struct lookup_field_info *) data;
-  tree type = BINFO_TYPE (binfo);
+  tree type = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
   tree nval = NULL_TREE;
 
   /* If this is a dependent base, don't look in it.  */
Index: gcc/cp/name-lookup.c
===================================================================
--- gcc/cp/name-lookup.c	(revision 184313)
+++ gcc/cp/name-lookup.c	(working copy)
@@ -1,5 +1,5 @@
 /* Definitions for C++ name lookup routines.
-   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
    Free Software Foundation, Inc.
    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
 
@@ -3259,6 +3259,14 @@ do_class_using_decl (tree scope, tree na
 	}
       else if (!name_dependent_p)
 	{
+	  /* If the using-declaration refers to a dependent base which
+	     is an enclosing class at the same time, the lookup can be
+	     performed.  So that it succeed, we need to remove the
+	     flag BINFO_DEPENDENT_BASE_P.  */
+	  if (BINFO_DEPENDENT_BASE_P (binfo)
+	      && dependent_type_p (BINFO_TYPE (binfo)))
+	    BINFO_DEPENDENT_BASE_P (binfo) = 0;
+
 	  decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
 	  if (!decl)
 	    {
