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-5422] Regression: Call to Optional flatMap/map segfault #47996

Closed
stephencelis opened this issue Jul 10, 2017 · 7 comments
Closed

[SR-5422] Regression: Call to Optional flatMap/map segfault #47996

stephencelis opened this issue Jul 10, 2017 · 7 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@stephencelis
Copy link
Contributor

Previous ID SR-5422
Radar None
Original Reporter @stephencelis
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 9 beta 3
(Some Xcode 8.3.3 issues noted, as well.)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @stephencelis
Priority Medium

md5: 69fb8bbd42d000a5ed52d2bd6b06b093

is blocked by:

  • SR-5251 Generic Sequence Function Compilation Fails

Issue Description:

The following causes a segfault in the type-checker:

extension Optional {
  public func apply<A>(_ f: ((Wrapped) -> A)?) -> A? {
    return self.flatMap { a in f.map { f in f(a) } }
  }
}

Trace:

0  swift                    0x00000001102317fa PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x0000000110230c36 SignalHandler(int) + 662
2  libsystem_platform.dylib 0x00007fffb9a3eb3a _sigtramp + 26
3  libsystem_platform.dylib 0x00007fb8149f9ac0 _sigtramp + 1526443936
4  swift                    0x000000010db7b9e2 std::__1::__function::__func<(anonymous namespace)::FailureDiagnosis::diagnoseTrailingClosureErrors(swift::ApplyExpr*)::$_0, std::__1::allocator<(anonymous namespace)::FailureDiagnosis::diagnoseTrailingClosureErrors(swift::ApplyExpr*)::$_0>, bool (swift::Type, swift::Type)>::operator()(swift::Type&&, swift::Type&&) + 258
5  swift                    0x000000010db683dc (anonymous namespace)::FailureDiagnosis::diagnoseClosureExpr(swift::ClosureExpr*, swift::Type, std::__1::function<bool (swift::Type, swift::Type)>) + 1260
6  swift                    0x000000010db6d739 (anonymous namespace)::FailureDiagnosis::visitApplyExpr(swift::ApplyExpr*) + 14377
7  swift                    0x000000010db499b9 swift::constraints::ConstraintSystem::diagnoseFailureForExpr(swift::Expr*) + 73
8  swift                    0x000000010db7ecf7 swift::constraints::ConstraintSystem::salvage(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::Expr*) + 6199
9  swift                    0x000000010dc471aa 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>) + 8858
10 swift                    0x000000010dc4aed4 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 692
11 swift                    0x000000010db5965b (anonymous namespace)::FailureDiagnosis::typeCheckChildIndependently(swift::Expr*, swift::Type, swift::ContextualTypePurpose, swift::OptionSet<TCCFlags, unsigned int>, swift::ExprTypeCheckListener*, bool) + 1963
12 swift                    0x000000010db683a0 (anonymous namespace)::FailureDiagnosis::diagnoseClosureExpr(swift::ClosureExpr*, swift::Type, std::__1::function<bool (swift::Type, swift::Type)>) + 1200
13 swift                    0x000000010db6d739 (anonymous namespace)::FailureDiagnosis::visitApplyExpr(swift::ApplyExpr*) + 14377
14 swift                    0x000000010db499b9 swift::constraints::ConstraintSystem::diagnoseFailureForExpr(swift::Expr*) + 73
15 swift                    0x000000010db7ecf7 swift::constraints::ConstraintSystem::salvage(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::Expr*) + 6199
16 swift                    0x000000010dc471aa 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>) + 8858
17 swift                    0x000000010dc4aed4 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 692
18 swift                    0x000000010dcce744 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 7764
19 swift                    0x000000010dccfe18 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 13608
20 swift                    0x000000010dccbcd8 swift::TypeChecker::typeCheckAbstractFunctionBodyUntil(swift::AbstractFunctionDecl*, swift::SourceLoc) + 1128
21 swift                    0x000000010dcd169b swift::TypeChecker::typeCheckAbstractFunctionBody(swift::AbstractFunctionDecl*) + 475
22 swift                    0x000000010dcef59a swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int) + 2490
23 swift                    0x000000010d825fa7 swift::CompilerInstance::performSema() + 5031
24 swift                    0x000000010cbb93b8 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1384
25 swift                    0x000000010cbb75e4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7332
26 swift                    0x000000010cb6cee8 main + 12248
27 libdyld.dylib            0x00007fffb982f235 start + 1
Stack dump:
0.  Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/stephencelis/Desktop/SwiftBug/SwiftBug/ViewController.swift /Users/stephencelis/Desktop/SwiftBug/SwiftBug/AppDelegate.swift -primary-file /Users/stephencelis/Desktop/SwiftBug/SwiftBug/Apply.swift -target x86_64-apple-ios11.0 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.0.sdk -I /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Products/Debug-iphonesimulator -F /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Products/Debug-iphonesimulator -enable-testing -g -module-cache-path /Users/stephencelis/Library/Developer/Xcode/DerivedData/ModuleCache -swift-version 4 -enforce-exclusivity=checked -D DEBUG -serialize-debugging-options -Xcc -I/Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/SwiftBug-generated-files.hmap -Xcc -I/Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/SwiftBug-own-target-headers.hmap -Xcc -I/Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/SwiftBug-all-target-headers.hmap -Xcc -iquote -Xcc /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/SwiftBug-project-headers.hmap -Xcc -I/Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Products/Debug-iphonesimulator/include -Xcc -I/Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/DerivedSources/x86_64 -Xcc -I/Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -working-directory/Users/stephencelis/Desktop/SwiftBug -emit-module-doc-path /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/Objects-normal/x86_64/Apply~partial.swiftdoc -serialize-diagnostics-path /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/Objects-normal/x86_64/Apply.dia -Onone -module-name SwiftBug -emit-module-path /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/Objects-normal/x86_64/Apply~partial.swiftmodule -emit-dependencies-path /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/Objects-normal/x86_64/Apply.d -emit-reference-dependencies-path /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/Objects-normal/x86_64/Apply.swiftdeps -o /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Build/Intermediates.noindex/SwiftBug.build/Debug-iphonesimulator/SwiftBug.build/Objects-normal/x86_64/Apply.o -index-store-path /Users/stephencelis/Library/Developer/Xcode/DerivedData/SwiftBug-bftxexkemfqqtedqttuedtceftui/Index/DataStore -index-system-modules 
1.  While type-checking 'apply(_:)' at /Users/stephencelis/Desktop/SwiftBug/SwiftBug/Apply.swift:2:10
2.  While type-checking expression at [/Users/stephencelis/Desktop/SwiftBug/SwiftBug/Apply.swift:3:12 - line:3:54] RangeText="self.flatMap { a in f.map { f_ in f_(a) } }"
3.  While type-checking expression at [/Users/stephencelis/Desktop/SwiftBug/SwiftBug/Apply.swift:3:32 - line:3:52] RangeText="f.map { f_ in f_(a) }"

