Skip to content

[SR-8193] Compiler crashes on local function with default argument which captures a local var #50725

@ole

Description

@ole
Previous ID SR-8193
Radar None
Original Reporter @ole
Type Bug
Status Resolved
Resolution Duplicate
Environment

Swift 4.2 from Xcode 10.0 beta 3.
macOS 10.13.5

Apple Swift version 4.2 (swiftlang-1000.0.25.1 clang-1000.10.28.1)
Target: x86_64-apple-darwin17.6.0

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

md5: fefd7d047e606e6914d050fd08467d94

duplicates:

  • SR-2189 Nested function with local default value crashes

Issue Description:

This code crashes the compiler:

// default_arg.swift
func parse(_ input: String) {
    var counter = 0

    func localFunc(_ c: Int = counter) {
        print("Processing character #\(c)")
    }

    for _ in input {
        localFunc()
        counter += 1
    }
}

parse("Hello world")

The issue is that the default argument in localFunc "captures" the local variable counter. I'm not even sure if this should be allowed or should be a compile error, but the compiler should not crash. Removing the default argument or calling localFunc(counter) with an explicit argument fixes the crash.

This is the backtrace:

$ swift --version
Apple Swift version 4.2 (swiftlang-1000.0.25.1 clang-1000.10.28.1)
Target: x86_64-apple-darwin17.6.0

$ swift default_arg.swift
Global is external, but doesn't have external or weak linkage!
i8* ()* @"$S11default_arg5parseyySSF7counterL_Sivau"
<unknown>:0: error: fatal error encountered during compilation; please file a bug report with your project and the crash log
<unknown>:0: note: Broken module found, compilation aborted!
0  swift                    0x0000000109d424aa PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x0000000109d418b6 SignalHandler(int) + 966
2  libsystem_platform.dylib 0x00007fff68fbcf5a _sigtramp + 26
3  libsystem_platform.dylib 0x00007ffee9c69d68 _sigtramp + 2160774696
4  libsystem_c.dylib        0x00007fff68d5a1ae abort + 127
5  swift                    0x00000001060126b9 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_1::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 521
6  swift                    0x0000000109cf5778 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
7  swift                    0x0000000109cf565b llvm::report_fatal_error(char const*, bool) + 43
8  swift                    0x0000000109cbd53c (anonymous namespace)::VerifierLegacyPass::doFinalization(llvm::Module&) + 204
9  swift                    0x0000000109c63a33 llvm::FPPassManager::doFinalization(llvm::Module&) + 83
10 swift                    0x0000000109c68e27 llvm::legacy::FunctionPassManagerImpl::doFinalization(llvm::Module&) + 71
11 swift                    0x00000001061d22a5 swift::performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine*, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 4533
12 swift                    0x00000001061d77a4 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**, unsigned int) + 2516
13 swift                    0x00000001061d4167 swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::GlobalVariable**) + 727
14 swift                    0x000000010601e9e7 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 47975
15 swift                    0x000000010600f985 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7733
16 swift                    0x0000000105fb9028 main + 13144
17 libdyld.dylib            0x00007fff68cae015 start + 1
18 libdyld.dylib            0x000000000000000a start + 2536841206
Stack dump:
0.  Program arguments: /Applications/Xcode-10b3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret default_arg.swift -enable-objc-interop -sdk /Applications/Xcode-10b3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name default_arg
fish: 'swift default_arg.swift' terminated by signal SIGABRT (Abort)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions