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-10305] Type Checker SegFault #52705

Closed
swift-ci opened this issue Apr 4, 2019 · 8 comments
Closed

[SR-10305] Type Checker SegFault #52705

swift-ci opened this issue Apr 4, 2019 · 8 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 5.0 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 4, 2019

Previous ID SR-10305
Radar rdar://problem/49609518
Original Reporter calebkleveter (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug, 5.0Regression, CompilerCrash, TypeChecker
Assignee calebkleveter (JIRA)
Priority Medium

md5: e4a5f8345ebf8ab1fdd4181d3edcf4df

is duplicated by:

  • SR-10493 compiler crasher on [Substring].map(Int.init)
  • SR-10561 Compiler crash Type Checker
  • SR-10784 Compiler crash: Segmentation fault

Issue Description:

The Swift 5 type checker is crashing with a segfault when I try to convert an optional Substring to an Int:

public struct Version {
    public init?(_ string: String) {
        let components: [Substring] = string.split(separator: ".")
        guard let major: Int = components.first.flatMap(Int.init) else { return nil }
    }
}

The program compiles if I map over the Substring array and convert them so Strings.

Here is the gist with the compiler output: https://gist.github.com/calebkleveter/e6a7f3943b50e022ecd2beefbed44a6e

The crash occurs in both Xcode and the Terminal.

@theblixguy
Copy link
Collaborator

This doesn't crash on 5.1 (near master), but it does crash for me on 5.0 (Xcode 10.2).

@weissi
Copy link
Member

weissi commented Apr 4, 2019

4.2

$ jw-swift-4.2 swiftc /tmp/test.swift
/tmp/test.swift:4:49: error: 'flatMap' produces 'U?', not the expected contextual result type 'Int?'
        guard let major: Int = components.first.flatMap(Int.init) else { return nil }
                                                ^

5.0:

$ jw-swift-5.0 swiftc /tmp/test.swift
Assertion failed: (AffectedDecl), function RequirementFailure, file /Users/buildnode/jenkins/workspace/oss-swift-5.0-package-osx/swift/lib/Sema/CSDiagnostics.h, line 172.
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-5.0-RELEASE.xctoolchain/usr/bin/swift -frontend -c -primary-file /tmp/test.swift -target x86_64-apple-darwin18.5.0 -enable-objc-interop -sdk /Volumes/Xcode11A301_m19A426z_i17A446z_t17J441z_w17R445z_b17P421z_32bitSupport_FastSim_Boost_37GB/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name test -o /var/folders/lj/qfjqv9_x6b972r7l6f49lvmh0000gn/T/test-556ccb.o 
1.  While type-checking 'init(_:)' (at /tmp/test.swift:2:12)
2.  While type-checking statement at [/tmp/test.swift:2:36 - line:5:5] RangeText="{
        let components: [Substring] = string.split(separator: ".")
        guard let major: Int = components.first.flatMap(Int.init) else { return nil }
    "
3.  While type-checking statement at [/tmp/test.swift:4:9 - line:4:85] RangeText="guard let major: Int = components.first.flatMap(Int.init) else { return nil "
4.  While type-checking expression at [/tmp/test.swift:4:32 - line:4:65] RangeText="components.first.flatMap(Int.init"
0  swift                    0x0000000107876245 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x00000001078754e5 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x0000000107876832 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff65365b5d _sigtramp + 29
4  swift                    0x0000000108b8c9a1 cmark_strbuf__initbuf + 131204
5  libsystem_c.dylib        0x00007fff652256a6 abort + 127
6  libsystem_c.dylib        0x00007fff651ee20d basename_r + 0
7  swift                    0x0000000104bce7da swift::constraints::RequirementFailure::RequirementFailure(swift::constraints::ConstraintSystem&, swift::Expr*, swift::RequirementKind, swift::constraints::ConstraintLocator*) + 330
8  swift                    0x0000000104bcdae7 swift::constraints::MissingConformance::diagnose(swift::Expr*, bool) const + 55
9  swift                    0x0000000104b52a14 swift::Expr* llvm::function_ref<swift::Expr* (swift::Expr*)>::callback_fn<swift::constraints::ConstraintSystem::applySolutionFixes(swift::Expr*, swift::constraints::Solution const&)::$_11>(long, swift::Expr*) + 244
10 swift                    0x0000000104fd7cae swift::Expr::forEachChildExpr(llvm::function_ref<swift::Expr* (swift::Expr*)>)::ChildWalker::walkToExprPre(swift::Expr*) + 14
11 swift                    0x0000000104f6e199 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Expr*) + 201
12 swift                    0x0000000104f70b4f (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) + 207
13 swift                    0x0000000104f6da84 swift::Expr::walk(swift::ASTWalker&) + 84
14 swift                    0x0000000104fca303 swift::Expr::forEachChildExpr(llvm::function_ref<swift::Expr* (swift::Expr*)>) + 51
15 swift                    0x0000000104b32478 swift::constraints::ConstraintSystem::applySolutionFixes(swift::Expr*, swift::constraints::Solution const&) + 536
16 swift                    0x0000000104b326bb swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::Expr*, swift::Type, bool, bool) + 59
17 swift                    0x0000000104c65b47 swift::TypeChecker::typeCheckExpressionImpl(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener&, swift::constraints::ConstraintSystem*) + 1159
18 swift                    0x0000000104c6719d swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, bool) + 397
19 swift                    0x0000000104c678e5 swift::TypeChecker::typeCheckStmtCondition(llvm::MutableArrayRef<swift::StmtConditionElement>&, swift::DeclContext*, swift::Diag<>) + 357
20 swift                    0x0000000104cfc23b swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 2363
21 swift                    0x0000000104cfe518 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 136
22 swift                    0x0000000104cfba08 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 264
23 swift                    0x0000000104cfad18 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
24 swift                    0x0000000104cf9cae swift::TypeChecker::typeCheckConstructorBodyUntil(swift::ConstructorDecl*, swift::SourceLoc) + 718
25 swift                    0x0000000104cfa3df swift::TypeChecker::typeCheckAbstractFunctionBody(swift::AbstractFunctionDecl*) + 271
26 swift                    0x0000000104d1b6a2 typeCheckFunctionsAndExternalDecls(swift::SourceFile&, swift::TypeChecker&) + 290
27 swift                    0x0000000104d1c449 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int) + 1065
28 swift                    0x00000001044d7ce6 swift::CompilerInstance::parseAndTypeCheckMainFileUpTo(swift::SourceFile::ASTStage_t, swift::PersistentParserState&, swift::DelayedParsingCallbacks*, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>) + 502
29 swift                    0x00000001044d6663 swift::CompilerInstance::parseAndCheckTypesUpTo(swift::CompilerInstance::ImplicitImports const&, swift::SourceFile::ASTStage_t) + 435
30 swift                    0x00000001044d5d17 swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 615
31 swift                    0x0000000103cb6188 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1192
32 swift                    0x0000000103cb4c78 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3016
33 swift                    0x0000000103c66d52 main + 690
34 libdyld.dylib            0x00007fff651803d5 start + 1
35 libdyld.dylib            0x000000000000000f start + 2598894651
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)

