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-7858] Compiler crash when applying escaping closure with a generic type parameter #50393

Closed
swift-ci opened this issue Jun 3, 2018 · 12 comments
Closed

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Jun 3, 2018

Previous ID SR-7858
Radar rdar://problem/40773543
Original Reporter guidomb (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

This crash can be reproduced with Swift 4.2 both on macOS and Ubuntu. I used

Swift 4.2 2018-05-12

Swift 4.2 2018-06-01

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash, SILGen
Assignee None
Priority Medium

md5: 7f925e1830d691068220ceeee2abd0e1

Issue Description:

Here is the stacktrace

Assertion failed: (isPlusOne(SGF)), function forwardInto, file /Users/buildnode/jenkins/workspace/oss-swift-4.2-package-osx/swift/lib/SILGen/ManagedValue.cpp, line 113.
0  swift                    0x0000000110759068 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x0000000110759776 SignalHandler(int) + 694
2  libsystem_platform.dylib 0x00007fff7424af5a _sigtramp + 26
3  libc++abi.dylib          0x00007fff71eef628 operator new(unsigned long) + 40
4  libsystem_c.dylib        0x00007fff73fe81ae abort + 127
5  libsystem_c.dylib        0x00007fff73fb01ac basename_r + 0
6  swift                    0x000000010d6beb4c swift::Lowering::ManagedValue::forwardInto(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::SILValue) + 332
7  swift                    0x000000010d7195c5 swift::Lowering::SILGenFunction::emitExistentialErasure(swift::SILLocation, swift::CanType, swift::Lowering::TypeLowering const&, swift::Lowering::TypeLowering const&, llvm::ArrayRef<swift::ProtocolConformanceRef>, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SGFContext)>, bool) + 789
8  swift                    0x000000010d790a92 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::SGFContext) + 13874
9  swift                    0x000000010d7990c5 (anonymous namespace)::TranslateArguments::translateIntoGuaranteed(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::ManagedValue) + 181
10 swift                    0x000000010d797959 (anonymous namespace)::TranslateArguments::translateSingle(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::ManagedValue, swift::SILParameterInfo) + 1177
11 swift                    0x000000010d792827 (anonymous namespace)::TranslateArguments::translate(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType) + 1911
12 swift                    0x000000010d79639e createThunk(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 1358
13 swift                    0x000000010d78e031 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::SGFContext) + 3025
14 swift                    0x000000010d78d40b swift::Lowering::SILGenFunction::emitTransformedValue(swift::SILLocation, swift::Lowering::ManagedValue, swift::CanType, swift::CanType, swift::Lowering::SGFContext) + 347
15 swift                    0x000000010d747142 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 20994
16 swift                    0x000000010d743888 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 6472
17 swift                    0x000000010d7371f9 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 57
18 swift                    0x000000010d6b99c1 swift::Lowering::ArgumentSource::getAsSingleValue(swift::Lowering::SILGenFunction&, swift::Lowering::SGFContext) && + 737
19 swift                    0x000000010d71ba75 swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 549
20 swift                    0x000000010d6b9b71 swift::Lowering::ArgumentSource::getConverted(swift::Lowering::SILGenFunction&, swift::Lowering::Conversion const&, swift::Lowering::SGFContext) && + 129
21 swift                    0x000000010d6f1c89 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 3417
22 swift                    0x000000010d6f0445 (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus) && + 453
23 swift                    0x000000010d6efcd7 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::CanTypeWrapper<swift::FunctionType>&, swift::Lowering::AbstractionPattern&, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&, swift::CanTypeWrapper<swift::FunctionType>&) + 1975
24 swift                    0x000000010d6dd0f9 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3401
25 swift                    0x000000010d6dc294 swift::Lowering::SILGenFunction::emitApplyExpr(swift::Expr*, swift::Lowering::SGFContext) + 1476
26 swift                    0x000000010d741f92 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 82
27 swift                    0x000000010d73155f swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional<swift::SILLocation>) + 319
28 swift                    0x000000010d7207f4 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 292
29 swift                    0x000000010d720a3d swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*) + 45
30 swift                    0x000000010d7a20c7 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 8743
31 swift                    0x000000010d7a1ce7 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 7751
32 swift                    0x000000010d7a2078 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 8664
33 swift                    0x000000010d79fe95 swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 21
34 swift                    0x000000010d759d7d swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 509
35 swift                    0x000000010d6d7217 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*)::$_1::operator()(swift::SILFunction*) const + 311
36 swift                    0x000000010d6cecd8 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 760
37 swift                    0x000000010d7ab7d5 (anonymous namespace)::SILGenType::visitFuncDecl(swift::FuncDecl*) + 21
38 swift                    0x000000010d7aa043 (anonymous namespace)::SILGenType::emitType() + 931
39 swift                    0x000000010d7a9c99 swift::Lowering::SILGenModule::visitNominalTypeDecl(swift::NominalTypeDecl*) + 25
40 swift                    0x000000010d6d3d1b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 827
41 swift                    0x000000010d6d4ad0 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 352
42 swift                    0x000000010d6d50df swift::performSILGeneration(swift::FileUnit&, swift::SILOptions&, llvm::Optional<unsigned int>) + 95
43 swift                    0x000000010cfb5471 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 8641
44 swift                    0x000000010cfb22a6 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3318
45 swift                    0x000000010cf6e6ed main + 2317
46 libdyld.dylib            0x00007fff73f3c015 start + 1
47 libdyld.dylib            0x0000000000000039 start + 2349613093
Stack dump:
0.    Program arguments: /Library/Developer/Toolchains/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-05-12-a.xctoolchain/usr/bin/swift -frontend -c /Users/guidomb/Documents/projects/wolox/BotEngine/Sources/TestKit/FixtureManager.swift -primary-file /Users/guidomb/Documents/projects/wolox/BotEngine/Sources/TestKit/MockGoogleAPIResourceExecutor.swift -emit-module-path /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/Objects-normal/x86_64/MockGoogleAPIResourceExecutor~partial.swiftmodule -emit-module-doc-path /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/Objects-normal/x86_64/MockGoogleAPIResourceExecutor~partial.swiftdoc -serialize-diagnostics-path /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/Objects-normal/x86_64/MockGoogleAPIResourceExecutor.dia -emit-dependencies-path /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/Objects-normal/x86_64/MockGoogleAPIResourceExecutor.d -emit-reference-dependencies-path /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/Objects-normal/x86_64/MockGoogleAPIResourceExecutor.swiftdeps -target x86_64-apple-macosx10.10 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -I /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Products/Debug -F /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Products/Debug -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -enable-testing -g -module-cache-path /Users/guidomb/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 4 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D Xcode -serialize-debugging-options -Xcc -I/Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/swift-overrides.hmap -Xcc -I/Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Products/Debug/include -Xcc -I/Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/DerivedSources/x86_64 -Xcc -I/Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/DerivedSources -Xcc -working-directory/Users/guidomb/Documents/projects/wolox/BotEngine -module-name TestKit -o /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Build/Intermediates.noindex/BotEngine.build/Debug/TestKit.build/Objects-normal/x86_64/MockGoogleAPIResourceExecutor.o -index-store-path /Users/guidomb/Library/Developer/Xcode/DerivedData/BotEngine-axetfyisbfwkcadwcheahozpsehr/Index/DataStore -index-system-modules 
1.    While emitting SIL for 'execute(resource:token:session:deserializer:)' at /Users/guidomb/Documents/projects/wolox/BotEngine/Sources/TestKit/MockGoogleAPIResourceExecutor.swift:20:12
2.    While silgen emitFunction SIL function "@$S7TestKit29MockGoogleAPIResourceExecutorC7execute8resource5token7session12deserializer13ReactiveSwift14SignalProducerVyx0D3APIALC12RequestErrorOGAM8ResourceVy_xG_AM5TokenVSo12NSURLSessionC6ResultAXOyxAX03AnyR0VG10Foundation4DataV_So17NSHTTPURLResponseCtctlF".
 for 'execute(resource:token:session:deserializer:)' at /Users/guidomb/Documents/projects/wolox/BotEngine/Sources/TestKit/MockGoogleAPIResourceExecutor.swift:20:12
3.    While emitting reabstraction thunk in SIL function "@$Ss5Error_p9GoogleAPIABC07RequestA0OIeggo_6Result03AnyA0VAEIeggo_TR".
 for <<debugloc at "<compiler-generated>":0:0>>

This is the file that makes the compiler crash

import Foundation
import GoogleAPI

public final class MockGoogleAPIResourceExecutor: GoogleAPIResourceExecutor {
    
    private let fixtureManager: FixtureManager
    var response: Data? = .none
    
    public init(fixturesDirectoryPath: String) {
        self.fixtureManager = FixtureManager(fixturesDirectoryPath: fixturesDirectoryPath)
    }
    
    public func execute<T>(
        resource: GoogleAPI.Resource<T>,
        token: GoogleAPI.Token,
        session: URLSession,
        deserializer: @escaping GoogleAPI.ResourceDeserializer<T>) -> GoogleAPI.ResourceProducer<T> {
        if let data = self.response {
            let url = URL(string: resource.urlPath)!
            guard let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "1.1", headerFields: [:]) else {
                fatalError("ERROR - Cannot create HTTURLResponse object")
            }
            let result = deserializer(data, response).mapError(GoogleAPI.RequestError.deserializationError)
            return GoogleAPI.ResourceProducer(result: result)
        } else {
            let error = GoogleAPI.ResourceError(
                code: 404,
                message: "There is no registered response. Maybe you forgot to call '\(setFixtureAsResourceResponse(fixturePath:))'",
                status: "NOT_AVAILABLE"
            )
            return GoogleAPI.ResourceProducer(error: .resourceError(error))
        }
    }
    
    public func setFixtureAsResourceResponse(fixturePath: String) {
        response = try! fixtureManager.loadFixture(in: fixturePath)
    }
    
}

And the exact line that produces the crash is

let result = deserializer(data, response).mapError(GoogleAPI.RequestError.deserializationError)

Changing that line to

let result = deserializer(data, response).mapError { GoogleAPI.RequestError.deserializationError($0) }

Avoids the compiler crash.

To give you more context about the types being used in this example

public typealias ResourceDeserializer<T> = (Data, HTTPURLResponse) -> Result<T, AnyError>

public enum RequestError: Error {
        
        case missingContentTypeHeader
        case unexpectedContentType(String)
        case unexpectedErrorContentType(contentType: String, statusCode: Int)
        case errorDataDeserializationError(error: Error, statusCode: Int)
        case unexpectedResponseStatusCode(Int)
        case unexpectedResponseObjectType(URLResponse)
        case deserializationError(Error)
        case networkingError(Error)
        case resourceError(ResourceError)
        
    }
@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Jun 3, 2018

Comment by Guido Marucci Blas (JIRA)

Here is a reduced version of the issue to easily reproduce it https://gist.github.com/guidomb/57b8ac391dfeb0514c9ae872b6574175

@belkadan
Copy link
Contributor

@belkadan belkadan commented Jun 4, 2018

@swift-ci create

@gottesmm
Copy link
Member

@gottesmm gottesmm commented Jun 8, 2018

I am going to fix this real quick. I just want to make the test case slightly smaller.

@gottesmm
Copy link
Member

@gottesmm gottesmm commented Jun 8, 2018

Keep in mind, I am not sure if this is a regression from the +0 work. We could have done this later and just not hit this problem.

The problem here is that we have a guaranteed parameter and we are trying to forward it into memory as a +1 value. So we are catching a real problem here.

@gottesmm
Copy link
Member

@gottesmm gottesmm commented Jun 8, 2018

Smaller:

class Klass {}

struct MyStruct1: Error {
  let b: Klass
}

enum Enum1<T2> {
  func enum1Func<FT1>(_ : (T2) -> FT1) -> Enum1<FT1> {}
}

protocol MyProtocol {
  func execute() -> Enum1<MyStruct1>
}

enum Enum2<T> {
  case bar(T)
}

struct Test {
  func testFunc(k: MyProtocol) -> Enum1<Enum2<Error>> {
    return k.execute().enum1Func(Enum2.bar)
  }
}

@gottesmm
Copy link
Member

@gottesmm gottesmm commented Jun 8, 2018

FYI pasted the wrong one. Got an even smaller version.

@gottesmm
Copy link
Member

@gottesmm gottesmm commented Jun 8, 2018

class Klass: Error {}

enum Enum<T> {
static func enum1Func(_ : (T) -> Enum<Error>) -> Enum<Error> {}
}

func bar<T>(t: T) -> Enum<T> {}

func testFunc() -> Enum<Error> {
return Enum<Klass>.enum1Func(bar)
}

@gottesmm
Copy link
Member

@gottesmm gottesmm commented Jun 8, 2018

(function_conversion_expr implicit type='(Klass) -> Foo<Error>' location=test.swift:11:31 range=[test.swift:11:31 - line:11:31]

(declref_expr type='(Error) -> Foo<Error>' location=test.swift:11:31 range=[test.swift:11:31 - line:11:31] decl=test.(file).bar(t🙂@test.swift:8:6 [with Error] function_ref=unapplied))

@gottesmm
Copy link
Member

@gottesmm gottesmm commented Jun 28, 2018

This was fixed by:

#17085

@AnnaZaks
Copy link
Mannequin

@AnnaZaks AnnaZaks mannequin commented Jul 26, 2018

guidomb (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 swift-ci commented Jul 29, 2018

Comment by Guido Marucci Blas (JIRA)

This is no longer an issue.

@AnnaZaks
Copy link
Mannequin

@AnnaZaks AnnaZaks mannequin commented Jul 30, 2018

Thanks! Updating the state to closed.

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

No branches or pull requests

3 participants