Merge pull request #20538 from benlangmuir/disable-DiagsAfterCloseAndReopen

[sourcekit] Disable DiagsAfterCloseAndReopen test (again)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6143abf..1187d4d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,20 @@
 Swift 5.0
 ---------
 
+* [SR-7139][]:
+
+  Exclusive memory access is now enforced at runtime by default in
+  optimized (`-O`/`-Osize`) builds. Programs that violate exclusivity will
+  trap at runtime with an "overlapping access" diagnostic
+  message. This can be disabled via a command line flag:
+  `-enforce-exclusivity=unchecked`, but doing so may result in undefined
+  behavior.
+
+  Runtime violations of exclusivity typically result from
+  simultaneous access of class properties, global variables (including
+  variables in top-level code), or variables captured by escaping
+  closures.
+
 * [SE-0216][]:
 
   The `@dynamicCallable` attribute enables nominal types to be "callable" via a
@@ -7290,4 +7304,5 @@
 [SR-2394]: <https://bugs.swift.org/browse/SR-2394>
 [SR-2608]: <https://bugs.swift.org/browse/SR-2608>
 [SR-4248]: <https://bugs.swift.org/browse/SR-4248>
+[SR-7139]: <https://bugs.swift.org/browse/SR-7139>
 [SR-7251]: <https://bugs.swift.org/browse/SR-7251>
diff --git a/docs/ABIStabilityManifesto.md b/docs/ABIStabilityManifesto.md
index 19d52a6..8b4c57f 100644
--- a/docs/ABIStabilityManifesto.md
+++ b/docs/ABIStabilityManifesto.md
@@ -213,7 +213,7 @@
 
 For all of the areas discussed above, more aggressive layout improvements may be invented in the post-ABI stability future. For example, we may want to explore rearranging and packing nested type data members with outer type data members. Such improvements would have to be done in an ABI-additive fashion through deployment target and/or min-version checking. This may mean that the module file will need to track per-type ABI versioning information.
 
-A potentially out of date description of Swift's current type layout can be found in the [Type Layout docs](https://github.com/apple/swift/blob/master/docs/ABI.rst#type-layout).
+A potentially out of date description of Swift's current type layout can be found in the [Type Layout docs](https://github.com/apple/swift/blob/master/docs/ABI/TypeLayout.rst).
 
 
 ## <a name="metadata"></a>Type Metadata
