swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.6k stars 10.37k forks source link

Compiler crashes when trying to switch over an `enum` value with a case, added by a macro #67862

Open romikabi opened 1 year ago

romikabi commented 1 year ago

Description

Compiler crashes when trying to switch over an enum value with a case, added by a macro.

Steps to reproduce

Minimal reproducing project: https://github.com/romikabi/MacroBug

  1. Create a peer macro, that adds an enum case:
    public struct AddCaseMacro: PeerMacro {
    public static func expansion(
        of node: AttributeSyntax,
        providingPeersOf declaration: some DeclSyntaxProtocol,
        in context: some MacroExpansionContext
    ) throws -> [DeclSyntax] {
        ["case addedCase"]
    }
    }
  2. Apply the macro to a declaration inside of an enum, alongside another case:

    enum Foo {
    case foo
    
    @AddCase
    enum Bar {}
    }
  3. Switch over an enum value:
    func fooBar(_ foo: Foo) {
    switch foo {
    case .foo:
        break
    case .addedCase:
        break
    }
    }

Expected behavior

Code works normally, as if a case was added manually and not through the macro.

Actual behavior Compilation crashes. Also switch autocomplete doesn't insert a case added by macro automatically, xcode doesn't color case .addedCase the same color as case .foo. If case foo is not added, Foo enum is considered unpopulated.

Stack dump ``` 0. Program arguments: /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/romikabi/Projects/MacroBug/Sources/MacroBugClient/main.swift -emit-dependencies-path /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/Objects-normal/arm64/main.d -emit-const-values-path /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/Objects-normal/arm64/main.swiftconstvalues -emit-reference-dependencies-path /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/Objects-normal/arm64/main.swiftdeps -serialize-diagnostics-path /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/Objects-normal/arm64/main.dia -target arm64-apple-macos10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -I /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Products/Debug -I /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Products/Debug/PackageFrameworks -F /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Products/Debug -F /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path /Users/romikabi/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -load-plugin-executable /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Products/Debug/MacroBugMacros#MacroBugMacros -package-name macrobug -const-gather-protocols-file /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/Objects-normal/arm64/MacroBugClient_const_extract_protocols.json -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/romikabi/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/romikabi/Projects/MacroBug -resource-dir /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -ivfsstatcache -Xcc /Users/romikabi/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx14.0-23A5312c-430ebd289dba5af249207038329132b1.sdkstatcache -Xcc -I/Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Products/Debug/include -Xcc -I/Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/DerivedSources-normal/arm64 -Xcc -I/Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/DerivedSources/arm64 -Xcc -I/Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name MacroBugClient -frontend-parseable-output -disable-clang-spi -target-sdk-version 14.0 -target-sdk-name macosx14.0 -external-plugin-path /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-15.0.0-Beta.6.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Build/Intermediates.noindex/MacroBug.build/Debug/MacroBugClient.build/Objects-normal/arm64/main.o -index-unit-output-path /MacroBug.build/Debug/MacroBugClient.build/Objects-normal/arm64/main.o -index-store-path /Users/romikabi/Library/Developer/Xcode/DerivedData/MacroBug-bpweuzevkghtofgkvsqckothtidv/Index.noindex/DataStore -index-system-modules 1. Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1) 2. Compiling with the current language version 3. While evaluating request IRGenRequest(IR Generation for file "/Users/romikabi/Projects/MacroBug/Sources/MacroBugClient/main.swift") 4. While emitting IR SIL function "@$s14MacroBugClient6fooBaryyAA3FooOF". for 'fooBar(_:)' (at /Users/romikabi/Projects/MacroBug/Sources/MacroBugClient/main.swift:14: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 0x00000001030c5d04 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56 1 swift-frontend 0x000000010106c4d4 llvm::sys::RunSignalHandlers() + 112 2 swift-frontend 0x00000001030c6500 SignalHandler(int) + 352 3 libsystem_platform.dylib 0x00000001942b6a24 _sigtramp + 56 4 libsystem_platform.dylib 0xffff800101c47360 _sigtramp + 18446603333764974964 5 swift-frontend 0x000000010540f734 (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 63344 6 swift-frontend 0x00000001053d4234 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 11680 7 swift-frontend 0x0000000102fac114 swift::irgen::IRGenerator::emitGlobalTopLevel(std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&) + 2516 8 swift-frontend 0x000000010286600c swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 6532 9 swift-frontend 0x00000001053c6c48 swift::SimpleRequest::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 176 10 swift-frontend 0x0000000104e3169c llvm::Expected swift::Evaluator::getResultUncached(swift::IRGenRequest const&) + 1480 11 swift-frontend 0x0000000104bd66d0 swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 264 12 swift-frontend 0x0000000105d70250 generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion, llvm::GlobalVariable*&, llvm::ArrayRef, std::__1::allocator>>) + 156 13 swift-frontend 0x000000010564739c performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr>, llvm::PointerUnion, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1596 14 swift-frontend 0x0000000105d732b8 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1748 15 swift-frontend 0x0000000100749b10 swift::performFrontend(llvm::ArrayRef, char const*, void*, swift::FrontendObserver*) + 4568 16 swift-frontend 0x00000001056b3dd4 swift::mainEntry(int, char const**) + 4116 17 dyld 0x0000000193f2ff28 start + 2236 Command SwiftCompile failed with a nonzero exit code ```

Environment

romikabi commented 1 year ago

still broken on Xcode 15 Beta 8 (15A5229m)

romikabi commented 1 year ago

still broken in Xcode 15 RC (15A240d)