Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic type parameter pack combining operation crash #78191

Open
stephencelis opened this issue Dec 14, 2024 · 2 comments
Open

Generic type parameter pack combining operation crash #78191

stephencelis opened this issue Dec 14, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software generic constraints Feature → generics: generic constraints generics Feature: generic declarations and types IRGen LLVM IR generation parameter packs Feature → generics: Parameter packs swift 6.0 tuples Feature: tuples types Feature: types

Comments

@stephencelis
Copy link
Contributor

Description

I tried to extend a generic type with a parameter pack combining operation, but the compiler crashes.

Reproduction

struct S<T> {
  let t: T
  func f<each A, each B>(
    _ b: S<(repeat each B)>
  ) -> S<(repeat each A, repeat each B)>
  where T == (repeat each A) {
    S<(repeat each A, repeat each B)>(
      t: (repeat each t, repeat each b.t)
    )
  }
}

Stack dump

PHI node entries do not match predecessors!
  %32 = phi i64 [ 0, %28 ], [ %61, %34 ], !dbg !87
label %34
label %50
Instruction does not dominate all uses!
  %61 = add i64 %32, 1, !dbg !87
  %32 = phi i64 [ 0, %28 ], [ %61, %34 ], !dbg !87
PHI node entries do not match predecessors!
  %148 = phi i64 [ 0, %144 ], [ %175, %150 ], !dbg !89
label %150
label %164
Instruction does not dominate all uses!
  %175 = add i64 %148, 1, !dbg !89
  %148 = phi i64 [ 0, %144 ], [ %175, %150 ], !dbg !89
PHI node entries do not match predecessors!
  %178 = phi i64 [ 0, %176 ], [ %205, %180 ], !dbg !89
label %180
label %194
Instruction does not dominate all uses!
  %205 = add i64 %178, 1, !dbg !89
  %178 = phi i64 [ 0, %176 ], [ %205, %180 ], !dbg !89
Instruction does not dominate all uses!
  %183 = call ptr @llvm.stacksave(), !dbg !89
  call void @llvm.stackrestore(ptr %183), !dbg !102
Instruction does not dominate all uses!
  %153 = call ptr @llvm.stacksave(), !dbg !89
  call void @llvm.stackrestore(ptr %153), !dbg !102
Instruction does not dominate all uses!
  %97 = call ptr @llvm.stacksave(), !dbg !87
  call void @llvm.stackrestore(ptr %97), !dbg !102
Instruction does not dominate all uses!
  %120 = call ptr @llvm.stacksave(), !dbg !87
  call void @llvm.stackrestore(ptr %120), !dbg !102
Instruction does not dominate all uses!
  %39 = call ptr @llvm.stacksave(), !dbg !87
  call void @llvm.stackrestore(ptr %39), !dbg !102
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode-16.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/stephen/Downloads/package/Sources/package/package.swift -emit-dependencies-path /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/Objects-normal/arm64/package.d -emit-const-values-path /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/Objects-normal/arm64/package.swiftconstvalues -emit-reference-dependencies-path /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/Objects-normal/arm64/package.swiftdeps -serialize-diagnostics-path /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/Objects-normal/arm64/package.dia -target arm64-apple-macos10.13 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-16.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -I /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Products/Debug -I /Applications/Xcode-16.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Products/Debug -F /Applications/Xcode-16.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /Users/stephen/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -profile-generate -profile-coverage-mapping -swift-version 6 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -serialize-debugging-options -const-gather-protocols-file /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/Objects-normal/arm64/package_const_extract_protocols.json -enable-experimental-feature DebugDescriptionMacro -enable-experimental-feature OpaqueTypeErasure -empty-abi-descriptor -plugin-path /Applications/Xcode-16.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins/testing -validate-clang-modules-once -clang-build-session-file /Users/stephen/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/stephen/Downloads/package/.swiftpm/xcode -resource-dir /Applications/Xcode-16.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/stephen/Downloads/package/.swiftpm/xcode -Xcc -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -Xcc -ivfsstatcache -Xcc /Users/stephen/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/macosx15.2-24C94-e9512ba338551096f4c3ebf3c3012957.sdkstatcache -Xcc -I/Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/swift-overrides.hmap -Xcc -I/Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Products/Debug/include -Xcc -I/Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/DerivedSources-normal/arm64 -Xcc -I/Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/DerivedSources/arm64 -Xcc -I/Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name package -package-name package -frontend-parseable-output -disable-clang-spi -target-sdk-version 15.2 -target-sdk-name macosx15.2 -external-plugin-path /Applications/Xcode-16.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-16.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-16.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-16.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode-16.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode-16.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -parse-as-library -o /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Build/Intermediates.noindex/package.build/Debug/package.build/Objects-normal/arm64/package.o -index-unit-output-path /package.build/Debug/package.build/Objects-normal/arm64/package.o -index-store-path /Users/stephen/Library/Developer/Xcode/DerivedData/package-ecllozokgctdaocbmbrwyqmbavdx/Index.noindex/DataStore -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
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           0x0000000106622a9c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106620cf0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000106623068 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x0000000199264184 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000019922ef70 pthread_kill + 288
5  libsystem_c.dylib        0x000000019913b908 abort + 128
6  swift-frontend           0x0000000101383ce8 createDispatchingDiagnosticConsumerIfNeeded(swift::FrontendInputsAndOutputs const&, llvm::function_ref<std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_delete<swift::DiagnosticConsumer>> (swift::InputFile const&)>) + 0
7  swift-frontend           0x0000000106592820 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x0000000106592708 llvm::report_fatal_error(llvm::Twine const&, bool) + 0
9  swift-frontend           0x00000001015c3bd4 llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::printPipeline(llvm::raw_ostream&, llvm::function_ref<llvm::StringRef (llvm::StringRef)>) + 0
10 swift-frontend           0x000000010635a420 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) + 224
11 swift-frontend           0x00000001015b86bc swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*, llvm::raw_pwrite_stream*) + 3764
12 swift-frontend           0x00000001015ba2f8 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, llvm::vfs::OutputBackend&, swift::UnifiedStatsReporter*) + 2352
13 swift-frontend           0x0000000100fcf8dc generateCode(swift::CompilerInstance&, llvm::StringRef, llvm::Module*, llvm::GlobalVariable*) + 368
14 swift-frontend           0x0000000100fcad70 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 2316
15 swift-frontend           0x0000000100fc9bd0 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 984
16 swift-frontend           0x0000000100fcce88 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1684
17 swift-frontend           0x0000000100fcbbb4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
18 swift-frontend           0x0000000100f52a5c swift::mainEntry(int, char const**) + 3680
19 dyld                     0x0000000198eac274 start + 2840

