Swift compiler crash when mixing opaque return types and generics

Hi, I've recently encountered an interesting crash in the compiler. This crash happens in both Swift 5.2 and Swift 5.3. I've submitted a bug report to Apple, but I'm genuinely curious at what is going on here, and am wondering if someone on the forums might have some insight. The test case is pretty concise, and it is interesting that the crash can be "fixed" by either removing a generic argument, explicitly specifying an opaque return type, or removing a property of a generic type.

To verify, you can simply swiftc <illegal-instruction.swift> if "illegal-instruction.swift" has the following contents:


protocol ContentProtocol { }
extension String: ContentProtocol { }

struct ContentContainer: ContentContainerProtocol {
  var content: Content
  /// Making the return type explicitly `String` fixes the crash
  static func content() -> some ContentProtocol { "Content" }
}

protocol ContentContainerProtocol {
  associatedtype Content
  static func content() -> Content
}

// Removing `<Controller>` fixes the crash
struct Search<Controller>: Command {
  var context: CommandContext<Self>
  typealias Response = ContentContainer
}

protocol Command {
  associatedtype Response
  var context: CommandContext<Self> { get set }
}

typealias OuterCommand = Command
struct CommandContext<Command: OuterCommand> {
  /// Removing this property fixes the crash
  var response: Command.Response
}

Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file illegal-instruction.swift -target x86_64-apple-darwin19.5.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -module-name main -o /var/folders/jg/_hvbx1hd6psf7vh9g_ytrrcw0000gn/T/illegal-instruction-cb0dba.o 
1.	Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
2.	While emitting SIL for getter for context (at illegal-instruction.swift:18:7)
3.	While silgen emitFunction SIL function "@$s4main6SearchV7contextAA14CommandContextVyACyxGGvg".
 for getter for context (at illegal-instruction.swift:18:7)
0  swift                    0x00000001072b14ea PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x00000001072b0cc0 SignalHandler(int) + 352
2  libsystem_platform.dylib 0x00007fff6b3005fd _sigtramp + 29
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2496657952
4  swift                    0x00000001034fdacd (anonymous namespace)::EmitBBArguments::visitType(swift::CanType, bool) + 621
5  swift                    0x00000001034fddce swift::CanTypeVisitor<(anonymous namespace)::EmitBBArguments, swift::Lowering::ManagedValue>::visit(swift::CanType) + 62
6  swift                    0x00000001034fd321 (anonymous namespace)::ArgumentInitHelper::emitParam(swift::ParamDecl*) + 545
7  swift                    0x00000001034fc97a swift::Lowering::SILGenFunction::emitProlog(swift::ParameterList*, swift::ParamDecl*, swift::Type, swift::DeclContext*, bool, swift::SourceLoc) + 490
8  swift                    0x00000001034fb294 swift::Lowering::SILGenFunction::emitProlog(swift::CaptureInfo, swift::ParameterList*, swift::ParamDecl*, swift::DeclContext*, swift::Type, bool, swift::SourceLoc) + 68
9  swift                    0x00000001034b15a3 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 323
10 swift                    0x0000000103417959 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 953
11 swift                    0x00000001035131b8 void llvm::function_ref<void (swift::AccessorDecl*)>::callback_fn<(anonymous namespace)::SILGenType::visitAccessors(swift::AbstractStorageDecl*)::'lambda'(swift::AccessorDecl*)>(long, swift::AccessorDecl*) + 24
12 swift                    0x000000010401ddf2 swift::AbstractStorageDecl::visitExpectedOpaqueAccessors(llvm::function_ref<void (swift::AccessorKind)>) const + 322
13 swift                    0x0000000103513166 (anonymous namespace)::SILGenType::visitVarDecl(swift::VarDecl*) + 2022
14 swift                    0x000000010350fcdb (anonymous namespace)::SILGenType::emitType() + 1163
15 swift                    0x0000000103421d82 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 82
16 swift                    0x0000000103420f4c swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 1356
17 swift                    0x0000000103422fce swift::SILModule::constructSIL(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&, swift::FileUnit*) + 1438
18 swift                    0x0000000103002d51 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 48065
19 swift                    0x0000000102f7ab73 main + 1283
20 libdyld.dylib            0x00007fff6b107cc9 start + 1
<unknown>:0: error: unable to execute command: Illegal instruction: 4
<unknown>:0: error: compile command failed due to signal 4 (use -v to see invocation)

This triggers an assertion on a recent master.

unsupported convention for API
UNREACHABLE executed at /Users/varun/foss-swift/swift/lib/SILGen/SILGenBuilder.cpp:452!
Stack dump:
0.      Program arguments: /Users/varun/foss-swift/build/Ninja-ReleaseAssert/swift-macosx-x86_64/bin/swift-frontend -frontend -c -primary-file tmp.swift -target x86_64-apple-darwin19.5.0 -enable-objc-interop -color-diagnostics -module-name tmp -o /var/folders/9t/dn6p4v392nb8c8cwxfyxc84c0000gn/T/tmp-fbe0a8.o
1.      Swift version 5.3-dev (LLVM f9957f081870012, Swift 43fd786ae08cf97)
2.      While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "tmp.swift")
3.      While silgen emitFunction SIL function "@$s3tmp6SearchV7contextAA14CommandContextVyACyxGGvg".
 for getter for context (at tmp.swift:17:7)
0  swift-frontend           0x0000000106e393a5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift-frontend           0x0000000106e38378 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x0000000106e39986 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007fff6d8b55fd _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603338678315552
5  libsystem_c.dylib        0x00007fff6d78b808 abort + 120
6  swift-frontend           0x0000000106da70e2 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 482
7  swift-frontend           0x0000000102d518bd createInputFunctionArgument(swift::Lowering::SILGenBuilder&, swift::SILType, swift::SILLocation, swift::ValueDecl*) + 333
8  swift-frontend           0x0000000102d51765 swift::Lowering::SILGenBuilder::createInputFunctionArgument(swift::SILType, swift::ValueDecl*) + 69

It's reasonably likely that the crash you're seeing is because of the same underlying issue.