The current check in MiscDiagnostics can only handle local 2-cycles as in:
func foo() -> some P { return bar() }
func bar() -> some P { return foo() }
Larger cycles, such as the 3-cycle present in the validation test in this patch cannot be detected without resolving all substitutions up front. Therefore, we take the tact that we can at least prevent the compiler from crashing. At runtime, instead, type resolution will blow out the stack.
The check here is simple: Plumb through a set of opaque type decls that the resolution machinery has already seen, and if we encounter a recursive opaque type substitution bail with the original opaque type.
rdar://87121502