swiftlang / swift

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

[ParameterPacks] Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file Casting.h, line 578. #71258

Open vanvoorden opened 8 months ago

vanvoorden commented 8 months ago

Description

No response

Reproduction

https://gist.github.com/vanvoorden/ff2ab196296646956178c70dc7d9a1fd

Hi! I'm seeing some crashes and compiler failures from passing a tuple of only one value to an argument expecting a parameter pack tuple. Passing multiple values in the tuple fixes the compiler failures. I've tested this code on the three latest Swift versions:

My first example fails to compile on all three versions:

struct Repeater<each Element> {
  private let element: (repeat () -> each Element)

  init(
    _ element: (repeat () -> each Element)
  ) {
    self.element = element
  }
}

let r = Repeater(
  ({ 0 })
)

That first example fails with the same error on 5.9.2 and 5.10:

Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file Casting.h, line 578.

That first example fails on 5.11 with a different failure:

let r = Repeater(
 ({ 0 })  //  Cannot convert value of type '_' to expected argument type '(_: () -> _)'
)

My second example fails only for 5.11:

struct Repeater<each Element> {
  init(
    _ element: (repeat () -> each Element)
  ) {

  }
}

let r = Repeater(
  ({ 0 })
)

That second example fails on 5.11 with the same failure from the first example:

My third example compiles on all three versions:

struct Repeater<each Element> {
  private let element: (repeat () -> each Element)

  init(
    _ element: (repeat () -> each Element)
  ) {
    self.element = element
  }
}

let r = Repeater(
  ({ 0 }, { 0 })
)

Any ideas why passing multiple values in my tuple is compiling… but only passing one fails? And why passing only one value compiles when the parameter pack is not saved as an instance variable?

