Skip to content

[AutoDiff] Assertion failure: (!isInvalid()) in function getRequirement. #70834

Open
@fibrechannelscsi

Description

@fibrechannelscsi

Description

The compiler generates an error message when attempting to compile the following code in Release mode. The error message is:

Assertion failed: (!isInvalid()), function getRequirement, file ProtocolConformanceRef.cpp, line 56.

Reproduction

This reproducer requires four files: two Package.swift files (one is for an internal package), and two regular Swift source files.
The directory tree looks like this:

./Package.swift
./Sources/InternalPackage/Package.swift
./Sources/InternalPackage/Sources/InternalPackage/User.swift
./Sources/ReproducerServer/Auth.swift

Listing for Package.swift:

// swift-tools-version: 5.9
import PackageDescription
let package = Package(
    name: "Reproducer",
    platforms: [.macOS(.v12),],
    products: [.library(name: "Reproducer", targets: ["ReproducerServer"]),],
    dependencies: [
        .package(name: "InternalPackage", path: "./Sources/InternalPackage"),
        .package(url: "https://github.com/apple/swift-algorithms", "1.0.0" ..< "2.0.0"),
        .package(url: "https://github.com/apple/swift-collections.git", "1.0.4" ..< "2.0.0")
        
    ],
    targets: [.target(name: "ReproducerServer", dependencies: [
        .product(name: "InternalPackage", package: "InternalPackage"),
        .product(name: "Algorithms", package: "swift-algorithms"),
        .product(name: "OrderedCollections", package: "swift-collections"),
    ])]
)

Listing for Sources/InternalPackage/Package.swift:

// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
    name: "InternalPackage",
    platforms: [.macOS(.v12), .iOS(.v15)],
    products: [.library(name: "InternalPackage",targets: ["InternalPackage"])],
    dependencies: [.package(url: "https://github.com/apple/swift-collections.git",from: "1.0.4")],
    targets: [.target(name: "InternalPackage",dependencies: [.product(name: "Collections", package: "swift-collections")])]
)

Listing for Sources/InternalPackage/Sources/InternalPackage/User.swift:

import Foundation; import _Differentiation
public protocol H {var x: Double { get }} 
public extension Sequence where Element: H {func e() -> [C] {return [C]()}} 
public struct C: Differentiable{}
@differentiable(reverse) public func w(s1: [C], s2: [C]) -> C {return C()}
public func w<V: H>(s1: any Sequence<V>, s2: any Sequence<V>) -> C {w(s1: s1.e(), s2: s2.e())}

Listing for Sources/ReproducerServer/Auth.swift:

import Algorithms; import Foundation; import _Differentiation; import OrderedCollections; import InternalPackage
public extension OrderedDictionary where Value: Sequence {
    func j<V: H>() -> [Key : [Key : C] ] where Value.Element == (key: Key, value: V) {self.mapValues { $0.map(\.value) }.j()}
    func j() -> [Key : [Key : C] ] where Value.Element: H {self.elements.combinations(ofCount: 2).reduce(into: [:]) {$0[$1[0].0, default: [:]][$1[1].0] = w(s1: $1[0].1, s2: $1[1].1)}}
}
extension V: H {}
public extension F {@discardableResult func z() -> [F] {self.r(); return [F]()}}
internal extension F {
    func r()  {
        let l = OrderedDictionary<String, A<F, V>>().reduce(into: [String : F]() ) {$0[$1.key] = F()}
        let n = OrderedDictionary<String, A<F, V>>().mapValues { Array($0.r(f: self)) }
        _ = B<F>().d(v: l,u: n)
    }
}
extension B<F> {func d(v l: [String : F], u n: OrderedDictionary<String, [(key: String, value: V)]>) -> [F] {let h = n.j(); return [F]()}}
public struct F{} 
struct V{public var x: Double}
struct B<S>{}
struct A<S, V>{func r<R>(f: R)  -> OrderedDictionary<String, V>{fatalError()}}

Expected behavior

The compilation should succeed. (Note that the compilation fails in Release mode, but not in Debug mode).

Environment

Nightly toolchains: 2023-11-27a and 2024-01-08a.
ARM64
Xcode 15.1

Additional information

Note that performing any of these operations causes the build to succeed (this list is not exhaustive):

  • Removing the public qualifier from struct F and its extension.
  • Editing func w to return C() instead of calling the differentiable w above.
  • Removing where Element: H from public extension Sequence.
  • Moving the contents of User.swift into Auth.swift (hence the requirement for an internal Package).

Stack trace:

1.	Apple Swift version 5.11-dev (LLVM c58c3691d438575, Swift a159604f1cc02a0)
2.	Compiling with the current language version
3.	While emitting IR SIL function "@$s18OrderedCollections0A10DictionaryV16ReproducerServerSTR_rlE1jSDyxSDyx15InternalPackage1CVGGyAF1H7ElementRp_rlFyAJz_Sayx3key_q_5valuetGtXEfU_SS_SayAD1VVGTg5".
 for expression at [/Users/user/reproducer/Sources/ReproducerServer/Auth.swift:4:117 - line:4:182] RangeText="{$0[$1[0].0, default: [:]][$1[1].0] = w(s1: $1[0].1, s2: $1[1].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           0x000000010a24dec0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010a24c68c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010a24e508 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000018b12aa24 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018b0fbc28 pthread_kill + 288
5  libsystem_c.dylib        0x000000018b009ae8 abort + 180
6  libsystem_c.dylib        0x000000018b008e44 err + 0
7  swift-frontend           0x000000010a767470 swift::ProtocolConformanceRef::subst(swift::Type, swift::InFlightSubstitution&) const (.cold.1) + 0
8  swift-frontend           0x00000001066750cc swift::ProtocolConformanceRef::getRequirement() const + 244
9  swift-frontend           0x00000001052d329c swift::irgen::emitWitnessTableRef(swift::irgen::IRGenFunction&, swift::CanType, llvm::Value**, swift::ProtocolConformanceRef) + 180
10 swift-frontend           0x00000001052d3edc swift::irgen::emitGenericRequirementFromSubstitutions(swift::irgen::IRGenFunction&, swift::GenericRequirement, swift::MetadataState, swift::SubstitutionMap, bool) + 324
11 swift-frontend           0x00000001052da11c void llvm::function_ref<void (swift::GenericRequirement)>::callback_fn<(anonymous namespace)::EmitPolymorphicArguments::emit(swift::SubstitutionMap, swift::irgen::WitnessMetadata*, swift::irgen::Explosion&)::$_17>(long, swift::GenericRequirement) + 64
12 swift-frontend           0x00000001052da74c void llvm::function_ref<void (swift::GenericRequirement)>::callback_fn<(anonymous namespace)::PolymorphicConvention::enumerateUnfulfilledRequirements(llvm::function_ref<void (swift::GenericRequirement)> const&)::$_8>(long, swift::GenericRequirement) + 180
13 swift-frontend           0x00000001052cc000 swift::irgen::enumerateGenericSignatureRequirements(swift::CanGenericSignature, llvm::function_ref<void (swift::GenericRequirement)> const&) + 280
14 swift-frontend           0x00000001052d35d4 swift::irgen::emitPolymorphicArguments(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::SILFunctionType>, swift::SubstitutionMap, swift::irgen::WitnessMetadata*, swift::irgen::Explosion&) + 272
15 swift-frontend           0x000000010538fe5c (anonymous namespace)::IRGenSILFunction::visitFullApplySite(swift::FullApplySite) + 2992
16 swift-frontend           0x0000000105371980 (anonymous namespace)::IRGenSILFunction::visitSILBasicBlock(swift::SILBasicBlock*) + 2280
17 swift-frontend           0x000000010536fe6c (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 9156
18 swift-frontend           0x000000010536d544 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1556
19 swift-frontend           0x000000010521a2e4 swift::irgen::IRGenerator::emitLazyDefinitions() + 1576
20 swift-frontend           0x00000001053236d4 swift::performIRGeneration(swift::ModuleDecl*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, llvm::GlobalVariable**) + 2348
21 swift-frontend           0x0000000104e545c4 generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, llvm::GlobalVariable*&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>) + 272
22 swift-frontend           0x0000000104e50c0c 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*) + 1416
23 swift-frontend           0x0000000104e502c4 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1180
24 swift-frontend           0x0000000104e60ba4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
25 swift-frontend           0x0000000104e525b0 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
26 swift-frontend           0x0000000104e515e4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2292
27 swift-frontend           0x0000000104cd541c swift::mainEntry(int, char const**) + 2192
28 dyld                     0x000000018ada3f28 start + 2236

Metadata

Metadata

Assignees

No one assigned

    Labels

    AutoDiffIRGenLLVM IR generationassertion failureBug → crash: An assertion failurebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.closuresFeature: closurescompilerThe Swift compiler itselfconformancesFeature → protocol: protocol conformancescrashBug: A crash, i.e., an abnormal termination of softwareexpressionsFeature: expressionsgenericsFeature: generic declarations and typesmultiple filesFlag: An issue whose reproduction requires multiple filesoptimized onlyFlag: An issue whose reproduction requires optimized compilationswift 6.0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions