-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Description
The following code fails to compile in Debug mode.
Note that this project requires one public import: Numerics
.
https://github.com/apple/swift-numerics.git
import Foundation; import _Differentiation; import Numerics
final class G {
public struct J: Differentiable {var s: ES; var l: [[Double]]}
@differentiable(reverse)
func o(pAnds: PandS) -> J {
var n = 0.0; var ts = 1.0; var d: [[Double]] = []; var x: [Double] = []
var y = pAnds.p.q.i?.first { $0 >= n } ?? Date.distantFuture.timeIntervalSince1970
while n < ts {
if n.isApproximatelyEqual(to: y) {y = pAnds.p.q.g(n: n) ?? Date.distantFuture.timeIntervalSince1970}
if n + ts > y {ts = y - n}; if n.isApproximatelyEqual(to: 0.0) {let sr = m(f: pAnds.s, w: [WritableKeyPath<ES, Double>]()); d.append(sr); x.append(n)}; n += ts
}
let sampleF = u(d, i: x, c: Array<String>())
return J(s: pAnds.s, l: d)
}
@differentiable(reverse) func m(f: ES, w: [WritableKeyPath<ES, Double>]) -> [Double] {return [Double]()}
@differentiable(reverse) func u(_: [[Double]], i: [Double], c: [String]) -> F<Double, Double> {return F<Double, Double>()}
}
public struct ES {}
extension ES: Differentiable & ZT {public struct ET: Differentiable {}}
public struct EP {public var q: F<Double, Double>}
extension EP: Differentiable & ZT {}
public struct PandS: Differentiable & ZT {
@differentiable(reverse) public var p: EP
@differentiable(reverse) public var s: ES
}
public protocol Empty {}
extension Double: Empty {}
public protocol FP {associatedtype I: Empty}
public extension FP {
@noDerivative var i: [I]? {return nil}
func g(n index: I) -> I? {return nil}
}
public struct S<I: Empty, Float> {}
public struct F<I: Empty, Float>: FP {}
extension F: Differentiable where Float: Differentiable & ZT, Float.TangentVector == Float {}
public protocol ZT: Differentiable {var zT: () -> TangentVector { get }}
public extension ZT {var zT: () -> TangentVector {{ Self.TangentVector.zero }}}
extension Double: ZT {@inlinable public var zT: () -> TangentVector {{ 0 }}}
Steps to reproduce
Compile the above code in Debug mode. The assertion failure is:
Assertion failed: (hasVal), function value, file Optional.h, line 216.
Expected behavior
The compilation should complete successfully, or, an error message should be generated by the compiler indicating the reason for the compilation failure.
Environment
- Swift compiler version info: 2023-06-27a and earlier exhibit this issue.
- Xcode version info: 14.2
- Deployment target: M1
Additional context
It was difficult to narrow down a single cause for this issue; the following is a (non-exhaustive) list of actions would cause the compilation to succeed:
- Commenting out line 38
- Commenting out line 9
- Commenting out line 10
- Commenting out lines 2 and 17 (meaning, eliminating
class G
and thereby moving its contents into the global scope)
Stack trace:
1. Apple Swift version 5.9-dev (LLVM e7b674b4e35c51c, Swift 50d2f4d3edd3a0f)
2. Compiling with the current language version
3. While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for SingleFile)
4. While running pass #1164 SILModuleTransform "Differentiation".
5. While processing // differentiability witness for G.o(pAnds:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] @$s10SingleFile1GC1o5pAndsAC1JVAA5PandSV_tF : $@convention(method) (PandS, @guaranteed G) -> @owned G.J {
}
on SIL function "@$s10SingleFile1GC1o5pAndsAC1JVAA5PandSV_tF".
for 'o(pAnds:)' (at /Users/user/SingleFile/SingleFile/main.swift:5:5)
6. While generating VJP for SIL function "@$s10SingleFile1GC1o5pAndsAC1JVAA5PandSV_tF".
for 'o(pAnds:)' (at /Users/user/SingleFile/SingleFile/main.swift:5:5)
7. While generating pullback for SIL function "@$s10SingleFile1GC1o5pAndsAC1JVAA5PandSV_tF".
for 'o(pAnds:)' (at /Users/user/SingleFile/SingleFile/main.swift:5:5)
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 0x00000001072739b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1 swift-frontend 0x0000000107272d5c llvm::sys::RunSignalHandlers() + 112
2 swift-frontend 0x0000000107273ff8 SignalHandler(int) + 304
3 libsystem_platform.dylib 0x0000000197f0ea24 _sigtramp + 56
4 libsystem_pthread.dylib 0x0000000197edfc28 pthread_kill + 288
5 libsystem_c.dylib 0x0000000197dedae8 abort + 180
6 libsystem_c.dylib 0x0000000197dece44 err + 0
7 swift-frontend 0x00000001073bbb9c swift::autodiff::PullbackCloner::Implementation::getAdjointBuffer(swift::SILBasicBlock*, swift::SILValue) (.cold.1) + 0
8 swift-frontend 0x0000000102c0a95c swift::autodiff::PullbackCloner::Implementation::getTangentValueCategory(swift::SILValue) + 260
9 swift-frontend 0x0000000102c07ed8 swift::autodiff::PullbackCloner::Implementation::run() + 2280
10 swift-frontend 0x0000000102c075b8 swift::autodiff::PullbackCloner::run() + 24
11 swift-frontend 0x0000000102c24388 swift::autodiff::VJPCloner::Implementation::run() + 1368
12 swift-frontend 0x0000000102c24a50 swift::autodiff::VJPCloner::run() + 24
13 swift-frontend 0x0000000102d5cefc (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5616
14 swift-frontend 0x0000000102d5afcc (anonymous namespace)::Differentiation::run() + 1032
15 swift-frontend 0x0000000102de54c4 swift::SILPassManager::runModulePass(unsigned int) + 980
16 swift-frontend 0x0000000102deb370 swift::SILPassManager::execute() + 624
17 swift-frontend 0x0000000102de2458 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
18 swift-frontend 0x0000000102de23dc swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
19 swift-frontend 0x0000000102e21088 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
20 swift-frontend 0x0000000102e0a920 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
21 swift-frontend 0x0000000102de264c swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
22 swift-frontend 0x0000000102e0c9c4 swift::runSILDiagnosticPasses(swift::SILModule&) + 192
23 swift-frontend 0x000000010270e4e0 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
24 swift-frontend 0x000000010256d3ac 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*) + 792
25 swift-frontend 0x000000010256ca9c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 600
26 swift-frontend 0x000000010257be14 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
27 swift-frontend 0x000000010256f5f8 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 740
28 swift-frontend 0x000000010256e568 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2476
29 swift-frontend 0x00000001023c2ebc swift::mainEntry(int, char const**) + 2144
30 dyld 0x0000000197b87f28 start + 2236
An older toolchain (2022-05-23a) still fails to compile the above code, however, the assertion failure, and corresponding stack trace, are different:
Assertion failed: (type->mayHaveMembers()), function lookupMember, file TypeCheckNameLookup.cpp, line 311.
1. Apple Swift version 5.8-dev (LLVM 67ca5c03f10a5f9, Swift 54102ef207bddf4)
2. Compiling with the current language version
3. While evaluating request TypeCheckSourceFileRequest(source_file "/Users/user/Downloads/Xcode 2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/Foundation.framework/Modules/Foundation.swiftmodule/arm64e-apple-macos.swiftinterface")
4. While evaluating request TypeCheckFunctionBodyRequest(Foundation.(file).AsyncCharacterSequence.AsyncIterator.next()@/Users/user/Downloads/Xcode 2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/Foundation.framework/Modules/Foundation.swiftmodule/arm64e-apple-macos.swiftinterface:9192:55)
5. While type-checking statement at [/Users/user/Downloads/Xcode 2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/Foundation.framework/Modules/Foundation.swiftmodule/arm64e-apple-macos.swiftinterface:9192:97 - line:9200:9] RangeText="{
while let scalar = try await remaining.next() {
accumulator.unicodeScalars.append(scalar)
if accumulator.count > 1 {
return accumulator.removeFirst()
}
}
return accumulator.count > 0 ? accumulator.removeFirst() : nil
"
6. While type-checking statement at [/Users/user/Downloads/Xcode 2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/Foundation.framework/Modules/Foundation.swiftmodule/arm64e-apple-macos.swiftinterface:9193:13 - line:9198:13] RangeText="while let scalar = try await remaining.next() {
accumulator.unicodeScalars.append(scalar)
if accumulator.count > 1 {
return accumulator.removeFirst()
}
"
7. While type-checking statement at [/Users/user/Downloads/Xcode 2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/Foundation.framework/Modules/Foundation.swiftmodule/arm64e-apple-macos.swiftinterface:9193:59 - line:9198:13] RangeText="{
accumulator.unicodeScalars.append(scalar)
if accumulator.count > 1 {
return accumulator.removeFirst()
}
"
8. While type-checking expression at [/Users/user/Downloads/Xcode 2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/Foundation.framework/Modules/Foundation.swiftmodule/arm64e-apple-macos.swiftinterface:9194:17 - line:9194:57] RangeText="accumulator.unicodeScalars.append(scalar"
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 0x00000001089a5b6c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1 swift-frontend 0x00000001089a4dcc llvm::sys::RunSignalHandlers() + 128
2 swift-frontend 0x00000001089a61d0 SignalHandler(int) + 304
3 libsystem_platform.dylib 0x0000000197f0ea24 _sigtramp + 56
4 libsystem_pthread.dylib 0x0000000197edfc28 pthread_kill + 288
5 libsystem_c.dylib 0x0000000197dedae8 abort + 180
6 libsystem_c.dylib 0x0000000197dece44 err + 0
7 swift-frontend 0x0000000108d18c30 swift::TypeChecker::isUnsupportedMemberTypeAccess(swift::Type, swift::TypeDecl*, bool) (.cold.1) + 0
8 swift-frontend 0x000000010517fb28 swift::TypeChecker::lookupMember(swift::DeclContext*, swift::Type, swift::DeclNameRef, swift::OptionSet<swift::NameLookupFlags, unsigned int>) + 420
9 swift-frontend 0x000000010505d09c swift::constraints::ConstraintSystem::lookupMember(swift::Type, swift::DeclNameRef) + 184
10 swift-frontend 0x0000000104fc50b0 swift::constraints::ConstraintSystem::performMemberLookup(swift::constraints::ConstraintKind, swift::DeclNameRef, swift::Type, swift::FunctionRefKind, swift::constraints::ConstraintLocator*, bool) + 3088
11 swift-frontend 0x0000000104fc7174 swift::constraints::ConstraintSystem::simplifyMemberConstraint(swift::constraints::ConstraintKind, swift::Type, swift::DeclNameRef, swift::Type, swift::DeclContext*, swift::FunctionRefKind, llvm::ArrayRef<swift::constraints::OverloadChoice>, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 492
12 swift-frontend 0x00000001050e0c3c swift::constraints::ConstraintSystem::addValueMemberConstraint(swift::Type, swift::DeclNameRef, swift::Type, swift::DeclContext*, swift::FunctionRefKind, llvm::ArrayRef<swift::constraints::OverloadChoice>, swift::constraints::ConstraintLocatorBuilder) + 212
13 swift-frontend 0x0000000104fa0788 (anonymous namespace)::ConstraintGenerator::addMemberRefConstraints(swift::Expr*, swift::Expr*, swift::DeclNameRef, swift::FunctionRefKind, llvm::ArrayRef<swift::ValueDecl*>) + 540
14 swift-frontend 0x0000000104f9cf9c swift::ASTVisitor<(anonymous namespace)::ConstraintGenerator, swift::Type, void, void, void, void, void>::visit(swift::Expr*) + 12216
15 swift-frontend 0x0000000104f97f34 (anonymous namespace)::ConstraintWalker::walkToExprPost(swift::Expr*) + 192
16 swift-frontend 0x00000001054b17e4 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Expr*) + 300
17 swift-frontend 0x00000001054b1900 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Expr*) + 584
18 swift-frontend 0x00000001054b0ecc swift::Expr::walk(swift::ASTWalker&) + 88
19 swift-frontend 0x0000000104f94ba8 swift::constraints::ConstraintSystem::generateConstraints(swift::Expr*, swift::DeclContext*, bool) + 172
20 swift-frontend 0x0000000104f93dcc swift::constraints::ConstraintSystem::generateConstraints(swift::constraints::SolutionApplicationTarget&, swift::FreeTypeVariableBinding) + 356
21 swift-frontend 0x0000000104fe587c swift::constraints::ConstraintSystem::solveImpl(swift::constraints::SolutionApplicationTarget&, swift::FreeTypeVariableBinding) + 404
22 swift-frontend 0x0000000104fe5240 swift::constraints::ConstraintSystem::solve(swift::constraints::SolutionApplicationTarget&, swift::FreeTypeVariableBinding) + 168
23 swift-frontend 0x0000000105121948 swift::TypeChecker::typeCheckExpression(swift::constraints::SolutionApplicationTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 476
24 swift-frontend 0x00000001051216e4 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::constraints::ContextualTypeInfo, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 84
25 swift-frontend 0x00000001051d8a2c (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 220
26 swift-frontend 0x00000001051dabf8 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 320
27 swift-frontend 0x00000001051daa84 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 136
28 swift-frontend 0x00000001051daf34 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 1148
29 swift-frontend 0x00000001051daa84 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 136
30 swift-frontend 0x00000001051d89dc (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 140
31 swift-frontend 0x00000001051dabf8 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 320
32 swift-frontend 0x00000001051d9dfc bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
33 swift-frontend 0x00000001051d94c8 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 756
34 swift-frontend 0x00000001055abd80 llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest>(swift::TypeCheckFunctionBodyRequest const&) + 404
35 swift-frontend 0x00000001055abb04 llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckFunctionBodyRequest, (void*)0>(swift::TypeCheckFunctionBodyRequest const&) + 140
36 swift-frontend 0x000000010550ae80 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 52
37 swift-frontend 0x0000000105206880 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 308
38 swift-frontend 0x0000000105208684 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 400
39 swift-frontend 0x0000000105208424 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 124
40 swift-frontend 0x0000000105206640 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 44
41 swift-frontend 0x0000000104552180 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_7>(long, swift::SourceFile&) + 16
42 swift-frontend 0x000000010454e688 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 160
43 swift-frontend 0x000000010454e5bc swift::CompilerInstance::performSema() + 76
44 swift-frontend 0x0000000104557550 std::__1::error_code llvm::function_ref<std::__1::error_code (swift::SubCompilerInstanceInfo&)>::callback_fn<swift::ModuleInterfaceBuilder::buildSwiftModuleInternal(llvm::StringRef, bool, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> >*, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >)::$_1::operator()() const::'lambda'(swift::SubCompilerInstanceInfo&)>(long, swift::SubCompilerInstanceInfo&) + 960
45 swift-frontend 0x000000010455ffcc swift::InterfaceSubContextDelegateImpl::runInSubCompilerInstance(llvm::StringRef, llvm::StringRef, llvm::StringRef, swift::SourceLoc, llvm::function_ref<std::__1::error_code (swift::SubCompilerInstanceInfo&)>) + 2868
46 swift-frontend 0x0000000104557164 void llvm::function_ref<void ()>::callback_fn<swift::ModuleInterfaceBuilder::buildSwiftModuleInternal(llvm::StringRef, bool, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> >*, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >)::$_1>(long) + 120
47 swift-frontend 0x00000001089084d0 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 252
48 swift-frontend 0x00000001089085f4 RunSafelyOnThread_Dispatch(void*) + 52
49 swift-frontend 0x00000001089086c8 void* llvm::thread::ThreadProxy<std::__1::tuple<void (*)(void*), (anonymous namespace)::RunSafelyOnThreadInfo*> >(void*) + 24
50 libsystem_pthread.dylib 0x0000000197edffa8 _pthread_start + 148
51 libsystem_pthread.dylib 0x0000000197edada0 thread_start + 8