diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index 3dd0243..9adfdff 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -74,6 +74,21 @@ static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) {
   filter.done();
 }
 
+static bool AnyBaseDependant(CXXRecordDecl *R) {
+  CXXRecordDecl::base_class_iterator
+    I =R->bases_begin(), E = R->bases_end();
+  for (; I!=E; ++I)
+    if (I->getType().getTypePtr()->isDependentType())
+      return true;
+
+  I = R->vbases_begin(); E = R->vbases_end();
+  for (; I!=E; ++I)
+    if (I->getType().getTypePtr()->isDependentType())
+      return true;
+
+  return false;
+}
+
 TemplateNameKind Sema::isTemplateName(Scope *S,
                                       const CXXScopeSpec &SS,
                                       UnqualifiedId &Name,
@@ -4708,9 +4723,15 @@ Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II,
   unsigned DiagID = 0;
   Decl *Referenced = 0;
   switch (Result.getResultKind()) {
-  case LookupResult::NotFound:
+  case LookupResult::NotFound: {
+    if (CurrentInstantiation && AnyBaseDependant(CurrentInstantiation))
+      // We did not found type, but we find it might,
+      // at point of template instantiation in one of dependant bases.
+      return Context.getTypenameType(NNS, &II);
+
     DiagID = diag::err_typename_nested_not_found;
     break;
+  }
 
   case LookupResult::Found:
     if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
diff --git a/test/SemaTemplate/typename-specifier-5.cpp b/test/SemaTemplate/typename-specifier-5.cpp
new file mode 100644
index 0000000..266c0f3
--- /dev/null
+++ b/test/SemaTemplate/typename-specifier-5.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template <typename T>
+struct A {
+    typedef T type;
+};
+
+template <typename T>
+struct B : A<T> {
+    void f(typename B::type) { // expected-error{{no type named 'type'}}
+        typename B::type t = 0;
+    }
+};
+
+template <>
+struct A<void> {};
+
+B<int> b1;
+B<void> b2; // expected-note{{in instantiation of template class 'struct B<void>' requested here}}