@theblixguy
Copy link
Collaborator

5.1

error: initializer 'init(exactly:)' requires that 'Substring' conform to 'BinaryInteger'
        guard let major: Int = components.first.flatMap(Int.init) else { return nil }
                                                            ^
Swift.SignedInteger:3:23: note: where 'T' = 'Substring'
    @inlinable public init?<T>(exactly source: T) where T : BinaryInteger
                      ^

@belkadan
Copy link
Contributor

belkadan commented Apr 4, 2019

I don't think Int has an initializer from Substring (or StringProtocol), but it shouldn't crash. Thanks, Caleb.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 4, 2019

Comment by Caleb Kleveter (JIRA)

Alright, that's what I suspected.

@xedin
Copy link
Member

xedin commented Apr 25, 2019

This should be addressed by one of my PRs in 5.1/master and produces diagnostic @theblixguy mentioned. Unfortunately the change didn't make it in time for 5.0 release...

Please verify using the latest 5.1/master snapshot.

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Aug 9, 2019

calebkleveter (JIRA User), Could you verify if the problem is fixed and if so move the JIRA to "Closed"?

Thanks!
Anna

@swift-ci
Copy link
Collaborator Author

swift-ci commented Aug 9, 2019

Comment by Caleb Kleveter (JIRA)

Yep! I get the expected compiler error now.

@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 5.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

6 participants