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-12723] Type compatibility check around @convention is not enough #55167

Closed
kateinoigakukun opened this issue May 2, 2020 · 2 comments
Closed
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

@kateinoigakukun
Copy link
Member

Previous ID SR-12723
Radar rdar://problem/62894044
Original Reporter @kateinoigakukun
Type Bug
Status Resolved
Resolution Done
Environment

Toolchain: swift-DEVELOPMENT-SNAPSHOT-2020-05-01-a
OS: macOS 10.15.4

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

md5: 970563cc96d0310cc02b3f2bb9f680b2

Issue Description:

The type checker says that `(@convention(c) () -> Void) -> Void` is compatible with `(() -> Void) -> Void` but actually not.

In this example, the wrong compatibility check allows capturing `a` in the closure called by C convention.

struct Functions {
    let cFunction: (@convention(c) () -> Void) -> Void
}

func proxy(_ f: (() -> Void) -> Void) {
    let a = 1
    f { print(a) }
}

func context() {

    let functions = Functions { app in
        app()
    }

    proxy(functions.cFunction)
}

context()

Output:

Assertion failed: (expectedFnType->getExtInfo().hasContext() || !fnType->getExtInfo().hasContext()), function transformFunction, file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/swift/lib/SILGen/SILGenPoly.cpp, line 4102.
Stack dump:
0.      Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2020-05-01-a.xctoolchain/usr/bin/swift -frontend -c -primary-file main.swift -target x86_64-apple-darwin19.4.0 -enable-objc-interop -color-diagnostics -module-name main -o /var/folders/br/7sr_p7cj7v549x_gtkb33df00000gp/T/main-034da3.o
1.      Apple Swift version 5.3-dev (LLVM a07d2929d8, Swift 2938466ecb)
2.      While evaluating request SILGenSourceFileRequest(SIL Generation for file "main.swift")
3.      While emitting SIL for 'context()' (at main.swift:10:1)
4.      While silgen emitFunction SIL function "@$s4main7contextyyF".
 for 'context()' (at main.swift:10:1)
5.      While emitting reabstraction thunk in SIL function "@$sItC_Iegy_Ig_Iegy_TR".
 for <<debugloc at "<compiler-generated>":0:0>>0  swift                    0x000000010c7d60e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010c7d5335 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010c7d66b6 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007fff6e6dc5fd _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603338663475744
5  libsystem_c.dylib        0x00007fff6e5b2808 abort + 120
6  libsystem_c.dylib        0x00007fff6e5b1ac6 err + 0
7  swift                    0x000000010c977363 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) (.cold.15) + 35
8  swift                    0x0000000108a0f35f (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 1935
9  swift                    0x0000000108a1b7cc (anonymous namespace)::TranslateArguments::translateSingle(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::ManagedValue, swift::SILParameterInfo) + 284
10 swift                    0x0000000108a1a5a7 (anonymous namespace)::TranslateArguments::translate(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType) + 2263
11 swift                    0x0000000108a1ae22 (anonymous namespace)::TranslateArguments::translate(llvm::ArrayRef<swift::Lowering::AbstractionPattern>, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, llvm::ArrayRef<swift::Lowering::AbstractionPattern>, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>) + 194
12 swift                    0x0000000108a13b1e (anonymous namespace)::TranslateArguments::translate(swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>) + 846
13 swift                    0x0000000108a18d35 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&) + 4613
14 swift                    0x0000000108a0f4bb (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lpowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 2283
15 swift                    0x0000000108a0ebb1 swift::Lowering::SILGenFunction::emitTransformedValue(swift::SILLocation, swift::Lowering::ManagedValue, swift::CanType, swift::CanType, swift::Lowering::SGFContext) + 321
16 swift                    0x00000001089bff00 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 30288
17 swift                    0x00000001089ae486 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 54
18 swift                    0x000000010896842b (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 2155
19 swift                    0x00000001089551b2 (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 450
20 swift                    0x00000001089676d3 (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 147
21 swift                    0x0000000108970c29 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 1321
22 swift                    0x0000000108959a04 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3204
23 swift                    0x00000001089573a5 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2277
24 swift                    0x00000001089b891a swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 106
25 swift                    0x00000001089ae891 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 913
26 swift                    0x0000000108a2ab80 (anonymous namespace)::StmtEmitter::visitBraceStmt(swift::BraceStmt*) + 320
27 swift                    0x0000000108a25185 swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 21
28 swift                    0x00000001089d2d97 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 359
29 swift                    0x000000010894de4c swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*)::$_3::operator()(swift::SILFunction*) const + 268
30 swift                    0x0000000108944745 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 677
31 swift                    0x000000010894b083 (anonymous namespace)::SILGenModuleRAII::emitSourceFile(swift::SourceFile*) + 851
32 swift                    0x000000010894ac86 swift::SILGenSourceFileRequest::evaluate(swift::Evaluator&, swift::SILGenDescriptor) const + 182
33 swift                    0x0000000108a24a91 swift::SimpleRequest<swift::SILGenSourceFileRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::SILGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::SILGenSourceFileRequest const&, swift::Evaluator&) + 33
34 swift                    0x0000000108950dc5 llvm::Expected<swift::SILGenSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::SILGenSourceFileRequest>(swift::SILGenSourceFileRequest const&) + 341
35 swift                    0x000000010894c506 llvm::Expected<swift::SILGenSourceFileRequest::OutputType> swift::Evaluator::operator()<swift::SILGenSourceFileRequest, (void*)0>(swift::SILGenSourceFileRequest const&) + 118
36 swift                    0x000000010894c415 swift::performSILGeneration(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 85
37 swift                    0x00000001085cfda2 performCompile(swift::CompilerInstance&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 8162
38 swift                    0x00000001085cce07 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3575
39 swift                    0x0000000108568a1d main + 861
40 libdyld.dylib            0x00007fff6e4e3cc9 start + 1
41 libdyld.dylib            0x000000000000000d start + 18446603338665542469
<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)
@beccadax
Copy link
Contributor

beccadax commented May 5, 2020

@swift-ci create

@LucianoPAlmeida
Copy link
Collaborator

@kateinoigakukun this is fixed on master, can you please verify on the latest snapshot? Thanks 🙂

@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