@@ -230,7 +230,7 @@
 
 Stabilizing the ABI means producing a precise technical specification for the fixed part of the metadata layout of all language constructs so that future compilers and tools can continue to read and write them. A prose description is not necessarily needed, though explanations are useful. We will also want to carve out extra space for areas where it is likely to be needed for future functionality [[SR-3731](https://bugs.swift.org/browse/SR-3731)].
 
-For more, but potentially out of date, details see the [Type Metadata docs](https://github.com/apple/swift/blob/master/docs/ABI.rst#type-metadata).
+For more, but potentially out of date, details see the [Type Metadata docs](https://github.com/apple/swift/blob/master/docs/ABI/TypeMetadata.rst).
 
 ### Generic Parameters
 
@@ -284,7 +284,7 @@
 
 Mangling is used to produce unique symbols. It applies to both external (public) symbols as well as internal or hidden symbols. Only the mangling scheme for external symbols is part of ABI.
 
-ABI stability means a stable mangling scheme, fully specified so that future compilers and tools can honor it. For a potentially out-of-date specification of what the mangling currently looks like, see the [Name Mangling docs](https://github.com/apple/swift/blob/master/docs/ABI.rst#mangling).
+ABI stability means a stable mangling scheme, fully specified so that future compilers and tools can honor it. For a potentially out-of-date specification of what the mangling currently looks like, see the [Name Mangling docs](https://github.com/apple/swift/blob/master/docs/ABI/Mangling.rst).
 
 There are some corner cases currently in the mangling scheme that should be fixed before declaring ABI stability. We need to come up with a canonicalization of generic and protocol requirements to allow for order-agnostic mangling [[SR-3733](https://bugs.swift.org/browse/SR-3733)]. We also may decide to more carefully mangle variadicity of function parameters, etc [[SR-3734](https://bugs.swift.org/browse/SR-3734)]. Most often, though, mangling improvements focus on reducing symbol size.
 
diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp
index 5b5a843..73c86d7 100644
--- a/lib/ClangImporter/ClangImporter.cpp
+++ b/lib/ClangImporter/ClangImporter.cpp
@@ -2023,7 +2023,7 @@
 }
 
 static bool isVisibleFromModule(const ClangModuleUnit *ModuleFilter,
-                                const ValueDecl *VD) {
+                                ValueDecl *VD) {
   assert(ModuleFilter);
 
   auto ContainingUnit = VD->getDeclContext()->getModuleScopeContext();
@@ -2038,24 +2038,34 @@
 
   auto ClangNode = VD->getClangNode();
   if (!ClangNode) {
-    // If we synthesized a ValueDecl, it won't have a Clang node. But so far
-    // all the situations where we synthesize top-level declarations are
-    // situations where we don't have to worry about C redeclarations.
-    // We should only consider the declaration visible from its owning module.
+    // If we synthesized a ValueDecl, it won't have a Clang node. Find the
+    // associated declaration that /does/ have a Clang node, and use that.
     auto *SynthesizedTypeAttr =
         VD->getAttrs().getAttribute<ClangImporterSynthesizedTypeAttr>();
     assert(SynthesizedTypeAttr);
 
-    // When adding new ClangImporterSynthesizedTypeAttr::Kinds, make sure that
-    // the above statement still holds: "we don't want to allow these
-    // declarations to be treated as present in multiple modules".
     switch (SynthesizedTypeAttr->getKind()) {
     case ClangImporterSynthesizedTypeAttr::Kind::NSErrorWrapper:
-    case ClangImporterSynthesizedTypeAttr::Kind::NSErrorWrapperAnon:
+    case ClangImporterSynthesizedTypeAttr::Kind::NSErrorWrapperAnon: {
+      ASTContext &Ctx = ContainingUnit->getASTContext();
+      auto LookupFlags =
+          NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions;
+      auto WrapperStruct = cast<StructDecl>(VD);
+      TinyPtrVector<ValueDecl *> LookupResults =
+          WrapperStruct->lookupDirect(Ctx.Id_Code, LookupFlags);
+      assert(!LookupResults.empty() && "imported error enum without Code");
+
+      auto CodeEnumIter = llvm::find_if(LookupResults,
+                                        [&](ValueDecl *Member) -> bool {
+        return Member->getDeclContext() == WrapperStruct;
+      });
+      assert(CodeEnumIter != LookupResults.end() &&
+             "could not find Code enum in wrapper struct");
+      assert((*CodeEnumIter)->hasClangNode());
+      ClangNode = (*CodeEnumIter)->getClangNode();
       break;
     }
-
-    return false;
+    }
   }
 
   // Macros can be "redeclared" by putting an equivalent definition in two
diff --git a/lib/Sema/CSDiag.cpp b/lib/Sema/CSDiag.cpp
index f454b3f..02f91af 100644
--- a/lib/Sema/CSDiag.cpp
+++ b/lib/Sema/CSDiag.cpp
@@ -6697,8 +6697,8 @@
                                            : defaultUnqualifiedLookupOptions),
                                corrections);
 
-      if (currentType)
-        if (isa<TupleType>(currentType.getPointer())) {
+      if (currentType) {
+        if (currentType->is<TupleType>()) {
           TC.diagnose(KPE->getLoc(), diag::expr_keypath_unimplemented_tuple);
           isInvalid = true;
           break;
@@ -6706,7 +6706,7 @@
         else
           TC.diagnose(componentNameLoc, diag::could_not_find_type_member,
                       currentType, componentName);
-      else
+      } else
         TC.diagnose(componentNameLoc, diag::use_unresolved_identifier,
                     componentName, false);
 
diff --git a/lib/Sema/TypeCheckAttr.cpp b/lib/Sema/TypeCheckAttr.cpp
index a67a6ae..d405046 100644
--- a/lib/Sema/TypeCheckAttr.cpp
+++ b/lib/Sema/TypeCheckAttr.cpp
@@ -2466,7 +2466,8 @@
       isa<AccessorDecl>(D))
     return;
 
-  if (!D->getAttrs().hasAttribute<DynamicAttr>()) {
+  if (!D->getAttrs().hasAttribute<DynamicAttr>() &&
+      !D->getAttrs().hasAttribute<DynamicReplacementAttr>()) {
     auto attr = new (D->getASTContext()) DynamicAttr(/*implicit=*/true);
     D->getAttrs().add(attr);
   }
diff --git a/test/ClangImporter/Inputs/custom-modules/RedeclaredErrorEnum/Redeclared.h b/test/ClangImporter/Inputs/custom-modules/RedeclaredErrorEnum/Redeclared.h
index 94f733a..ae66a21 100644
--- a/test/ClangImporter/Inputs/custom-modules/RedeclaredErrorEnum/Redeclared.h
+++ b/test/ClangImporter/Inputs/custom-modules/RedeclaredErrorEnum/Redeclared.h
@@ -1,7 +1,16 @@
 @import Foundation;
+#ifndef NO_IMPORT_BASE_FROM_REDECLARED
 @import Base;
+#endif
 
 extern NSString * const SomeErrorDomain;
 // typedef NS_ERROR_ENUM(SomeErrorDomain, SomeErrorCode);
 typedef enum SomeErrorCode : long SomeErrorCode;
-enum __attribute__((ns_error_domain(SomeErrorDomain))) SomeErrorCode : long;
\ No newline at end of file
+enum __attribute__((ns_error_domain(SomeErrorDomain))) SomeErrorCode : long
+#ifdef NO_IMPORT_BASE_FROM_REDECLARED
+{
+  SomeErrorX,
+  SomeErrorY
+}
+#endif
+;
\ No newline at end of file
diff --git a/test/ClangImporter/enum-error-redeclared.swift b/test/ClangImporter/enum-error-redeclared.swift
index 6833908..582a836 100644
--- a/test/ClangImporter/enum-error-redeclared.swift
+++ b/test/ClangImporter/enum-error-redeclared.swift
@@ -1,4 +1,10 @@
 // RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify -enable-objc-interop -I %S/Inputs/custom-modules/RedeclaredErrorEnum
+// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify -enable-objc-interop -I %S/Inputs/custom-modules/RedeclaredErrorEnum -DIMPORT_BASE
+// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify -enable-objc-interop -I %S/Inputs/custom-modules/RedeclaredErrorEnum -DIMPORT_BASE -Xcc -DNO_IMPORT_BASE_FROM_REDECLARED
+
+#if IMPORT_BASE
+import Base
+#endif
 
 import Redeclared
 
diff --git a/test/Interpreter/Inputs/dynamic_replacement_module.swift b/test/Interpreter/Inputs/dynamic_replacement_module.swift
index dd49581..2fd74b5 100644
--- a/test/Interpreter/Inputs/dynamic_replacement_module.swift
+++ b/test/Interpreter/Inputs/dynamic_replacement_module.swift
@@ -63,7 +63,71 @@
     return "public_enum_generic_func"
   }
 }
+#elseif MODULENODYNAMIC
+public func public_global_func() -> String {
+  return "public_global_func"
+}
 
+public func public_global_generic_func<T>(_ t: T.Type) -> String {
+  return "public_global_generic_func"
+}
+
+public class PublicClass {
+  public var str : String = ""
+  public init() {}
+  public init(x: Int) { str = "public_class_init" }
+
+  public func function() -> String {
+    return "public_class_func"
+  }
+  public func genericFunction<T>(_ t: T.Type) -> String {
+    return "public_class_generic_func"
+  }
+}
+
+public struct PublicStruct {
+  public var str = ""
+  public init() {}
+
+  public init(x: Int) { str = "public_struct_init" }
+
+  public func function() -> String {
+    return "public_struct_func"
+  }
+  public func genericFunction<T>(_ t: T.Type) -> String {
+    return "public_struct_generic_func"
+  }
+  public var public_stored_property : String = "public_stored_property"
+
+  public subscript(_ x: Int) -> String {
+    get {
+      return "public_subscript_get"
+    }
+    set {
+      str = newValue
+    }
+  }
+  public subscript(y x: Int) -> String {
+    _read {
+      yield "public_subscript_get_modify_read"
+    }
+    _modify {
+      yield &str
+    }
+  }
+}
+
+public enum PublicEnumeration<Q> {
+  case A
+  case B
+
+  public func function() -> String {
+    return "public_enum_func"
+  }
+  public func genericFunction<T>(_ t: T.Type) -> String {
+    return "public_enum_generic_func"
+  }
+}
 #elseif MODULE2
 
 import Module1
diff --git a/test/Interpreter/dynamic_replacement.swift b/test/Interpreter/dynamic_replacement.swift
index 3db1341..4e23a0a 100644
--- a/test/Interpreter/dynamic_replacement.swift
+++ b/test/Interpreter/dynamic_replacement.swift
@@ -29,6 +29,23 @@
 // RUN: %target-codesign %t/main %t/libModule1.%target-dylib-extension %t/libModule2.%target-dylib-extension
 // RUN: %target-run %t/main %t/libModule1.%target-dylib-extension %t/libModule2.%target-dylib-extension
 
+// Test the -enable-implicit-dynamic flag.
+
+// RUN: %empty-directory(%t)
+// RUN: %target-build-swift-dylib(%t/libModule1.%target-dylib-extension) -DMODULENODYNAMIC -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -Xfrontend -enable-implicit-dynamic
+// RUN: %target-build-swift-dylib(%t/libModule2.%target-dylib-extension) -I%t -L%t -lModule1 -Xlinker -rpath -Xlinker %t -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift
+// RUN: %target-build-swift -I%t -L%t -lModule1 -DMAIN -o %t/main -Xlinker -rpath -Xlinker %t %s -swift-version 5
+// RUN: %target-codesign %t/main %t/libModule1.%target-dylib-extension %t/libModule2.%target-dylib-extension
+// RUN: %target-run %t/main %t/libModule1.%target-dylib-extension %t/libModule2.%target-dylib-extension
+
+// Test the -enable-implicit-dynamic flag in optimized wholemodule mode.
+// RUN: %empty-directory(%t)
+// RUN: %target-build-swift-dylib(%t/libModule1.%target-dylib-extension) -O -wmo -DMODULENODYNAMIC -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift -Xfrontend -enable-implicit-dynamic
+// RUN: %target-build-swift-dylib(%t/libModule2.%target-dylib-extension) -O -wmo -I%t -L%t -lModule1 -Xlinker -rpath -Xlinker %t -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_module.swift
+// RUN: %target-build-swift -O -wmo -I%t -L%t -lModule1 -DMAIN -o %t/main -Xlinker -rpath -Xlinker %t %s -swift-version 5
+// RUN: %target-codesign %t/main %t/libModule1.%target-dylib-extension %t/libModule2.%target-dylib-extension
+// RUN: %target-run %t/main %t/libModule1.%target-dylib-extension %t/libModule2.%target-dylib-extension
+
 
 // REQUIRES: executable_test
 
diff --git a/test/SILGen/dynamically_replaceable.swift b/test/SILGen/dynamically_replaceable.swift
index 4db2b07..ffff84d 100644
--- a/test/SILGen/dynamically_replaceable.swift
+++ b/test/SILGen/dynamically_replaceable.swift
@@ -1,5 +1,10 @@
 // RUN: %target-swift-emit-silgen -enable-sil-ownership -swift-version 5 %s | %FileCheck %s
+// RUN: %target-swift-emit-silgen -enable-sil-ownership -swift-version 5 %s -enable-implicit-dynamic | %FileCheck %s --check-prefix=IMPLICIT
 
+// CHECK-LABEL: sil hidden @$s23dynamically_replaceable014maybe_dynamic_B0yyF : $@convention(thin) () -> () {
+// IMPLICIT-LABEL: sil hidden [dynamically_replacable] @$s23dynamically_replaceable014maybe_dynamic_B0yyF : $@convention(thin) () -> () {
+func maybe_dynamic_replaceable() {
+}
 
 // CHECK-LABEL: sil hidden [dynamically_replacable] @$s23dynamically_replaceable08dynamic_B0yyF : $@convention(thin) () -> () {
 dynamic func dynamic_replaceable() {
diff --git a/test/expr/unary/keypath/keypath-unimplemented.swift b/test/expr/unary/keypath/keypath-unimplemented.swift
index ac441f0..222ba45 100644
--- a/test/expr/unary/keypath/keypath-unimplemented.swift
+++ b/test/expr/unary/keypath/keypath-unimplemented.swift
@@ -22,3 +22,14 @@
 let _: KeyPath<TupleKeypath, String> = \TupleKeypath.labeled.bar // expected-error {{key path support for tuples is not implemented}}
 let _: KeyPath<TupleKeypath, Int> = \TupleKeypath.unlabeled.0 // expected-error {{key path support for tuples is not implemented}}
 let _: KeyPath<TupleKeypath, String> = \TupleKeypath.unlabeled.1 // expected-error {{key path support for tuples is not implemented}}
+
+struct S {
+  typealias X = (lhs: Int, rhs: Int)
+  typealias Y = (Int, Int)
+
+  let x: X
+  let y: Y
+}
+
+let _: KeyPath<S, Int> = \S.x.lhs // expected-error {{key path support for tuples is not implemented}}
+let _: KeyPath<S, Int> = \S.y.0   // expected-error {{key path support for tuples is not implemented}}
diff --git a/validation-test/stdlib/ModelIO.swift b/validation-test/stdlib/ModelIO.swift
index 9ffeb4a..6272a35 100644
--- a/validation-test/stdlib/ModelIO.swift
+++ b/validation-test/stdlib/ModelIO.swift
@@ -3,11 +3,6 @@
 // UNSUPPORTED: OS=watchos
 // REQUIRES: objc_interop
 
-// Currently crashes on iphonesimulator.
-// rdar://35490456
-// UNSUPPORTED: OS=ios
-// UNSUPPORTED: OS=tvos
-
 import StdlibUnittest
 
 import Foundation