Merge branch 'swift-2.2-branch' into branch
diff --git a/include/swift/AST/ProtocolConformance.h b/include/swift/AST/ProtocolConformance.h
index 0d1d726..c635424 100644
--- a/include/swift/AST/ProtocolConformance.h
+++ b/include/swift/AST/ProtocolConformance.h
@@ -606,7 +606,11 @@
   /// Get the declaration context that contains the conforming extension or
   /// nominal type declaration.
   DeclContext *getDeclContext() const {
-    return getType()->getClassOrBoundGenericClass();
+    auto bgc = getType()->getClassOrBoundGenericClass();
+
+    // In some cases, we may not have a BGC handy, in which case we should
+    // delegate to the inherited conformance for the decl context.
+    return bgc ? bgc : InheritedConformance->getDeclContext();
   }
 
   /// Retrieve the state of this conformance.
diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp
index 4017692..76fc105 100644
--- a/lib/IRGen/IRGenSIL.cpp
+++ b/lib/IRGen/IRGenSIL.cpp
@@ -4316,6 +4316,10 @@
       return emitAllocateBuffer(*this, i->getLoweredConcreteType(), buffer);
     }
   }();
+
+  // Make sure we have a value of the right type.
+  address =
+    Builder.CreateBitCast(address, srcTI.getStorageType()->getPointerTo());
   
   setLoweredAddress(SILValue(i, 0), address);
 }
diff --git a/stdlib/public/runtime/SwiftObject.mm b/stdlib/public/runtime/SwiftObject.mm
index d31f0fc..380f846 100644
--- a/stdlib/public/runtime/SwiftObject.mm
+++ b/stdlib/public/runtime/SwiftObject.mm
@@ -77,9 +77,12 @@
 #if SWIFT_OBJC_INTEROP
 struct SwiftObject_s {
   void *isa  __attribute__((unavailable));
-  long refCount  __attribute__((unavailable));
+  uint32_t strongRefCount  __attribute__((unavailable));
+  uint32_t weakRefCount  __attribute__((unavailable));
 };
 
+static_assert(sizeof(SwiftObject_s) == sizeof(HeapObject),
+              "SwiftObject and HeapObject must have the same header");
 static_assert(std::is_trivially_constructible<SwiftObject_s>::value,
               "SwiftObject must be trivially constructible");
 static_assert(std::is_trivially_destructible<SwiftObject_s>::value,
@@ -89,13 +92,7 @@
 __attribute__((objc_root_class))
 #endif
 @interface SwiftObject<NSObject> {
-  // FIXME: rdar://problem/18950072 Clang emits ObjC++ classes as having
-  // non-trivial structors if they contain any struct fields at all, regardless of
-  // whether they in fact have nontrivial default constructors. Dupe the body
-  // of SwiftObject_s into here as a workaround because we don't want to pay
-  // the cost of .cxx_destruct method dispatch at deallocation time.
-  void *magic_isa  __attribute__((unavailable));
-  long magic_refCount  __attribute__((unavailable));
+  SwiftObject_s header;
 }
 
 - (BOOL)isEqual:(id)object;
diff --git a/test/IRGen/existentials.sil b/test/IRGen/existentials.sil
index 834a9fd..bcda438 100644
--- a/test/IRGen/existentials.sil
+++ b/test/IRGen/existentials.sil
@@ -111,3 +111,33 @@
 
   return undef : $()
 }
+
+typealias Any = protocol<>
+
+struct Foo<T> {
+  private let x : T -> ()
+  private let y : T
+  private let z : Any
+}
+
+// rdar://25870973
+//   swift-2.2 specific test that field projections of concrete but
+//   non-fixed-size types don't crash south of an init_existential_addr.
+sil @bar : $@convention(thin) <T> (@out Foo<T>) -> ()
+sil @foo : $@convention(thin) <T> (@out Any, @in Foo<T>) -> () {
+bb0(%0 : $*Any, %1 : $*Foo<T>):
+  %2 = alloc_stack $Any
+
+  %3 = init_existential_addr %2 : $*Any, $Foo<T>
+  %fn = function_ref @bar : $@convention(thin) <T> (@out Foo<T>) -> ()
+  %result = apply %fn<T>(%3) : $@convention(thin) <T> (@out Foo<T>) -> ()
+
+  %4 = init_existential_addr %0 : $*Any, $Foo<T>
+  copy_addr %3 to [initialization] %4 : $*Foo<T>
+
+  destroy_addr %2 : $*Any
+
+  dealloc_stack %2 : $*Any
+  %ret = tuple ()
+  return %ret : $()
+}
diff --git a/test/NameBinding/InheritedConformance.swift b/test/NameBinding/InheritedConformance.swift
new file mode 100644
index 0000000..a5f960a
--- /dev/null
+++ b/test/NameBinding/InheritedConformance.swift
@@ -0,0 +1,25 @@
+// XFAIL: linux
+// RUN: rm -rf %t && mkdir %t
+// RUN: cp %s %t/main.swift
+// RUN: %target-swift-frontend -parse -primary-file %t/main.swift -emit-reference-dependencies-path - > %t.swiftdeps
+
+// SR-1267, SR-1270
+import Foundation
+
+class TypeType<T where T: NSString> {
+    func call(notification: NSNotification?) {
+        let set = NSOrderedSet()
+        if let objects = set.array as? [T] {
+            let _ = (notification?.userInfo?["great_key"] as? NSSet).flatMap { updatedObjects in
+                return updatedObjects.filter({ element in
+                    guard let element = element as? T
+                        where objects.indexOf(element) != nil
+                    else {
+                        return false
+                    }
+                    return true
+                })
+            } ?? []
+        }
+    }
+}
\ No newline at end of file