Stack dump

Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file Casting.h, line 578.
 Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
 Stack dump:
 0.  Program arguments: /Library/Developer/Toolchains/swift-5.9-DEVELOPMENT-SNAPSHOT-2024-01-06-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/rick/Desktop/RepeaterDemo/Sources/main.swift -emit-dependencies-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.d -emit-const-values-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.swiftconstvalues -emit-reference-dependencies-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.swiftdeps -serialize-diagnostics-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.dia -target arm64-apple-macos14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -I /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Products/Debug -I /Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Products/Debug -F /Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path /Users/rick/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -external-plugin-path /Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins#/Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins#/Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -serialize-debugging-options -package-name repeaterdemo -const-gather-protocols-file /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/RepeaterDemo_const_extract_protocols.json -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/rick/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/rick/Desktop/RepeaterDemo -resource-dir /Library/Developer/Toolchains/swift-5.9-DEVELOPMENT-SNAPSHOT-2024-01-06-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Products/Debug/include -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/DerivedSources-normal/arm64 -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/DerivedSources/arm64 -Xcc -I/Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name RepeaterDemo -frontend-parseable-output -disable-clang-spi -target-sdk-version 14.2 -target-sdk-name macosx14.2 -external-plugin-path /Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/lib/swift/host/plugins#/Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/Toolchains/swift-5.9-DEVELOPMENT-SNAPSHOT-2024-01-06-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-5.9-DEVELOPMENT-SNAPSHOT-2024-01-06-a.xctoolchain/usr/local/lib/swift/host/plugins -o /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Build/Intermediates.noindex/RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.o -index-unit-output-path /RepeaterDemo.build/Debug/RepeaterDemo.build/Objects-normal/arm64/main.o -index-store-path /Users/rick/Library/Developer/Xcode/DerivedData/RepeaterDemo-aoiveyvurevauqfmtsmslveqfmoc/Index.noindex/DataStore -index-system-modules
 1.  Apple Swift version 5.9.2-dev (LLVM 2b42c5ce063a374, Swift 9067148bc9c9a72)
 2.  Compiling with the current language version
 3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { Non-Diagnostic Mandatory Optimizations, Serialization, Rest of Onone } on SIL for RepeaterDemo)
 4.  While running pass #25 SILModuleTransform "UsePrespecialized".
 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           0x00000001055e1554 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
 1  swift-frontend           0x00000001055e08f8 llvm::sys::RunSignalHandlers() + 112
 2  swift-frontend           0x00000001055e1b94 SignalHandler(int) + 304
 3  libsystem_platform.dylib 0x0000000188ce1a24 _sigtramp + 56
 4  libsystem_pthread.dylib  0x0000000188cb1cc0 pthread_kill + 288
 5  libsystem_c.dylib        0x0000000188bbda40 abort + 180
 6  libsystem_c.dylib        0x0000000188bbcd30 err + 0
 7  swift-frontend           0x00000001058c34a0 swift::Lowering::AbstractionPattern::forEachFunctionParam(swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, bool, llvm::function_ref<void (swift::Lowering::FunctionParamGenerator&)>) const (.cold.1) + 0
 8  swift-frontend           0x0000000101578f20 swift::Lowering::AbstractionPattern::getNumFunctionParams() const + 92
 9  swift-frontend           0x00000001015790f0 swift::Lowering::FunctionParamGenerator::FunctionParamGenerator(swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, bool) + 256
 10 swift-frontend           0x0000000101578f7c swift::Lowering::AbstractionPattern::forEachFunctionParam(swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, bool, llvm::function_ref<void (swift::Lowering::FunctionParamGenerator&)>) const + 64
 11 swift-frontend           0x000000010157b6e4 (anonymous namespace)::SubstFunctionTypePatternVisitor::handleUnabstractedFunctionType(swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern) + 160
 12 swift-frontend           0x000000010157b3ac swift::Lowering::AbstractionPattern::getSubstFunctionTypePattern(swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeConverter&, swift::Lowering::AbstractionPattern, swift::CanType, bool&) const + 392
 13 swift-frontend           0x00000001015c10c4 getSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILExtInfoBuilder, (anonymous namespace)::Conventions const&, swift::ForeignInfo const&, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SubstitutionMap>, swift::ProtocolConformanceRef) + 1708
 14 swift-frontend           0x00000001015b90c8 getNativeSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILExtInfoBuilder, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SubstitutionMap>, swift::ProtocolConformanceRef) + 1376
 15 swift-frontend           0x00000001015b8b5c swift::getNativeSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILExtInfo, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SILDeclRef>, llvm::Optional<swift::SubstitutionMap>, swift::ProtocolConformanceRef) + 96
 16 swift-frontend           0x0000000101667008 swift::Lowering::TypeConverter::computeLoweredRValueType(swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanType)::LoweredRValueTypeVisitor::visitAnyFunctionType(swift::CanTypeWrapper<swift::AnyFunctionType>) + 760
 17 swift-frontend           0x00000001016549f4 swift::CanTypeVisitor<swift::Lowering::TypeConverter::computeLoweredRValueType(swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanType)::LoweredRValueTypeVisitor, swift::CanType>::visit(swift::CanType) + 456
 18 swift-frontend           0x0000000101653240 swift::Lowering::TypeConverter::getTypeLowering(swift::Lowering::AbstractionPattern, swift::Type, swift::TypeExpansionContext) + 620
 19 swift-frontend           0x0000000101648640 swift::SILType::getFieldType(swift::VarDecl*, swift::Lowering::TypeConverter&, swift::TypeExpansionContext) const + 232
 20 swift-frontend           0x00000001016592f8 countNumberOfInnerFields(unsigned int&, swift::Lowering::TypeConverter&, swift::SILType, swift::TypeExpansionContext) + 148
 21 swift-frontend           0x00000001016591e0 swift::Lowering::TypeConverter::countNumberOfFields(swift::SILType, swift::TypeExpansionContext) + 160
 22 swift-frontend           0x0000000101393460 swift::shouldExpand(swift::SILModule&, swift::SILType) + 188
 23 swift-frontend           0x000000010136acc4 swift::ReabstractionInfo::getReturnTypeCategory(swift::SILResultInfo const&, swift::SILFunctionConventions const&, swift::TypeExpansionContext) + 316
 24 swift-frontend           0x000000010136a748 swift::ReabstractionInfo::createSubstitutedAndSpecializedTypes() + 400
 25 swift-frontend           0x0000000101369884 swift::ReabstractionInfo::performFullSpecializationPreparation(swift::SILFunction*, swift::SubstitutionMap) + 116
 26 swift-frontend           0x00000001013689dc swift::ReabstractionInfo::ReabstractionInfo(swift::ModuleDecl*, bool, swift::ApplySite, swift::SILFunction*, swift::SubstitutionMap, swift::IsSerialized_t, bool, bool, swift::OptRemark::Emitter*) + 252
 27 swift-frontend           0x000000010105ea24 (anonymous namespace)::UsePrespecialized::run() + 848
 28 swift-frontend           0x00000001011a9854 swift::SILPassManager::runModulePass(unsigned int) + 976
 29 swift-frontend           0x00000001011af69c swift::SILPassManager::execute() + 624
 30 swift-frontend           0x00000001011a6870 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
 31 swift-frontend           0x00000001011a67f8 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
 32 swift-frontend           0x00000001011c7dac swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
 33 swift-frontend           0x00000001011b20e8 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
 34 swift-frontend           0x00000001011a6a60 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
 35 swift-frontend           0x00000001011b42f8 swift::runSILPassesForOnone(swift::SILModule&) + 80
 36 swift-frontend           0x0000000100ac0898 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 248
 37 swift-frontend           0x000000010092ed58 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*) + 712
 38 swift-frontend           0x000000010092e4c8 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 568
 39 swift-frontend           0x000000010093d494 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
 40 swift-frontend           0x0000000100930064 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2848
 41 swift-frontend           0x00000001007b4f60 swift::mainEntry(int, char const**) + 3256
 42 dyld                     0x00000001889310e0 start + 2360
 Command SwiftCompile failed with a nonzero exit code

Expected behavior

no compiler crashes or errors

Environment

Additional information

No response

AnthonyLatsis commented 7 months ago

Reduced test case:

func foo<each Element>(_ element: (repeat () -> each Element)) {}
foo(({ () -> Int in 0 }))

Not labeling this as a crash because it no longer does so.