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-27] Passing variadic function as a function parameter crashes compiler with SIGSEGV #42650

Closed
swift-ci opened this issue Dec 4, 2015 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 4, 2015

Previous ID SR-27
Radar None
Original Reporter nate (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

El Capitan, late 2013 Retina MacBook Pro

Apple Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f829)
Target: x86_64-apple-macosx10.9

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

md5: df05c318f165adcec0ebd665df8e3438

relates to:

  • SR-41 Covariance does not work for functions with variadic arguments

Issue Description:

If I define a variadic function:

func variadic(integers: Int...) {}

I would expect this to be acceptable anywhere Int -> (), (Int, Int) -> (), etc. were. For example:

func variadic(integers: Int...) {}

[1].forEach(variadic)
[(1, 2)].forEach(variadic)

However, the compiler crashes with this error:

0  swift                    0x00000001043da47b llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 43
1  swift                    0x00000001043d9916 llvm::sys::RunSignalHandlers() + 70
2  swift                    0x00000001043daaa3 SignalHandler(int) + 243
3  libsystem_platform.dylib 0x00007fff9573052a _sigtramp + 26
4  swift                    0x000000010235e6fe swift::Lowering::TypeConverter::find(swift::Lowering::TypeConverter::TypeKey) + 142
5  swift                    0x00000001023c0b09 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 4393
6  swift                    0x00000001023bc047 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*) + 279
7  swift                    0x000000010239c2ae (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 1950
8  swift                    0x000000010239c835 (anonymous namespace)::ArgEmitter::emitExpanded(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 261
9  swift                    0x000000010239c689 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 2937
10 swift                    0x000000010239d7dd (anonymous namespace)::ArgEmitter::emitShuffle(swift::Expr*, swift::Expr*, llvm::ArrayRef<swift::TupleTypeElt>, swift::ConcreteDeclRef, llvm::ArrayRef<swift::Expr*>, llvm::ArrayRef<int>, llvm::ArrayRef<unsigned int>, swift::Type, swift::Lowering::AbstractionPattern) + 2429
11 swift                    0x000000010239cdd5 (anonymous namespace)::ArgEmitter::emitExpanded(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 1701
12 swift                    0x000000010239c689 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 2937
13 swift                    0x000000010239b55b (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<std::__1::pair<swift::Lowering::LValue, swift::SILLocation> >&, llvm::Optional<swift::ForeignErrorConvention> const&) && + 315
14 swift                    0x00000001023900ba (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2698
15 swift                    0x000000010238ee0a swift::Lowering::SILGenFunction::emitApplyExpr(swift::Expr*, swift::Lowering::SGFContext) + 58
16 swift                    0x00000001023bfae8 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 264
17 swift                    0x00000001023bfbb9 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 473
18 swift                    0x00000001023bea0f swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 47
19 swift                    0x000000010237dbd4 swift::Lowering::ArgumentSource::getAsSingleValue(swift::Lowering::SILGenFunction&, swift::Lowering::SGFContext) && + 340
20 swift                    0x000000010239c563 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 2643
21 swift                    0x000000010239b55b (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<std::__1::pair<swift::Lowering::LValue, swift::SILLocation> >&, llvm::Optional<swift::ForeignErrorConvention> const&) && + 315
22 swift                    0x00000001023900ba (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2698
23 swift                    0x000000010238ee0a swift::Lowering::SILGenFunction::emitApplyExpr(swift::Expr*, swift::Lowering::SGFContext) + 58
24 swift                    0x00000001023bfae8 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 264
25 swift                    0x00000001023bec96 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 486
26 swift                    0x0000000102387de6 swift::Lowering::SILGenModule::visitTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 182
27 swift                    0x000000010238841b swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 827
28 swift                    0x0000000102388e62 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool, bool) + 802
29 swift                    0x0000000102389216 swift::performSILGeneration(swift::ModuleDecl*, swift::SILOptions&, bool, bool) + 38
30 swift                    0x00000001021cb390 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&) + 11424
31 swift                    0x00000001021c84e3 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 2691
32 swift                    0x00000001021c4285 main + 2821
33 libdyld.dylib            0x00007fff98c265ad start + 1
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swift -frontend -interpret test.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -color-diagnostics -module-name test
fish: Job 1, '/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swift test.swift' terminated by signal SIGSEGV (Address boundary error)

If this is not currently intended to permissible in the language, the crash should be fixed and a compiler error should be printed instead. For example:

func variadic(integers: Int...) {}

struct Variadic
{
    let function: Int -> ()
}

Variadic(function: variadic)

Prints the error:

test.swift:8:20: error: cannot convert value of type '(Int...) -> ()' to expected argument type 'Int -> ()'
Variadic(function: variadic)
@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 4, 2015

Comment by Nikita Leonov (JIRA)

I had the same issue reported as radar, and duplicated it here as well SR-41. Sorry for duplication, as I didn't expect this issue to be so popular and already reported by someone else. Apparently there was one already reported.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 7, 2015

Comment by Steven Downum (JIRA)

This is probably not unexpected considering the nature of the issue, but I was able to yield a very similar result under Ubuntu 14.04:

  6> func variadic(numbers: Int...) {}
  7> [1].forEach(variadic)
lldb: /home/buildslave/jenkins/workspace/oss-swift-linux-packages-ubuntu_14_04-one-off-build/swift/lib/SILGen/SILGenExpr.cpp:1799: void emitTupleShuffleExprInto((anonymous namespace)::RValueEmitter &, swift::TupleShuffleExpr *, swift::Lowering::Initialization *): Assertion `innerMapping >= 0 && "non-argument tuple shuffle with default arguments or variadics?"' failed.
Aborted

I can add to SR-41, if desired.

@CodaFi
Copy link
Member

CodaFi commented Jul 11, 2016

This one is resolved in master by the type error.

Variadics are a distinct type from "ground types". If you want to pass them around, you have to explicitly annotate the function as accepting that kind of type. Swift's type system does not support arity-generic overloads like you suspect it should here (though this may be something you could go through the evolution process to ask for if you think it is a useful feature). If you need to invoke these variadic functions, you must explicitly apply the necessary number of arguments. For example:

func giveMeVoid(callback: (Void)->Void) {
  
}

func giveMeInt(callback: (Int)->Void) {
  
}

func any(values:Any?...) {
  
}

any()
any(values: 5)

giveMeVoid { any() }
giveMeInt { any(values: $0) } 

If you need to pass these functions, then you can annotate the types of each function as accepting a variadic number of arguments and we'll figure out the rest for you (but you must maintain invariance [as though they were an [Blah] instead of Blah...])

func giveMeInt(callback: (Int...)->Void) {
  
}

func any(values:Int...) {
  
}

giveMeInt(any) 

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

No branches or pull requests

2 participants