Expected behavior

I expect the code to compile and not crash.

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: arm64-apple-macosx15.0

Additional information

No response

@stephencelis stephencelis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Dec 14, 2024
@JessyCatterwaul
Copy link

JessyCatterwaul commented Dec 14, 2024

This reduction displays that you simply cannot refer to another parameter pack in an instance method:

extension S {
  func ƒ<each A, each B>(
    _ b: S<(repeat each B)>
  ) where T == (repeat each A) {
    _ = b.t // compiler crash
  }
}

Fortunately, you can still have the equivalently-functional API, as this is not a problem with static methods.

@Test func chain() {
  let boxes = (
    Box(1, "2"),
    Box(3.0, "4", 5)
  )
  #expect(
    Box.chain(boxes.0, boxes.1).value == (1, "2", 3, "4", 5)
  )
}

@Test func zip() {
  let boxes = (
    Box(0, "1"),
    Box("0", 1)
  )
  let value = Box.zip(boxes.0, boxes.1).value
  #expect(value.0 == (0, "0"))
  #expect(value.1 == ("1", 1))
}
public extension Box {
  init<each Element>(_ element: repeat each Element)
  where Value == (repeat each Element) {
    self.value = (repeat each element)
  }

  static func chain<each Element0, each Element1>(
    _ box0: Box<(repeat each Element0)>,
    _ box1: Box<(repeat each Element1)>
  ) -> Self
  where Value == (repeat each Element0, repeat each Element1) {
    .init(
      value: MyLibrary.chain((repeat each box0.value), (repeat each box1.value))
    )
  }

  static func zip<each Element0, each Element1>(
    _ box0: Box<(repeat each Element0)>,
    _ box1: Box<(repeat each Element1)>
  ) -> Self
  where Value == (repeat (each Element0, each Element1)) {
    .init(
      value: MyLibrary.zip((repeat each box0.value), (repeat each box1.value))
    )
  }
}
// You can't extend tuples so these are global in whatever module "MyLibrary" actually is.
// E.g. https://github.com/Catterwaul/Tuplay

/// Concatenate 2 tuples into one.
@inlinable public func chain<
  each Element0,
  each Element1
>(
  _ element0: (repeat each Element0),
  _ element1: (repeat each Element1)
) -> (
  repeat each Element0,
  repeat each Element1
) {
  ( repeat each element0,
    repeat each element1
  )
}

/// A tuple of pairs built out of two underlying tuples.
///
/// - Note: Unlike `zip` for sequences, these two tuples are required to have the same count.
@_documentation(visibility: private) // DocC can't cope with this signature.
@inlinable public func zip<each Element0, each Element1>(
  _ element0: (repeat each Element0),
  _ element1: (repeat each Element1)
) -> (repeat (each Element0, each Element1)) {
  (repeat (each element0, each element1))
}

I saw your post here. You can copy my information there if you want but I won't ever go back there.

@stephencelis
Copy link
Contributor Author

@JessyCatterwaul Thanks for sharing a potential workaround! Hopefully the underlying bug will be fixed soon.

@AnthonyLatsis AnthonyLatsis added generics Feature: generic declarations and types parameter packs Feature → generics: Parameter packs compiler The Swift compiler itself swift 6.0 IRGen LLVM IR generation tuples Feature: tuples types Feature: types generic constraints Feature → generics: generic constraints and removed triage needed This issue needs more specific labels labels Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software generic constraints Feature → generics: generic constraints generics Feature: generic declarations and types IRGen LLVM IR generation parameter packs Feature → generics: Parameter packs swift 6.0 tuples Feature: tuples types Feature: types
Projects
None yet
Development

No branches or pull requests

3 participants