Should this compile?

The following code doesn't compile. Before submitting a bug, I wanted to check here to make sure that I'm not missing something obvious.

func outer() {

    var value = 0

    func inner(n: Int = value) {}

    inner()
}

Compilation succeeds if you remove the invocation of inner.

Can you add which Swift version you're using, and what kind of error you're seeing? That compiles and runs without error for me in Swift 5.1.

In Xcode 11.3.1, the compiler crashes.

0 swift 0x0000000107f18a63 PrintStackTraceSignalHandler(void*) + 51
1 swift 0x0000000107f18236 SignalHandler(int) + 358
2 libsystem_platform.dylib 0x00007fff63e7642d _sigtramp + 29
3 libsystem_platform.dylib 0x00007ffeec1381b0 _sigtramp + 2284592544
4 libsystem_c.dylib 0x00007fff63d4ba1c abort + 120
5 swift 0x0000000103b59530 std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_deleteswift::DiagnosticConsumer > llvm::function_ref<std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_deleteswift::DiagnosticConsumer > (swift::InputFile const&)>::callback_fn<createSerializedDiagnosticConsumerIfNeeded(swift::FrontendInputsAndOutputs const&)::$_15>(long, swift::InputFile const&) + 0
6 swift 0x0000000107e91886 llvm::report_fatal_error(llvm::Twine const&, bool) + 278
7 swift 0x0000000107e9176b llvm::report_fatal_error(char const*, bool) + 43
8 swift 0x0000000107e51a4c (anonymous namespace)::VerifierLegacyPass::doFinalization(llvm::Module&) + 204
9 swift 0x0000000107df2fd3 llvm::FPPassManager::doFinalization(llvm::Module&) + 51
10 swift 0x0000000107dfb622 llvm::legacy::FunctionPassManagerImpl::doFinalization(llvm::Module&) + 82
11 swift 0x0000000103d728d5 swift::performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine*, llvm::sys::SmartMutex, llvm::GlobalVariable, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 4677
12 swift 0x0000000103b62fc2 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 38274
13 swift 0x0000000103b56234 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
14 swift 0x0000000103ae3733 main + 1219
15 libdyld.dylib 0x00007fff63c7d7fd start + 1
16 libdyld.dylib 0x0000000000000057 start + 2620926043
error: Abort trap: 6 (in target 'Xit' from project 'Xit')

Yes, sorry, I should've included that from the outset.

Xcode 11.3, Swift 5.1

Global is external, but doesn't have external or weak linkage!
i8* ()* @"$s13CompilerError5outeryyF5valueL_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!
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/clay/Library/Autosave Information/CompilerError/CompilerError/main.swift -emit-module-path /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/Objects-normal/x86_64/main~partial.swiftmodule -emit-module-doc-path /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/Objects-normal/x86_64/main~partial.swiftdoc -serialize-diagnostics-path /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/Objects-normal/x86_64/main.dia -emit-dependencies-path /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/Objects-normal/x86_64/main.d -emit-reference-dependencies-path /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/Objects-normal/x86_64/main.swiftdeps -target x86_64-apple-macos10.15 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -I /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Products/Debug -F /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Products/Debug -enable-testing -g -module-cache-path /Users/clay/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -serialize-debugging-options -Xcc -working-directory -Xcc /Users/clay/Library/Autosave Information/CompilerError -enable-anonymous-context-mangled-names -Xcc -I/Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/CompilerError-generated-files.hmap -Xcc -I/Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/CompilerError-own-target-headers.hmap -Xcc -I/Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/CompilerError-all-target-headers.hmap -Xcc -iquote -Xcc /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/CompilerError-project-headers.hmap -Xcc -I/Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Products/Debug/include -Xcc -I/Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/DerivedSources-normal/x86_64 -Xcc -I/Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/DerivedSources/x86_64 -Xcc -I/Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/DerivedSources -Xcc -DDEBUG=1 -module-name CompilerError -o /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Build/Intermediates.noindex/CompilerError.build/Debug/CompilerError.build/Objects-normal/x86_64/main.o -index-store-path /Users/clay/Library/Developer/Xcode/DerivedData/CompilerError-fpjwnmgbtiadxgagufwyarpaathc/Index/DataStore -index-system-modules 
0  swift                    0x0000000110c0da63 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x0000000110c0d236 SignalHandler(int) + 358
2  libsystem_platform.dylib 0x00007fff6c89d42d _sigtramp + 29
3  libsystem_platform.dylib 0x00007ffee34422b0 _sigtramp + 1991921312
4  libsystem_c.dylib        0x00007fff6c772a1c abort + 120
5  swift                    0x000000010c84f5b0 std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_delete<swift::DiagnosticConsumer> > llvm::function_ref<std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_delete<swift::DiagnosticConsumer> > (swift::InputFile const&)>::callback_fn<createSerializedDiagnosticConsumerIfNeeded(swift::FrontendInputsAndOutputs const&)::$_15>(long, swift::InputFile const&) + 0
6  swift                    0x0000000110b86886 llvm::report_fatal_error(llvm::Twine const&, bool) + 278
7  swift                    0x0000000110b8676b llvm::report_fatal_error(char const*, bool) + 43
8  swift                    0x0000000110b46a4c (anonymous namespace)::VerifierLegacyPass::doFinalization(llvm::Module&) + 204
9  swift                    0x0000000110ae7fd3 llvm::FPPassManager::doFinalization(llvm::Module&) + 51
10 swift                    0x0000000110af0622 llvm::legacy::FunctionPassManagerImpl::doFinalization(llvm::Module&) + 82
11 swift                    0x000000010ca68955 swift::performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine*, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 4677
12 swift                    0x000000010c859042 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 38274
13 swift                    0x000000010c84c2b4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
14 swift                    0x000000010c7d97b3 main + 1219
15 libdyld.dylib            0x00007fff6c6a47fd start + 1
16 libdyld.dylib            0x0000000000000047 start + 2476062795
error: Abort trap: 6 (in target 'CompilerError' from project 'CompilerError')

Ah, it looks like I'm on the 5.2-dev toolchain, not 5.1. So it may already be fixed!

This is fixed in 5.2. It’s actually in the changelog: https://github.com/apple/swift/blob/master/CHANGELOG.md (see SR-2189).

1 Like

Fantastic. That was incredibly timely. Thanks guys.

Terms of Service

Privacy Policy

Cookie Policy