(This compiles fine in previous versions of Xcode.)

The flipped version doesn't segfault, but fails to compile:

extension Optional {
  public func apply<A>(_ f: ((Wrapped) -> A)?) -> A? {
    return f.flatMap { f in self.map { a in f(a) } }
  }
}

Invalid conversion from throwing function of type '((((Wrapped) -> A)) throws -> _?) throws -> _?' to non-throwing function type '((((Wrapped) -> A)) throws -> A?) -> A?'

Adopting a point-free style also fails to compile:

extension Optional {
  public func apply<A>(_ f: ((Wrapped) -> A)?) -> A? {
    return self.flatMap { f.map($0) }
  }
}

Invalid conversion from throwing function of type '((((Wrapped) -> A)) throws -> _?) throws -> _?' to non-throwing function type '((((Wrapped) -> A)) throws -> A?) -> A?'

(This also used to compile.)

Meanwhile:

extension Optional {
  public func apply<A>(_ f: ((Wrapped) -> A)?) -> A? {
    return self.flatMap { f.map($0) }
  }
}

Xcode 9:

Cannot convert value of type 'Wrapped' to expected argument type '(((_) -> _)) throws -> _'

Xcode 8.3.3:

generic parameter 'U' could not be inferred

And:

extension Optional {
  public func apply<A>(_ f: ((Wrapped) -> A)?) -> A? {
    return f.flatMap(self.map)
    // or return self.flatMap(f.map)
  }
}

Xcode 9:

'flatMap' produces 'U?', not the expected contextual result type '_?'

Xcode 8.3.3:

call can throw, but it is not marked with 'try' and the error is not handled

@stephencelis
Copy link
Contributor Author

Related to https://bugs.swift.org/browse/SR-5251

@belkadan
Copy link
Contributor

Does not crash but fails to compile on master. @xedin?

@xedin
Copy link
Member

xedin commented Jul 11, 2017

I think this is related to #10881 which @rudkx just merged, and everything now compiles correctly on master.

@xedin
Copy link
Member

xedin commented Jul 18, 2017

@stephencelis Please verify.

@stephencelis
Copy link
Contributor Author

@xedin Big improvements! Most of these iterations compile, save for a couple that are giving throws diagnostics some issues:

return self.flatMap { f.map($0) }

Cannot convert value of type 'Wrapped' to expected argument type '(((_) -> _)) throws -> _'

return self.flatMap(f.map)

Call can throw, but it is not marked with 'try' and the error is not handled

@stephencelis
Copy link
Contributor Author

(Should I reopen? Or post another issue highlighting these two issues?)

@xedin
Copy link
Member

xedin commented Jul 25, 2017

@stephencelis Please open a separate issue for the examples you mentioned, that might be a different root cause...

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants