Passing a var to immediately called Swift closure causes segfault in Xcode 6.1 (6A1042b)

Originator:msealand
Number:rdar://18571392 Date Originated:10/7/2014
Status:Closed Resolved:02/10/2015
Product:Developer Tools Product Version:Xcode 6.1 (6A1042b)
Classification:Crash Reproducible:Always
 
Summary:
If a closure with an inferred type is created and immediately called (i.e. not assigned to a variable first) and is passed a variable defined as a var, it will trigger a compiler crash in Xcode 6.1 (6A1042b).

Steps to Reproduce:
1. Install Xcode 6.1 (6A1042b)
2. Create a new OS X command line tool project with its language set to Swift
3. Replace the code in main.swift with the following:

    var index = 1
    _ = {
        println($0)
    } (index)

4. Build the project

Expected Results:
A compiled binary

Actual Results:
Swift Compiler Error: Command failed due to signal: Segmentation fault: 11

0  swift                    0x0000000107c4bf68 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x0000000107c4c454 SignalHandler(int) + 452
2  libsystem_platform.dylib 0x00007fff97762f1a _sigtramp + 26
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 1753862400
4  swift                    0x000000010801025c swift::NominalTypeDecl::getMembers(bool) const + 28
5  swift                    0x00000001070a6fe8 (anonymous namespace)::LowerType::visitAnyStructType(swift::CanType, swift::StructDecl*) + 40
6  swift                    0x00000001070a4c3d swift::Lowering::TypeLowering const* swift::CanTypeVisitor<(anonymous namespace)::LowerType, swift::Lowering::TypeLowering const*>::visit<>(swift::CanType) + 125
7  swift                    0x00000001070a4ab7 swift::Lowering::TypeConverter::getTypeLoweringForUncachedLoweredType(swift::Lowering::TypeConverter::TypeKey) + 263
8  swift                    0x00000001070a43af swift::Lowering::TypeConverter::getTypeLowering(swift::Lowering::AbstractionPattern, swift::Type, unsigned int) + 1999
9  swift                    0x000000010706ebf4 (anonymous namespace)::DestructureInputs::visitType(swift::CanType, swift::CanType) + 52
10 swift                    0x000000010706e76c getSILFunctionType(swift::SILModule&, swift::CanType, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::AnyFunctionType::ExtInfo, (anonymous namespace)::Conventions const&) + 556
11 swift                    0x000000010706c6e8 getUncachedSILFunctionTypeForConstant(swift::SILModule&, swift::SILDeclRef, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::AnyFunctionType::Representation) + 776
12 swift                    0x000000010706c2d3 swift::Lowering::TypeConverter::getConstantInfo(swift::SILDeclRef) + 691
13 swift                    0x00000001070b992c swift::Lowering::SILGenModule::getFunction(swift::SILDeclRef, swift::ForDefinition_t) + 204
14 swift                    0x00000001070bc60b swift::SILFunction* swift::Lowering::SILGenModule::preEmitFunction<swift::AbstractClosureExpr>(swift::SILDeclRef, swift::AbstractClosureExpr*, swift::SILLocation) + 91
15 swift                    0x00000001070ba897 swift::Lowering::SILGenModule::emitClosure(swift::AbstractClosureExpr*) + 119
16 swift                    0x00000001070f5491 (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 97
17 swift                    0x00000001070e7d86 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 2870
18 swift                    0x00000001070f05dd swift::Lowering::

Version:
Xcode 6.1 (6A1042b)

Notes:
There are two workarounds:

1. Define the variable passed to the closure as a let:

    let index = 1
    _ = {
        println($0)
    } (index)

2. Explicitly define the type of the parameter in the closure:

    var index = 1
    _ = { (i: Int) in
        println(i)
    } (index)


Configuration:
iMac (27-inch, Late 2012)
3.4 GHz Intel Core i7
24 GB 1600 MHz DDR3
NVIDIA GeForce GTX 680MX 2048 MB

OS X Yosemite 10.10 (14A379a)

Comments

This is resolved in Xcode 6.3 beta (6D520o)


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!