Index: lib/AST/Decl.cpp
===================================================================
--- lib/AST/Decl.cpp	(revision 202762)
+++ lib/AST/Decl.cpp	(working copy)
@@ -1216,70 +1216,69 @@
 
   // C++ [basic.link]p6:
   //   Names not covered by these rules have no linkage.
   return LinkageInfo::none();
 }
 
 namespace clang {
 class LinkageComputer {
 public:
   static LinkageInfo getLVForDecl(const NamedDecl *D,
                                   LVComputationKind computation) {
     if (computation == LVForLinkageOnly && D->hasCachedLinkage())
       return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false);
 
     LinkageInfo LV = computeLVForDecl(D, computation);
     if (D->hasCachedLinkage())
       assert(D->getCachedLinkage() == LV.getLinkage());
 
     D->setCachedLinkage(LV.getLinkage());
 
 #ifndef NDEBUG
     // In C (because of gnu inline) and in c++ with microsoft extensions an
     // static can follow an extern, so we can have two decls with different
     // linkages.
     const LangOptions &Opts = D->getASTContext().getLangOpts();
     if (!Opts.CPlusPlus || Opts.MicrosoftExt)
       return LV;
 
     // We have just computed the linkage for this decl. By induction we know
     // that all other computed linkages match, check that the one we just
-    // computed
-    // also does.
+    // computed also does.
     NamedDecl *Old = NULL;
     for (NamedDecl::redecl_iterator I = D->redecls_begin(),
                                     E = D->redecls_end();
          I != E; ++I) {
       NamedDecl *T = cast<NamedDecl>(*I);
       if (T == D)
         continue;
-      if (T->hasCachedLinkage()) {
+      if (!T->isInvalidDecl() && T->hasCachedLinkage()) {
         Old = T;
         break;
       }
     }
     assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage());
 #endif
 
     return LV;
   }
 };
 }
 
 static LinkageInfo getLVForDecl(const NamedDecl *D,
                                 LVComputationKind computation) {
   return clang::LinkageComputer::getLVForDecl(D, computation);
 }
 
 std::string NamedDecl::getQualifiedNameAsString() const {
   std::string QualName;
   llvm::raw_string_ostream OS(QualName);
   printQualifiedName(OS, getASTContext().getPrintingPolicy());
   return OS.str();
 }
 
 void NamedDecl::printQualifiedName(raw_ostream &OS) const {
   printQualifiedName(OS, getASTContext().getPrintingPolicy());
 }
 
 void NamedDecl::printQualifiedName(raw_ostream &OS,
                                    const PrintingPolicy &P) const {
Index: test/SemaCXX/linkage2.cpp
===================================================================
--- test/SemaCXX/linkage2.cpp	(revision 202762)
+++ test/SemaCXX/linkage2.cpp	(working copy)
@@ -186,30 +186,34 @@
     g();
   }
 }
 
 namespace test18 {
   template <typename T> struct foo {
     template <T *P> static void f() {}
     static void *g() { return (void *)f<&x>; }
     static T x;
   };
   template <typename T> T foo<T>::x;
   inline void *f() {
     struct S {
     };
     return foo<S>::g();
   }
   void *h() { return f(); }
 }
 
 extern "C" void pr16247_foo(int);
 static void pr16247_foo(double);
 void pr16247_foo(int) {}
 void pr16247_foo(double) {}
 
 namespace PR16247 {
   extern "C" void pr16247_bar(int);
   static void pr16247_bar(double);
   void pr16247_bar(int) {}
   void pr16247_bar(double) {}
 }
+namespace PR18964 {
+  unsigned &*foo; // expected-error{{'foo' declared as a pointer to a reference of type}}
+  extern struct {} *foo;
+}
