Merge pull request #21060 from rjmccall/global-observer-crash-5.0

[5.0] Don't crash when modifying a global observed variable
diff --git a/lib/SILGen/SILGen.cpp b/lib/SILGen/SILGen.cpp
index 5a3e11c..f23c479 100644
--- a/lib/SILGen/SILGen.cpp
+++ b/lib/SILGen/SILGen.cpp
@@ -675,10 +675,10 @@
 /// Emit a function now, if it's externally usable or has been referenced in
 /// the current TU, or remember how to emit it later if not.
 template<typename /*void (SILFunction*)*/ Fn>
-void emitOrDelayFunction(SILGenModule &SGM,
-                         SILDeclRef constant,
-                         Fn &&emitter,
-                         bool forceEmission = false) {
+static void emitOrDelayFunction(SILGenModule &SGM,
+                                SILDeclRef constant,
+                                Fn &&emitter,
+                                bool forceEmission = false) {
   auto emitAfter = SGM.lastEmittedFunction;
 
   SILFunction *f = nullptr;
@@ -1312,7 +1312,8 @@
       case AccessorKind::Read:
         return impl.getReadImpl() != ReadImplKind::Read;
       case AccessorKind::Set:
-        return impl.getWriteImpl() != WriteImplKind::Set;
+        return impl.getWriteImpl() != WriteImplKind::Set &&
+               impl.getWriteImpl() != WriteImplKind::StoredWithObservers;
       case AccessorKind::Modify:
         return impl.getReadWriteImpl() != ReadWriteImplKind::Modify;
 #define ACCESSOR(ID) \
diff --git a/test/SILGen/lazy_global_access.swift b/test/SILGen/lazy_global_access.swift
index 836f34d..5f09180 100644
--- a/test/SILGen/lazy_global_access.swift
+++ b/test/SILGen/lazy_global_access.swift
@@ -22,3 +22,16 @@
   return (globalProp, Fooo.staticProp)
 }
 
+// rdar://46472759
+// We used to crash tying to double-emit the setter.
+struct Bar {
+  mutating func mutate() {}
+}
+func useGlobalBar() -> Bar {
+  globalBar = Bar()
+  globalBar.mutate()
+  return globalBar
+}
+private var globalBar = Bar() {
+  willSet {}
+}