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-4175] 3.1 regression: Segfault in MergeSwiftModule, "Unable to resolve type witness" #46758

Closed
swift-ci opened this issue Mar 7, 2017 · 9 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

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 7, 2017

Previous ID SR-4175
Radar rdar://problem/30897622
Original Reporter ssheldon (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

macOS 10.12.3, Xcode 8.3 beta 3, Swift 3.1 snapshot 2017-03-05

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, 3.1Regression, CompilerCrash
Assignee None
Priority Medium

md5: 1c5878440c1a7581b3ddc4ef6a45c777

Issue Description:

If you copy the following into an iOS app target:

protocol FooProtocol {
  associatedtype MyType

  func doWithBar<T: BarProtocol>(bar: T) where T.FooType == Self
}

protocol BarProtocol {
  associatedtype FooType: FooProtocol
}

class Foo: FooProtocol {
  typealias MyType = Int

  func doWithBar<T: BarProtocol>(bar: T) where T.FooType == Foo {
  }
}

And then try to compile, you'll see a segfault during a MergeSwiftModule command:

Assertion failed: (resolver && "Unable to resolve type witness"), function getTypeWitnessSubstAndDecl, file /Users/buildnode/jenkins/workspace/oss-swift-3.1-package-osx/swift/lib/AST/ProtocolConformance.cpp, line 299.
0  swift                    0x000000010c939548 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010c938766 llvm::sys::RunSignalHandlers() + 86
2  swift                    0x000000010c939b99 SignalHandler(int) + 361
3  libsystem_platform.dylib 0x00007fffbc28fbba _sigtramp + 26
4  swift                    0x000000010d4e4690 vtable for llvm::PrettyStackTraceString + 16
5  libsystem_c.dylib        0x00007fffbc116420 abort + 129
6  libsystem_c.dylib        0x00007fffbc0dd893 basename_r + 0
7  swift                    0x000000010a9b0a62 swift::NormalProtocolConformance::getTypeWitnessSubstAndDecl(swift::AssociatedTypeDecl*, swift::LazyResolver*) const + 402
8  swift                    0x000000010a9b0899 swift::ProtocolConformance::getTypeWitnessSubstAndDecl(swift::AssociatedTypeDecl*, swift::LazyResolver*) const + 25
9  swift                    0x000000010a9b1169 swift::ProtocolConformance::getTypeWitness(swift::AssociatedTypeDecl*, swift::LazyResolver*) const + 9
10 swift                    0x000000010a8a92ef swift::ArchetypeBuilder::addSameTypeRequirementToConcrete(swift::ArchetypeBuilder::PotentialArchetype*, swift::Type, swift::RequirementSource) + 1391
11 swift                    0x000000010a8ad1cb swift::ArchetypeBuilder::addSameTypeRequirement(swift::Type, swift::Type, swift::RequirementSource) + 171
12 swift                    0x000000010a8af90f swift::ArchetypeBuilder::addGenericSignature(swift::GenericSignature*) + 111
13 swift                    0x000000010a5ca918 swift::ModuleFile::finishNormalConformance(swift::NormalProtocolConformance*, unsigned long long) + 1304
14 swift                    0x000000010a9b1903 swift::NormalProtocolConformance::resolveLazyInfo() const + 99
15 swift                    0x000000010a5fa2e0 swift::serialization::Serializer::writeNormalConformance(swift::NormalProtocolConformance const*) + 960
16 swift                    0x000000010a60ac0b swift::serialization::Serializer::writeAllDeclsAndTypes() + 8523
17 swift                    0x000000010a60c0c3 swift::serialization::Serializer::writeAST(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, bool) + 3171
18 swift                    0x000000010a610abb swift::serialization::Serializer::writeToStream(llvm::raw_ostream&, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SILModule const*, swift::SerializationOptions const&) + 139
19 swift                    0x000000010a63759e void llvm::function_ref<void (llvm::raw_ostream&)>::callback_fn<swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::SILModule const*)::$_5>(long, llvm::raw_ostream&) + 110
20 swift                    0x000000010a611d3b withOutputFile(swift::ASTContext&, llvm::StringRef, llvm::function_ref<void (llvm::raw_ostream&)>) + 395
21 swift                    0x000000010a611b2c swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::SILModule const*) + 220
22 swift                    0x0000000109b0167a swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 14394
23 swift                    0x0000000109abc9b0 main + 3312
24 libdyld.dylib            0x00007fffbc082255 start + 1

This code compiles fine under Xcode 8.2. The segfault here was recorded on Xcode 8.3 beta 3 using the 2017-03-05 snapshot of Swift 3.1.

I've attached a project which can be used to repro.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 7, 2017

Comment by Steven Sheldon (JIRA)

