diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h
index 52e09e6..ede31e2 100644
--- a/include/clang/AST/Decl.h
+++ b/include/clang/AST/Decl.h
@@ -252,15 +252,17 @@ public:
       setLinkage(minLinkage(linkage(), L));
     }
     void mergeLinkage(LinkageInfo Other) {
-      setLinkage(minLinkage(linkage(), Other.linkage()));
+      mergeLinkage(Other.linkage());
     }
 
-    void mergeVisibility(Visibility V) {
-      setVisibility(minVisibility(visibility(), V));
-    }
     void mergeVisibility(Visibility V, bool E) {
+      if (visibilityExplicit())
+        return;
       setVisibility(minVisibility(visibility(), V), visibilityExplicit() || E);
     }
+    void mergeVisibility(Visibility V) {
+      mergeVisibility(V, visibilityExplicit());
+    }
     void mergeVisibility(LinkageInfo Other) {
       mergeVisibility(Other.visibility(), Other.visibilityExplicit());
     }
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index b4a4eb1..30669b1 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -340,8 +340,7 @@ static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
       LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
       if (TypeLV.first != ExternalLinkage)
         return LinkageInfo::uniqueExternal();
-      if (!LV.visibilityExplicit())
-        LV.mergeVisibility(TypeLV.second);
+      LV.mergeVisibility(TypeLV.second);
     }
 
     if (Var->getStorageClass() == SC_PrivateExtern)
@@ -602,12 +601,9 @@ static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
     LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
     if (TypeLV.first != ExternalLinkage)
       LV.mergeLinkage(UniqueExternalLinkage);
-    if (!LV.visibilityExplicit())
-      LV.mergeVisibility(TypeLV.second);
+    LV.mergeVisibility(TypeLV.second);
   }
 
-  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
-
   // Apply -fvisibility if desired.
   if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
     LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
diff --git a/test/CodeGenCXX/visibility.cpp b/test/CodeGenCXX/visibility.cpp
index ce1e8fd..b614e2e 100644
--- a/test/CodeGenCXX/visibility.cpp
+++ b/test/CodeGenCXX/visibility.cpp
@@ -325,10 +325,10 @@ namespace Test18 {
   // CHECK: declare void @_ZN6Test181AIiE1B3barEv()
   // CHECK: declare hidden void @_ZN6Test181AIiE1B3bazEv()
   // CHECK: declare hidden void @_ZN6Test181AINS_1HEE3fooEv()
-  // CHECK: declare hidden void @_ZN6Test181AINS_1HEE3barEv()
+  // CHECK: declare void @_ZN6Test181AINS_1HEE3barEv()
   // CHECK: declare hidden void @_ZN6Test181AINS_1HEE3bazEv()
-  // CHECK: declare hidden void @_ZN6Test181AINS_1HEE1B3fooEv()
-  // CHECK: declare hidden void @_ZN6Test181AINS_1HEE1B3barEv()
+  // CHECK: declare void @_ZN6Test181AINS_1HEE1B3fooEv()
+  // CHECK: declare void @_ZN6Test181AINS_1HEE1B3barEv()
   // CHECK: declare hidden void @_ZN6Test181AINS_1HEE1B3bazEv()
   // CHECK-HIDDEN: declare hidden void @_ZN6Test181AIiE3fooEv()
   // CHECK-HIDDEN: declare void @_ZN6Test181AIiE3barEv()
@@ -337,10 +337,10 @@ namespace Test18 {
   // CHECK-HIDDEN: declare void @_ZN6Test181AIiE1B3barEv()
   // CHECK-HIDDEN: declare hidden void @_ZN6Test181AIiE1B3bazEv()
   // CHECK-HIDDEN: declare hidden void @_ZN6Test181AINS_1HEE3fooEv()
-  // CHECK-HIDDEN: declare hidden void @_ZN6Test181AINS_1HEE3barEv()
+  // CHECK-HIDDEN: declare void @_ZN6Test181AINS_1HEE3barEv()
   // CHECK-HIDDEN: declare hidden void @_ZN6Test181AINS_1HEE3bazEv()
-  // CHECK-HIDDEN: declare hidden void @_ZN6Test181AINS_1HEE1B3fooEv()
-  // CHECK-HIDDEN: declare hidden void @_ZN6Test181AINS_1HEE1B3barEv()
+  // CHECK-HIDDEN: declare void @_ZN6Test181AINS_1HEE1B3fooEv()
+  // CHECK-HIDDEN: declare void @_ZN6Test181AINS_1HEE1B3barEv()
   // CHECK-HIDDEN: declare hidden void @_ZN6Test181AINS_1HEE1B3bazEv()
 }
 
@@ -456,7 +456,7 @@ namespace test22 {
 }
 
 namespace PR10113 {
-  namespace foo __attribute__ ((__visibility__ ("default"))) {
+  namespace foo DEFAULT {
     template<typename T>
       class bar {
       void zed() {}
@@ -465,4 +465,10 @@ namespace PR10113 {
   template class foo::bar<char>;
   // CHECK: define weak_odr void @_ZN7PR101133foo3barIcE3zedEv
   // CHECK-HIDDEN: define weak_odr void @_ZN7PR101133foo3barIcE3zedEv
+
+  struct zed {
+  };
+  template class foo::bar<zed>;
+  // CHECK: define weak_odr void @_ZN7PR101133foo3barINS_3zedEE3zedEv
+  // CHECK-HIDDEN: define weak_odr void @_ZN7PR101133foo3barINS_3zedEE3zedEv
 }
