Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7308] Seg Fault: While emitting IR SIL function #49856

Closed
swift-ci opened this issue Mar 29, 2018 · 20 comments
Closed

[SR-7308] Seg Fault: While emitting IR SIL function #49856

swift-ci opened this issue Mar 29, 2018 · 20 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software regression swift 4.1

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7308
Radar rdar://problem/39040680
Original Reporter rexmas (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Xcode 9.3

Swift 4.1

MacBook Pro

iOS Simulator

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.1Regression, CompilerCrash
Assignee rexmas (JIRA)
Priority Medium

md5: 3ddb60c8a7ab99482c8525758d6b3590

Issue Description:

A project for mapping data from JSON to Realm that heavily relies on generics can no longer compile as of Xcode 9.3. This is preventing our team from updating to the latest Xcode since it's heavily used in our App.

You can clone it and try to build here

0  swift                    0x0000000109c47ffa PrintStackTraceSignalHandler(void*) + 42

1  swift                    0x0000000109c473b6 SignalHandler(int) + 966

2  libsystem_platform.dylib 0x00007fff6ddfcf5a _sigtramp + 26

3  swift                    0x000000010643aaa0 llvm::Optional<swift::ProtocolConformanceRef> llvm::function_ref<llvm::Optional<swift::ProtocolConformanceRef> (swift::CanType, swift::Type, swift::ProtocolType*)>::callback_fn<swift::MakeAbstractConformanceForGenericType>(long, swift::CanType, swift::Type, swift::ProtocolType*) + 0

4  swift                    0x00000001063d43ab emitDirectTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanType) + 811

5  swift                    0x00000001063d401e swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 494

6  swift                    0x00000001063defe0 void llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>::callback_fn<(anonymous namespace)::GenericArguments::collect(swift::irgen::IRGenFunction&, swift::CanType)::'lambda'(unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>(long, unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>) + 80

7  swift                    0x0000000106413fbe swift::irgen::GenericTypeRequirements::enumerateFulfillments(swift::irgen::IRGenModule&, swift::SubstitutionMap const&, llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>) + 366

8  swift                    0x00000001063de0bf emitNominalMetadataRef(swift::irgen::IRGenFunction&, swift::NominalTypeDecl*, swift::CanType) + 479

9  swift                    0x00000001063d416e emitDirectTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanType) + 238

10 swift                    0x00000001063d401e swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 494

11 swift                    0x00000001063df423 llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRefForLayout, llvm::Value*>::visit<>(swift::CanType) + 947

12 swift                    0x00000001063dfd29 llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRefForLayout, llvm::Value*>::visit<>(swift::CanType) + 3257

13 swift                    0x00000001063dfe8c llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRefForLayout, llvm::Value*>::visit<>(swift::CanType) + 3612

14 swift                    0x000000010642a9d6 (anonymous namespace)::TupleNonFixedOffsets::getOffsetForIndex(swift::irgen::IRGenFunction&, unsigned int) + 38

15 swift                    0x00000001064b68e8 swift::irgen::ElementLayout::project(swift::irgen::IRGenFunction&, swift::irgen::Address, llvm::Optional<swift::irgen::NonFixedOffsetsImpl*>, llvm::Twine const&) const + 296

16 swift                    0x00000001064296bf swift::irgen::RecordTypeInfoImpl<(anonymous namespace)::NonFixedTupleTypeInfo, swift::irgen::WitnessSizedTypeInfo<(anonymous namespace)::NonFixedTupleTypeInfo>, (anonymous namespace)::TupleFieldInfo, false>::initializeWithTake(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType, bool) const + 607

17 swift                    0x000000010638a1ca (anonymous namespace)::MultiPayloadEnumImplStrategy::emitIndirectInitialize(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType, swift::IsTake_t, bool) const + 2442

18 swift                    0x00000001063865de (anonymous namespace)::MultiPayloadEnumImplStrategy::initializeWithTake(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType, bool) const + 302

