Merge pull request #18133 from gregomni/8324

diff --git a/lib/Sema/TypeCheckProtocolInference.cpp b/lib/Sema/TypeCheckProtocolInference.cpp
index bcc92c8..7dcc839 100644
--- a/lib/Sema/TypeCheckProtocolInference.cpp
+++ b/lib/Sema/TypeCheckProtocolInference.cpp
@@ -1002,11 +1002,14 @@
       if (auto depMemTy = dyn_cast<DependentMemberType>(type)) {
         if (!depMemTy->getAssocType() ||
             depMemTy->getAssocType()->getProtocol() != proto) {
+
           for (auto member : proto->lookupDirect(depMemTy->getName())) {
             if (auto assocType = dyn_cast<AssociatedTypeDecl>(member)) {
-              return Type(DependentMemberType::get(
-                                           sanitizeType(depMemTy->getBase()),
-                                           assocType));
+              Type sanitizedBase = sanitizeType(depMemTy->getBase());
+              if (!sanitizedBase)
+                return Type();
+              return Type(DependentMemberType::get(sanitizedBase,
+                                                   assocType));
             }
           }
 
diff --git a/test/Generics/conditional_conformances.swift b/test/Generics/conditional_conformances.swift
index 786cc5c..74a4322 100644
--- a/test/Generics/conditional_conformances.swift
+++ b/test/Generics/conditional_conformances.swift
@@ -396,3 +396,20 @@
     public typealias Iterator = IndexingIterator<[Float]>
     public func makeIterator() -> Iterator { fatalError() }
 }
+
+// SR-8324
+protocol ElementProtocol {
+  associatedtype BaseElement: BaseElementProtocol = Self
+}
+protocol BaseElementProtocol: ElementProtocol where BaseElement == Self {}
+protocol ArrayProtocol {
+  associatedtype Element: ElementProtocol
+}
+protocol NestedArrayProtocol: ArrayProtocol where Element: ArrayProtocol, Element.Element.BaseElement == Element.BaseElement {
+  associatedtype BaseElement = Element.BaseElement
+}
+extension Array: ArrayProtocol where Element: ElementProtocol {}
+extension Array: NestedArrayProtocol where Element: ElementProtocol, Element: ArrayProtocol, Element.Element.BaseElement == Element.BaseElement {
+  // with the typealias uncommented you do not get a crash.
+  // typealias BaseElement = Element.BaseElement
+}