cc @slavapestov; our code which ultimately resulted in this segfault was originally crashing from SR-3500, so this issue might have a similar root cause.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 7, 2017

Comment by Steven Sheldon (JIRA)

I encountered this segfault while trying to minimize a different segfault:

Assertion failed: (TypeWitnesses.count(assocType) == 0 && "Type witness already known"), function setTypeWitness, file /Users/buildnode/jenkins/workspace/oss-swift-3.1-package-osx/swift/lib/AST/ProtocolConformance.cpp, line 314.
0  swift                    0x000000010bd08548 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010bd07766 llvm::sys::RunSignalHandlers() + 86
2  swift                    0x000000010bd08b99 SignalHandler(int) + 361
3  libsystem_platform.dylib 0x00007fffbc28fbba _sigtramp + 26
4  swift                    0x0000000109d9271f swift::LookUpConformanceInModule::operator()(swift::CanType, swift::Type, swift::ProtocolType*) const + 79
5  libsystem_c.dylib        0x00007fffbc116420 abort + 129
6  libsystem_c.dylib        0x00007fffbc0dd893 basename_r + 0
7  swift                    0x0000000109d80ab2 swift::NormalProtocolConformance::setTypeWitness(swift::AssociatedTypeDecl*, swift::Substitution const&, swift::TypeDecl*) const + 258
8  swift                    0x000000010999a260 swift::ModuleFile::finishNormalConformance(swift::NormalProtocolConformance*, unsigned long long) + 3680
9  swift                    0x0000000109d80903 swift::NormalProtocolConformance::resolveLazyInfo() const + 99
10 swift                    0x0000000109d7f8f5 swift::NormalProtocolConformance::getTypeWitnessSubstAndDecl(swift::AssociatedTypeDecl*, swift::LazyResolver*) const + 37
11 swift                    0x0000000109d7f899 swift::ProtocolConformance::getTypeWitnessSubstAndDecl(swift::AssociatedTypeDecl*, swift::LazyResolver*) const + 25
12 swift                    0x0000000109d80169 swift::ProtocolConformance::getTypeWitness(swift::AssociatedTypeDecl*, swift::LazyResolver*) const + 9
13 swift                    0x0000000109d92d10 getMemberForBaseType(llvm::function_ref<llvm::Optional<swift::ProtocolConformanceRef> (swift::CanType, swift::Type, swift::ProtocolType*)>, swift::Type, swift::Type, swift::AssociatedTypeDecl*, swift::Identifier, swift::OptionSet<swift::SubstFlags, unsigned int>) + 864
14 swift                    0x0000000109d968ea llvm::Optional<swift::Type> llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>::callback_fn<substType(swift::Type, llvm::function_ref<swift::Type (swift::SubstitutableType*)>, llvm::function_ref<llvm::Optional<swift::ProtocolConformanceRef> (swift::CanType, swift::Type, swift::ProtocolType*)>, swift::OptionSet<swift::SubstFlags, unsigned int>)::$_14>(long, swift::TypeBase*) + 554
15 swift                    0x0000000109d939c6 swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 166
16 swift                    0x0000000109d92e86 swift::Type::subst(swift::ModuleDecl*, llvm::DenseMap<swift::SubstitutableType*, swift::Type, llvm::DenseMapInfo<swift::SubstitutableType*>, llvm::detail::DenseMapPair<swift::SubstitutableType*, swift::Type> > const&, swift::OptionSet<swift::SubstFlags, unsigned int>) const + 134
17 swift                    0x0000000109ba7a60 swift::Type llvm::function_ref<swift::Type (swift::Type)>::callback_fn<swift::constraints::ConstraintSystem::simplifyType(swift::Type)::$_4>(long, swift::Type) + 720
18 swift                    0x0000000109d9a459 llvm::Optional<swift::Type> llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>::callback_fn<swift::Type::transform(llvm::function_ref<swift::Type (swift::Type)>) const::$_9>(long, swift::TypeBase*) + 25
19 swift                    0x0000000109d939c6 swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 166
20 swift                    0x0000000109d9440d swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 2797
21 swift                    0x0000000109d8b8f8 swift::Type::transform(llvm::function_ref<swift::Type (swift::Type)>) const + 40
22 swift                    0x0000000109ba1dc4 swift::constraints::ConstraintSystem::simplifyType(swift::Type) + 36
23 swift                    0x0000000109b63602 swift::constraints::ConstraintSystem::matchTypes(swift::Type, swift::Type, swift::constraints::ConstraintKind, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 1026
24 swift                    0x0000000109b6f6b8 swift::constraints::ConstraintSystem::addConstraintImpl(swift::constraints::ConstraintKind, swift::Type, swift::Type, swift::constraints::ConstraintLocatorBuilder, bool) + 136
25 swift                    0x0000000109b6864c swift::constraints::ConstraintSystem::addConstraint(swift::constraints::ConstraintKind, swift::Type, swift::Type, swift::constraints::ConstraintLocatorBuilder, bool) + 92
26 swift                    0x0000000109ba4d25 swift::constraints::ConstraintSystem::resolveOverload(swift::constraints::ConstraintLocator*, swift::Type, swift::constraints::OverloadChoice, swift::DeclContext*) + 2421
27 swift                    0x0000000109ba4229 swift::constraints::ConstraintSystem::addOverloadSet(swift::Type, llvm::ArrayRef<swift::constraints::OverloadChoice>, swift::DeclContext*, swift::constraints::ConstraintLocator*, swift::constraints::OverloadChoice*) + 233
28 swift                    0x0000000109b6bb33 swift::constraints::ConstraintSystem::simplifyMemberConstraint(swift::constraints::ConstraintKind, swift::Type, swift::DeclName, swift::Type, swift::DeclContext*, swift::FunctionRefKind, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 515
29 swift                    0x0000000109b58693 swift::constraints::ConstraintSystem::addValueMemberConstraint(swift::Type, swift::DeclName, swift::Type, swift::DeclContext*, swift::FunctionRefKind, swift::constraints::ConstraintLocatorBuilder) + 179
30 swift                    0x0000000109b58ced (anonymous namespace)::ConstraintGenerator::addMemberRefConstraints(swift::Expr*, swift::Expr*, swift::DeclName, swift::FunctionRefKind) + 237
31 swift                    0x0000000109b4ed18 swift::ASTVisitor<(anonymous namespace)::ConstraintGenerator, swift::Type, void, void, void, void, void>::visit(swift::Expr*) + 10216
32 swift                    0x0000000109b53e8a (anonymous namespace)::ConstraintWalker::walkToExprPost(swift::Expr*) + 58
33 swift                    0x0000000109d021e6 swift::Expr::walk(swift::ASTWalker&) + 118
34 swift                    0x0000000109b4b568 swift::constraints::ConstraintSystem::generateConstraints(swift::Expr*) + 264
35 swift                    0x0000000109b76aef swift::constraints::ConstraintSystem::solve(swift::Expr*&, swift::Type, swift::ExprTypeCheckListener*, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 79
36 swift                    0x0000000109be8db4 swift::TypeChecker::solveForExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem&, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 340
37 swift                    0x0000000109bebd6f swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 655
38 swift                    0x0000000109bef825 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, bool) + 389
39 swift                    0x0000000109befa05 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 277
40 swift                    0x0000000109c03e4c validatePatternBindingDecl(swift::TypeChecker&, swift::PatternBindingDecl*, unsigned int) + 876
41 swift                    0x0000000109c0072e (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 334
42 swift                    0x0000000109c005d4 swift::TypeChecker::typeCheckDecl(swift::Decl*, bool) + 100
43 swift                    0x0000000109c5f0b5 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 405
44 swift                    0x0000000109c5e459 (anonymous namespace)::StmtChecker::typeCheckBody(swift::BraceStmt*&) + 25
45 swift                    0x0000000109c5d79a swift::TypeChecker::typeCheckFunctionBodyUntil(swift::FuncDecl*, swift::SourceLoc) + 394
46 swift                    0x0000000109c5d5b4 swift::TypeChecker::typeCheckAbstractFunctionBodyUntil(swift::AbstractFunctionDecl*, swift::SourceLoc) + 132
47 swift                    0x0000000109c5e1aa swift::TypeChecker::typeCheckAbstractFunctionBody(swift::AbstractFunctionDecl*) + 250
48 swift                    0x0000000109c70cf9 typeCheckFunctionsAndExternalDecls(swift::TypeChecker&) + 201
49 swift                    0x0000000109c71aa4 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int) + 1572
50 swift                    0x00000001098a1919 swift::CompilerInstance::performSema() + 3753
51 swift                    0x0000000108ecdedf swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4255
52 swift                    0x0000000108e8b9b0 main + 3312
53 libdyld.dylib            0x00007fffbc082255 start + 1
54 libdyld.dylib            0x00000000000000d5 start + 1140317825

I'm not having much luck minimizing that other one (partially due to this segfault blocking me), so if they look like the same issue that'd be convenient for me 😃

@belkadan
Copy link
Contributor

belkadan commented Mar 7, 2017

FWIW, I'm not seeing this on master.

@belkadan
Copy link
Contributor

belkadan commented Mar 7, 2017

@swift-ci create

@belkadan
Copy link
Contributor

belkadan commented Mar 7, 2017

Unfortunately I suspect I'm not seeing this on master because DougG and HuonW have been actively and heavily changing this part of the compiler. That means there's probably not one obvious fix for Swift 3.1.

@belkadan
Copy link
Contributor

belkadan commented Mar 7, 2017

FWIW both backtraces look like the same issue to me. Unfortunately, I'm not sure there's a good fix. This just barely squeaked by our checking in 3.0, but wasn't implemented on solid ground; in 3.1 the compiler now cares a lot more about being able to build up the type of the generic function consistently. Neither Doug nor I can think of a good workaround, either.

(This ought to work again in Swift 4, even in Swift 3 compatibility mode, but that's little comfort right now.)

@swift-ci
Copy link
Collaborator Author

Comment by Stan Chang Khin Boon (JIRA)

I'm facing similar issue in Xcode 8.3.1…

public protocol RequestPoint {
    associatedtype Response : ResponsePoint
    
    var request: URLRequest { get }
}

public protocol ResponsePoint {
    associatedtype Payload
    
    var data: Data? { get }
    var response: URLResponse? { get }
    var error: Error? { get }
    
    var payload: Payload { get }
    var createdAt: Date { get }
    
    init<Request: RequestPoint>(requestPoint: Request, data optData: Data?, response optResponse: URLResponse?, error optError: Error?) throws where Request.Response == Self
}

public struct CustomResponse : ResponsePoint {
    
    public struct Payload {
    }
    
    public let data: Data?
    public let response: URLResponse?
    public let error: Error?
    
    public let payload: Payload
    public let createdAt: Date
    
    public init<Request: RequestPoint>(requestPoint: Request, data optData: Data?, response optResponse: URLResponse?, error optError: Error?) throws where Request.Response == CustomResponse {
        self.data = optData
        self.response = optResponse
        self.error = optError
        self.payload = Payload()
        self.createdAt = Date()
    }
}
0  swift                    0x000000010ec6aec7 PrintStackTraceSignalHandler(void*) + 39
1  swift                    0x000000010ec6a376 SignalHandler(int) + 646
2  libsystem_platform.dylib 0x00007fffb1478b3a _sigtramp + 26
3  libsystem_platform.dylib 0x00007fbc73282dc0 _sigtramp + 3252724384
4  swift                    0x000000010cb07d57 swift::ProtocolConformance::getTypeWitnessSubstAndDecl(swift::AssociatedTypeDecl*, swift::LazyResolver*) const + 39
5  swift                    0x000000010ca19142 swift::ArchetypeBuilder::addSameTypeRequirementToConcrete(swift::ArchetypeBuilder::PotentialArchetype*, swift::Type, swift::RequirementSource) + 2290
6  swift                    0x000000010ca1e9c0 swift::ArchetypeBuilder::addSameTypeRequirement(swift::Type, swift::Type, swift::RequirementSource) + 176
7  swift                    0x000000010ca21fef swift::ArchetypeBuilder::addGenericSignature(swift::GenericSignature*) + 271
8  swift                    0x000000010c6da000 swift::ModuleFile::finishNormalConformance(swift::NormalProtocolConformance*, unsigned long long) + 1696
9  swift                    0x000000010c735293 swift::serialization::Serializer::writeAllDeclsAndTypes() + 35619
10 swift                    0x000000010c73dc16 swift::serialization::Serializer::writeAST(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, bool) + 4886
11 swift                    0x000000010c748b78 swift::serialization::Serializer::writeToStream(llvm::raw_ostream&, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SILModule const*, swift::SerializationOptions const&) + 11224
12 swift                    0x000000010c74c472 void llvm::function_ref<void (llvm::raw_ostream&)>::callback_fn<swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::SILModule const*)::$_5>(long, llvm::raw_ostream&) + 226
13 swift                    0x000000010c74c690 withOutputFile(swift::ASTContext&, llvm::StringRef, llvm::function_ref<void (llvm::raw_ostream&)>) + 528
14 swift                    0x000000010c74c2bb swift::serialize(llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::SerializationOptions const&, swift::SILModule const*) + 283
15 swift                    0x000000010ba8bc1b swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 50203
16 swift                    0x000000010ba3957c main + 9052
17 libdyld.dylib            0x00007fffb1269235 start + 1
18 libdyld.dylib            0x0000000000000036 start + 1322872322

@DougGregor
Copy link
Member

I fixed all of the known issues with this in

2f167ab

and the various examples provided work now on master.

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 7, 2017

Comment by Stan Chang Khin Boon (JIRA)

Thanks @DougGregor. Just want to check this is not in Xcode 8.3.2 as I still face crashes though. 🙁

@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
Projects
None yet
Development

No branches or pull requests

4 participants