19 swift                    0x000000010636a81c void llvm::function_ref<void (swift::irgen::TypeInfo const&, swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType)>::callback_fn<swift::irgen::IRGenModule::getOrCreateOutlinedInitializeWithTakeFunction(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*)::$_8>(long, swift::irgen::TypeInfo const&, swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType) + 44

20 swift                    0x000000010636a149 void llvm::function_ref<void (swift::irgen::IRGenFunction&)>::callback_fn<swift::irgen::IRGenModule::getOrCreateOutlinedCopyAddrHelperFunction(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, llvm::function_ref<void (swift::irgen::TypeInfo const&, swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType)>, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*)::$_7>(long, swift::irgen::IRGenFunction&) + 329

21 swift                    0x0000000106368474 swift::irgen::IRGenModule::getOrCreateHelperFunction(llvm::StringRef, llvm::Type*, llvm::ArrayRef<llvm::Type*>, llvm::function_ref<void (swift::irgen::IRGenFunction&)>, bool) + 884

22 swift                    0x0000000106369fd6 swift::irgen::IRGenModule::getOrCreateOutlinedCopyAddrHelperFunction(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, llvm::function_ref<void (swift::irgen::TypeInfo const&, swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType)>, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*) + 454

23 swift                    0x000000010636a787 swift::irgen::IRGenModule::getOrCreateOutlinedInitializeWithTakeFunction(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*) + 1367

24 swift                    0x0000000106368a91 swift::irgen::IRGenModule::generateCallToOutlinedCopyAddr(swift::irgen::IRGenFunction&, swift::irgen::TypeInfo const&, swift::irgen::Address, swift::irgen::Address, swift::SILType, llvm::Constant* (swift::irgen::IRGenModule::*)(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*), llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*) + 353

25 swift                    0x0000000106386597 (anonymous namespace)::MultiPayloadEnumImplStrategy::initializeWithTake(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType, bool) const + 231

26 swift                    0x00000001064769d9 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 37961

27 swift                    0x000000010646aff1 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9073

28 swift                    0x0000000106360070 swift::irgen::IRGenerator::emitGlobalTopLevel() + 1024

29 swift                    0x000000010644c527 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**, unsigned int) + 1399

30 swift                    0x00000001062a7dce performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 38110

31 swift                    0x000000010629ce64 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7908

32 swift                    0x00000001062518b5 main + 18917

33 libdyld.dylib            0x00007fff6db7b115 start + 1

Stack dump:

0.  Program arguments: /Users/Rex/Downloads/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/Rex/Documents/projects/Crust/Crust/Mapper/MappingKey.swift /Users/Rex/Documents/projects/Crust/Crust/Mapper/Mapper.swift -primary-file /Users/Rex/Documents/projects/Crust/Crust/Mapper/MappingOperator.swift /Users/Rex/Documents/projects/Crust/Crust/Mapper/MappingProtocols.swift /Users/Rex/Documents/projects/Crust/Crust/Utilities/Utilities.swift /Users/Rex/Documents/projects/Crust/Crust/Utilities/AnyAdapter.swift -target arm64-apple-ios8.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Users/Rex/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.3.sdk -I /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Products/Debug-iphoneos -F /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Products/Debug-iphoneos -F /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Products/Debug-iphoneos/JSONValueRX -enable-testing -g -import-underlying-module -module-cache-path /Users/Rex/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 4 -enforce-exclusivity=checked -Onone -D COCOAPODS -serialize-debugging-options -Xcc -I/Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Crust-generated-files.hmap -Xcc -I/Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Crust-own-target-headers.hmap -Xcc -I/Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Crust-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Crust-project-headers.hmap -Xcc -I/Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Products/Debug-iphoneos/include -Xcc -I/Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/DerivedSources/arm64 -Xcc -I/Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DCOCOAPODS=1 -Xcc -ivfsoverlay -Xcc /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/unextended-module-overlay.yaml -Xcc -working-directory/Users/Rex/Documents/projects/Crust -emit-module-doc-path /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Objects-normal/arm64/MappingOperator~partial.swiftdoc -serialize-diagnostics-path /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Objects-normal/arm64/MappingOperator.dia -module-name Crust -emit-module-path /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Objects-normal/arm64/MappingOperator~partial.swiftmodule -emit-dependencies-path /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Objects-normal/arm64/MappingOperator.d -emit-reference-dependencies-path /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Objects-normal/arm64/MappingOperator.swiftdeps -o /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Build/Intermediates.noindex/Crust.build/Debug-iphoneos/Crust.build/Objects-normal/arm64/MappingOperator.o -embed-bitcode-marker -index-store-path /Users/Rex/Library/Developer/Xcode/DerivedData/Crust-edirvgitijjvcrggclmcjhivbkrf/Index/DataStore -index-system-modules

1.  While emitting IR SIL function "@_T05Crust2lsoiq1_xz_AA7BindingOyq0_q_G3key_q1_7payloadtt12MappedObjectQy_RszAA7MappingR_AA0H3KeyR0_AA0H7PayloadCyq0_GRb1_r2_lF".

for '<-(_:_:)' at /Users/Rex/Documents/projects/Crust/Crust/Mapper/MappingOperator.swift:80:8
@belkadan
Copy link
Contributor

Current commit on that repo: rexmas/Crust@5f76fc8

@belkadan
Copy link
Contributor

Reproduced. The function in question looks like this:

public func <- <T, M, K, MC: MappingPayload<K>>(field: inout T, binding:(key: Binding<K, M>, payload: MC)) -> MC where M.MappedObject == T {
    return map(to: &field, using: binding)
}

and, uh, that constraint that takes another generic parameter has probably never worked. Still, that should at least be caught at compile time if we can't implement it… cc @huonw

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Rex Fenley (JIRA)

refactoring like so has the same result

public func <- <T, M, K>(field: inout T, binding:(key: Binding<K, M>, payload: MappingPayload<K>)) -> MappingPayload<K> where M.MappedObject == T {

what approach should I use to fix this in the repo so I can unblock clients?

@belkadan
Copy link
Contributor

Gotta ask: does it still crash on that function, or did that change move it to the next function in the file?

@swift-ci
Copy link
Collaborator Author

Comment by Rex Fenley (JIRA)

First crash

While emitting IR SIL function "@_T05Crust2lsoiq1_xz_AA7BindingOyq0_q_G3key_q1_7payloadtt12MappedObjectQy_RszAA7MappingR_AA0H3KeyR0_AA0H7PayloadCyq0_GRb1_r2_lF".

for '<-(_:_:)' at /Users/Rex/Documents/projects/Crust/Crust/Mapper/MappingOperator.swift:80:8

Second crash after change and clean build

While emitting IR SIL function "@_T05Crust2lsoiAA14MappingPayloadCyq0_Gxz_AA7BindingOyq0_q_G3key_AE7payloadtt12MappedObjectQy_RszAA0C0R_AA0C3KeyR0_r1_lF".

for '<-(_:_:)' at /Users/Rex/Documents/projects/Crust/Crust/Mapper/MappingOperator.swift:80:8

Line numbers are the same.

@belkadan
Copy link
Contributor

Okay, so that's a red herring. Thanks!

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 2, 2018

Comment by Rex Fenley (JIRA)

No problem, thanks for replying quickly! Do you know if there is a work around we can use?

@huonw
Copy link
Mannequin

huonw mannequin commented Apr 10, 2018

It looks as if this is resolved on master: both a recent build of the master branch, and the older 2018-04-04 development snapshot successfully compiles the whole library!

However, that doesn't solve the immediate problem of using it in Xcode 9.3. It looks like the problem here is an interaction between the -enable-testing flag, the (key: Binding<K, M>, payload: MappingPayload<K>) tuples and some unnecessary type parameters/bounds. Removing any one of those seems to allow it to compile okay, but I think the last one is the best, as it doesn't change the library's API and still allows normal testing.

For instance, T isn't needed in the following, as it is entirely identical to M.MappedObject:

public func <- <T, M, K, MC: MappingPayload<K>>(field: inout T, binding:(key: Binding<K, M>, payload: MC)) -> MC where M.MappedObject == T { ... }
// change to
public func <- <M, K, MC: MappingPayload<K>>(field: inout M.MappedObject, binding:(key: Binding<K, M>, payload: MC)) -> MC { ... }

// and

public func <- <T, M, K, MC: MappingPayload<K>, RRC: RangeReplaceableCollection>(field: inout RRC, binding:(key: Binding<K, M>, payload: MC)) -> MC where M.MappedObject == T, RRC.Iterator.Element == M.MappedObject {
// change to
public func <- <M, K, MC: MappingPayload<K>, RRC: RangeReplaceableCollection>(field: inout RRC, binding:(key: Binding<K, M>, payload: MC)) -> MC where  RRC.Iterator.Element == M.MappedObject

The same applies when there's protocol requirements:

public func <- <T, M, K, MC: MappingPayload<K>, RRC: RangeReplaceableCollection>(field: inout RRC?, binding:(key: Binding<K, M>, payload: MC)) -> MC where M.MappedObject == T, RRC.Iterator.Element == M.MappedObject, T: Equatable {
// change to
public func <- <M, K, MC: MappingPayload<K>, RRC: RangeReplaceableCollection>(field: inout RRC?, binding:(key: Binding<K, M>, payload: MC)) -> MC where M.MappedObject: Equatable, RRC.Iterator.Element == M.MappedObject {

Finally, there's two functions (around line 100 of MappingOperator.swift) that have a seemingly unused TF: Transform bound, that also, unfortunately, causes things to crash, so along with the previous point of removing type parameters:

public func <- <T: JSONable, TF: Transform, K, MC: MappingPayload<K>>(field: inout T, binding:(key: Binding<K, TF>, payload: MC)) -> MC where TF.MappedObject == T, T == T.ConversionType
// change to
public func <- <TF, K, MC: MappingPayload<K>>(field: inout TF.MappedObject, binding:(key: Binding<K, TF>, payload: MC)) -> MC where TF.MappedObject: JSONable, TF.MappedObject == TF.MappedObject.ConversionType {

The original code should work, and it's definitely sometimes clearer to be able to use a short generic parameter T to abbreviate a longer nested type like M.MappedObject, but it seems like that confuses the Xcode 9.3 compiler.

> that constraint that takes another generic parameter has probably never worked

I think it should be okay: MappingPayload is a class, not a protocol.

@slavapestov
Copy link
Member

@belkadan superclass constraints where the superclass references another generic parameter (eg, <T, U : Bar<T>>) should work. I don't think that was the problem here.

@swift-ci
Copy link
Collaborator Author

Comment by Rex Fenley (JIRA)

@huonw Thanks for the help. It got me pretty far but now I'm crashing here on Xcode 9.3 https://github.com/rexmas/Crust/blob/swift-4.1/RealmCrustTests/RealmMappings.swift#L268

You'll need to checkout `swift-4.1` branch and build the tests to see this file compile and the crash.

However, this still blocks us. We don't distribute this file with the Pod since it's dependent on Realm, which would thus require a client to include all of Realm (which they may not use) with Crust. However, our team does copy this source file directly into our own project since we personally do use Realm. In other words, we're still blocked on moving to 9.3 until the test files can all compile too.

@huonw
Copy link
Mannequin

huonw mannequin commented Apr 12, 2018

Ah! I'd missed the tests completely, sorry.

Unfortunately that does look like the solution I mentioned above doesn't fix that problem, so there's something more fundamental going on here.

@jckarter
Copy link
Member

Using a swift 4.1 snapshot with assertions on, we get a more descriptive assertion failure message for the original crash:

Assertion failed: (!archetype->isPrimary() && "type metadata for primary archetype was not bound in context"), function emitArchetypeTypeMetadataRef, file /Users/buildnode/jenkins/workspace/oss-swift-4.1-package-osx/swift/lib/IRGen/GenArchetype.cpp, line 64.
0  swift                    0x000000010f84b498 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010f84bba6 SignalHandler(int) + 694
2  libsystem_platform.dylib 0x00007fff5b0def5a _sigtramp + 26
3  libdyld.dylib            0x00007fff5add0149 dyldGlobalLockRelease() + 0
4  libsystem_c.dylib        0x00007fff5ae7c1ae abort + 127
5  libsystem_c.dylib        0x00007fff5ae441ac basename_r + 0
6  swift                    0x000000010c349e76 swift::irgen::emitArchetypeTypeMetadataRef(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::ArchetypeType>) + 182
7  swift                    0x000000010c3fea5a llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRef, llvm::Value*>::visit<>(swift::CanType) + 778
8  swift                    0x000000010c3f09c0 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 96
9  swift                    0x000000010c400c1e void llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>::callback_fn<(anonymous namespace)::GenericArguments::collect(swift::irgen::IRGenFunction&, swift::CanType)::'lambda'(unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>(long, unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>) + 78
10 swift                    0x000000010c41d0e5 swift::irgen::GenericTypeRequirements::enumerateFulfillments(swift::irgen::IRGenModule&, swift::SubstitutionMap const&, llvm::function_ref<void (unsigned int, swift::CanType, llvm::Optional<swift::ProtocolConformanceRef>)>) + 261
11 swift                    0x000000010c40081c emitNominalMetadataRef(swift::irgen::IRGenFunction&, swift::NominalTypeDecl*, swift::CanType) + 348
12 swift                    0x000000010c3fe8a6 llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRef, llvm::Value*>::visit<>(swift::CanType) + 342
13 swift                    0x000000010c3f09c0 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 96
14 swift                    0x000000010c3f0d65 llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRefForLayout, llvm::Value*>::visit<>(swift::CanType) + 85
15 swift                    0x000000010c3f118f llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRefForLayout, llvm::Value*>::visit<>(swift::CanType) + 1151
16 swift                    0x000000010c3f1207 llvm::Value* swift::CanTypeVisitor<(anonymous namespace)::EmitTypeMetadataRefForLayout, llvm::Value*>::visit<>(swift::CanType) + 1271
17 swift                    0x000000010c3f0cde swift::irgen::IRGenFunction::emitTypeMetadataRefForLayout(swift::SILType) + 46
18 swift                    0x000000010c43fea9 (anonymous namespace)::TupleNonFixedOffsets::getOffsetForIndex(swift::irgen::IRGenFunction&, unsigned int) + 25
19 swift                    0x000000010c4b650d swift::irgen::ElementLayout::project(swift::irgen::IRGenFunction&, swift::irgen::Address, llvm::Optional<swift::irgen::NonFixedOffsetsImpl*>, llvm::Twine const&) const + 141
20 swift                    0x000000010c43ed8b swift::irgen::RecordTypeInfoImpl<(anonymous namespace)::NonFixedTupleTypeInfo, swift::irgen::WitnessSizedTypeInfo<(anonymous namespace)::NonFixedTupleTypeInfo>, (anonymous namespace)::TupleFieldInfo, false>::initializeWithTake(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType, bool) const + 747
21 swift                    0x000000010c3b49ff (anonymous namespace)::MultiPayloadEnumImplStrategy::emitIndirectInitialize(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType, swift::IsTake_t, bool) const + 1279
22 swift                    0x000000010c3ae78a (anonymous namespace)::MultiPayloadEnumImplStrategy::initializeWithTake(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType, bool) const + 426
23 swift                    0x000000010c39a71c void llvm::function_ref<void (swift::irgen::TypeInfo const&, swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType)>::callback_fn<swift::irgen::IRGenModule::getOrCreateOutlinedInitializeWithTakeFunction(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*)::$_8>(long, swift::irgen::TypeInfo const&, swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType) + 44
24 swift                    0x000000010c39a67b void llvm::function_ref<void (swift::irgen::IRGenFunction&)>::callback_fn<swift::irgen::IRGenModule::getOrCreateOutlinedCopyAddrHelperFunction(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, llvm::function_ref<void (swift::irgen::TypeInfo const&, swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType)>, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*)::$_7>(long, swift::irgen::IRGenFunction&) + 203
25 swift                    0x000000010c3940d5 swift::irgen::IRGenModule::getOrCreateHelperFunction(llvm::StringRef, llvm::Type*, llvm::ArrayRef<llvm::Type*>, llvm::function_ref<void (swift::irgen::IRGenFunction&)>, bool) + 293
26 swift                    0x000000010c3952ea swift::irgen::IRGenModule::getOrCreateOutlinedCopyAddrHelperFunction(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, llvm::function_ref<void (swift::irgen::TypeInfo const&, swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType)>, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*) + 346
27 swift                    0x000000010c39554a swift::irgen::IRGenModule::getOrCreateOutlinedInitializeWithTakeFunction(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*) + 570
28 swift                    0x000000010c39484f swift::irgen::IRGenModule::generateCallToOutlinedCopyAddr(swift::irgen::IRGenFunction&, swift::irgen::TypeInfo const&, swift::irgen::Address, swift::irgen::Address, swift::SILType, llvm::Constant* (swift::irgen::IRGenModule::*)(swift::irgen::TypeInfo const&, llvm::Type*, swift::SILType, llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*), llvm::MapVector<swift::CanType, llvm::Value*, llvm::DenseMap<swift::CanType, unsigned int, llvm::DenseMapInfo<swift::CanType>, llvm::detail::DenseMapPair<swift::CanType, unsigned int> >, std::__1::vector<std::__1::pair<swift::CanType, llvm::Value*>, std::__1::allocator<std::__1::pair<swift::CanType, llvm::Value*> > > > const*) + 287
29 swift                    0x000000010c3ae71b (anonymous namespace)::MultiPayloadEnumImplStrategy::initializeWithTake(swift::irgen::IRGenFunction&, swift::irgen::Address, swift::irgen::Address, swift::SILType, bool) const + 315
30 swift                    0x000000010c48aeb1 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 37041
31 swift                    0x000000010c47e47f (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 7151
32 swift                    0x000000010c47c3a1 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1361
33 swift                    0x000000010c38b585 swift::irgen::IRGenerator::emitGlobalTopLevel() + 485
34 swift                    0x000000010c458d04 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**, unsigned int) + 1124
35 swift                    0x000000010c459396 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, llvm::LLVMContext&, unsigned int, llvm::GlobalVariable**) + 86
36 swift                    0x000000010c3022ba performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13786
37 swift                    0x000000010c2fdcf0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3376
38 swift                    0x000000010c2bd5a3 main + 3075
39 libdyld.dylib            0x00007fff5add0015 start + 1
40 libdyld.dylib            0x0000000000000054 start + 2770534464
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-28-a.xctoolchain/usr/bin/swift -frontend -c /Users/jgroff/src/Crust/Crust/Mapper/MappingKey.swift /Users/jgroff/src/Crust/Crust/Mapper/Mapper.swift -primary-file /Users/jgroff/src/Crust/Crust/Mapper/MappingOperator.swift /Users/jgroff/src/Crust/Crust/Mapper/MappingProtocols.swift /Users/jgroff/src/Crust/Crust/Utilities/Utilities.swift /Users/jgroff/src/Crust/Crust/Utilities/AnyAdapter.swift -target x86_64-apple-ios8.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.3.sdk -I /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Products/Debug-iphonesimulator -F /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Products/Debug-iphonesimulator -F /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Products/Debug-iphonesimulator/JSONValueRX -enable-testing -g -import-underlying-module -module-cache-path /Users/jgroff/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 4 -enforce-exclusivity=checked -Onone -D COCOAPODS -serialize-debugging-options -Xcc -I/Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Crust-generated-files.hmap -Xcc -I/Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Crust-own-target-headers.hmap -Xcc -I/Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Crust-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Crust-project-headers.hmap -Xcc -I/Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/DerivedSources/x86_64 -Xcc -I/Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DCOCOAPODS=1 -Xcc -ivfsoverlay -Xcc /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/unextended-module-overlay.yaml -Xcc -working-directory/Users/jgroff/src/Crust -emit-module-doc-path /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Objects-normal/x86_64/MappingOperator~partial.swiftdoc -serialize-diagnostics-path /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Objects-normal/x86_64/MappingOperator.dia -module-name Crust -emit-module-path /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Objects-normal/x86_64/MappingOperator~partial.swiftmodule -emit-dependencies-path /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Objects-normal/x86_64/MappingOperator.d -emit-reference-dependencies-path /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Objects-normal/x86_64/MappingOperator.swiftdeps -o /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Build/Intermediates.noindex/Crust.build/Debug-iphonesimulator/Crust.build/Objects-normal/x86_64/MappingOperator.o -index-store-path /Users/jgroff/Library/Developer/Xcode/DerivedData/Crust-ajwdfzcatvzzfdfhqnedyxqusudn/Index/DataStore -index-system-modules 
1.  While emitting IR SIL function "@_T05Crust2lsoiq1_xz_AA7BindingOyq0_q_G3key_q1_7payloadtt12MappedObjectQy_RszAA7MappingR_AA0H3KeyR0_AA0H7PayloadCyq0_GRb1_r2_lF".
 for '<-(_:_:)' at /Users/jgroff/src/Crust/Crust/Mapper/MappingOperator.swift:80:8

@jckarter
Copy link
Member

I can reproduce Huon's success with the master branch; I was able to both build Crust and run the tests (after altering Realm a bit to work around https://bugs.swift.org/browse/SR-7435) cleanly with a snapshot from master. Let me look back at the 4.1 branch and see if I can figure out a workaround that clears up the issues in both the implementation and tests.

@slavapestov
Copy link
Member

@rjmccall made some major changes to the outlined value operation thunks on master, so its possible some bugs were fixed along the way. If we can add a regression test it would be great.

@jckarter
Copy link
Member

I'd suspect the fix to be somewhere like in enumerateFulfillments rather than anything specific to out-of-line value operations.

@slavapestov
Copy link
Member

No, IIRC the issue was the the out-of-line operations had a visitor to collect substitutions from the generic type and they weren't visiting all the structural components of the type in all cases.

@jckarter
Copy link
Member

I see. Since the problem is in the out-of-line copy/consume enum operations, it looks like you can work around the bug by making the Binding enum be indirect:

--- a/Crust/Mapper/MappingProtocols.swift
+++ b/Crust/Mapper/MappingProtocols.swift
@@ -9,7 +9,7 @@ public enum CollectionInsertionMethod<Element> {
 public typealias CollectionUpdatePolicy<Element> =
     (insert: CollectionInsertionMethod<Element>, unique: Bool, nullable: Bool)
 
-public enum Binding<K: MappingKey, M: Mapping> {
+public indirect enum Binding<K: MappingKey, M: Mapping> {
     
     case mapping(K, M)
     case collectionMapping(K, M, CollectionUpdatePolicy<M.MappedObject>)

You should be able to back out that change when the 4.2 or master branch ships, but for the meantime in Xcode 9.3 that should hopefully keep you going. Would you be able to submit the Crust project to our compatibility test suite at https://github.com/apple/swift-source-compat-suite ?

@swift-ci
Copy link
Collaborator Author

Comment by Rex Fenley (JIRA)

Amazing, that worked. I'd be happy to add it to the compatibility suite. It will take some rejiggering first to support SPM, but I'll set some time aside to make that happen. Thank you all for helping out!

@jckarter
Copy link
Member

Thanks for following up, and for offering to contribute your project to the compatibility suite!

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Jul 26, 2018

Changing the state to "Closed" since it seems that Rex confirmed that the issue is fixed.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software regression swift 4.1
Projects
None yet
Development

No branches or pull requests

5 participants