Skip to content

Compiler crashes when calling function from another file which uses global variable #81818

Open
@Artem-Goldenberg

Description

@Artem-Goldenberg

Description

When building a simple executable target with main.swift which imports Foundation and defines global variable via let and a function which uses that global variable, when that function is called from another file, the crash happens.

Reproduction

Create new swift package with a single executable target and files:
main.swift
Ext.swift
Some.swift

// main.swift

import Foundation


let programText = String(data: FileHandle.standardInput.availableData, encoding: .utf8)
guard let programText else {
    quit(with: "Failed to read Stella program from the standard input")
}


func foo(_ b: Ext) -> Bool {
    enabledExtension.contains(b)
}


let enabledExtension: [Ext] = [.a]


func quit(with message: String) -> Never {
    print(message)
    exit(EXIT_FAILURE)
}
// Some.swift
let res = foo(.a)
// Ext.swift

enum Ext: String {
    case a = "some"
    case some = "more"
}

try to run swift build

it crashes with a stack dump

attaching the code archive:

MyTool.zip

Stack dump

Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/goldenberg/Desktop/MyTool/Sources/Ext.swift -primary-file /Users/goldenberg/Desktop/MyTool/Sources/Some.swift /Users/goldenberg/Desktop/MyTool/Sources/main.swift -emit-dependencies-path /Users/goldenberg/Desktop/MyTool/.build/x86_64-apple-macosx/debug/MyTool.build/Some.d -emit-reference-dependencies-path /Users/goldenberg/Desktop/MyTool/.build/x86_64-apple-macosx/debug/MyTool.build/Some.swiftdeps -target x86_64-apple-macosx10.13 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -I /Users/goldenberg/Desktop/MyTool/.build/x86_64-apple-macosx/debug/Modules -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /Users/goldenberg/Desktop/MyTool/.build/x86_64-apple-macosx/debug/ModuleCache -swift-version 6 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -entry-point-function-name MyTool_main -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/goldenberg/Desktop/MyTool -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC -Xcc -g -module-name MyTool -package-name mytool -disable-clang-spi -target-sdk-version 15.2 -target-sdk-name macosx15.2 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /Users/goldenberg/Desktop/MyTool/.build/x86_64-apple-macosx/debug/MyTool.build/Some.swift.o -index-store-path /Users/goldenberg/Desktop/MyTool/.build/x86_64-apple-macosx/debug/index/store -index-system-modules
1.	Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/goldenberg/Desktop/MyTool/Sources/Some.swift")
4.	While getting lowered local captures at 'foo(_:)' (at /Users/goldenberg/Desktop/MyTool/Sources/main.swift:15:1)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000112ed0878 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 40
1  swift-frontend           0x0000000112ece649 llvm::sys::RunSignalHandlers() + 233
2  swift-frontend           0x0000000112ed0e36 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007ff81c580e1d _sigtramp + 29
4  libsystem_platform.dylib 0x00006000027a82a0 _sigtramp + 18446708923263251616
5  swift-frontend           0x000000010d6a8ba3 std::__1::__function::__func<swift::Lowering::TypeConverter::getLoweredLocalCaptures(swift::SILDeclRef)::$_2, std::__1::allocator<swift::Lowering::TypeConverter::getLoweredLocalCaptures(swift::SILDeclRef)::$_2>, void (swift::AnyFunctionRef)>::operator()(swift::AnyFunctionRef&&) + 483
6  swift-frontend           0x000000010d6a92c4 std::__1::__function::__func<swift::Lowering::TypeConverter::getLoweredLocalCaptures(swift::SILDeclRef)::$_3, std::__1::allocator<swift::Lowering::TypeConverter::getLoweredLocalCaptures(swift::SILDeclRef)::$_3>, void (swift::SILDeclRef)>::operator()(swift::SILDeclRef&&) + 772
7  swift-frontend           0x000000010d697fb4 swift::Lowering::TypeConverter::getLoweredLocalCaptures(swift::SILDeclRef) + 1028
8  swift-frontend           0x000000010d80122b (anonymous namespace)::SILGenApply::visitDeclRefExpr(swift::DeclRefExpr*) + 2043
9  swift-frontend           0x000000010d7e71f9 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 345
10 swift-frontend           0x000000010d878771 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 673
11 swift-frontend           0x000000010d8662d8 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 120
12 swift-frontend           0x000000010d84dfc0 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 2368
13 swift-frontend           0x000000010d8aa20a swift::Lowering::SILGenFunction::emitLazyGlobalInitializer(swift::PatternBindingDecl*, unsigned int) + 586
14 swift-frontend           0x000000010d7d3802 swift::Lowering::SILGenModule::emitLazyGlobalInitializer(llvm::StringRef, swift::PatternBindingDecl*, unsigned int) + 1218
15 swift-frontend           0x000000010d8a9b51 swift::Lowering::SILGenModule::emitGlobalInitialization(swift::PatternBindingDecl*, unsigned int) + 1041
16 swift-frontend           0x000000010d7cdfb3 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 259
17 swift-frontend           0x000000010d7d52cd swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 2365
18 swift-frontend           0x000000010d93e263 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 179
19 swift-frontend           0x000000010d7dadcd swift::ASTLoweringRequest::OutputType swift::Evaluator::getResultUncached<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 605
20 swift-frontend           0x000000010cca3384 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1012
21 swift-frontend           0x000000010cca7b6b performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 4667
22 swift-frontend           0x000000010cca5a98 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4392
23 swift-frontend           0x000000010cc18167 swift::mainEntry(int, char const**) + 8103
24 dyld                     0x00007ff81c1bb2cd start + 1805

Expected behavior

My could would compile and run without any problems.

Environment

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: x86_64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwaretriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions