Skip to content

Crash at compilation when calling variadic generic function using pack expansion as argument #67645

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

Closed
martialln opened this issue Aug 1, 2023 · 10 comments
Assignees
Labels
assertion failure Bug → crash: An assertion failure 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 expressions Feature: expressions generics Feature: generic declarations and types pack expansions Feature → expressions: Pack expansion expressions parameter packs Feature → generics: Parameter packs swift 5.9 type checker Area → compiler: Semantic analysis

Comments

@martialln
Copy link

martialln commented Aug 1, 2023

Description
Compiling the code below result in a crash / fatalError of the compiler.

Steps to reproduce

@discardableResult
func foo<each Arg, each Name: StringProtocol>(
    named: repeat each Name,
    fn: @escaping (repeat each Arg) -> Void
) -> (repeat ((each Arg).Type, each Name)) {
    // Do something interesting here
    
    // Return this tuple to constrain named and fn type pack to have the same shape
    // Is there a better way ?
    return (repeat ((each Arg).self, each named))
}

func foo2<each Arg>(
    fn: @escaping (repeat each Arg) -> Void
) {
    // try to generate default value for named by having an empty strings value pack which
    // has the same shape than fn parameters
    func returnEmptyString<S>(_ type: S.Type) -> String { "" }
    foo(
        named: repeat returnEmptyString((each Arg).self),
        fn: fn
    )
}
  • Compile the snippets above.
  • On Xcode a crash is reported "Command SwiftCompile failed with a nonzero exit code"
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-2023-07-25-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file <projectpath>/Sources/main.swift -emit-dependencies-path <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.d -emit-const-values-path <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.swiftconstvalues -emit-reference-dependencies-path <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.swiftdeps -serialize-diagnostics-path <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.dia -target arm64-apple-macos13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -I <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Products/Debug -I /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Products/Debug -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path <homepath>/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-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -serialize-debugging-options -package-name variadic_generic_tests -const-gather-protocols-file <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/vgtests_const_extract_protocols.json -enable-experimental-feature VariadicGenerics -empty-abi-descriptor -plugin-path /Library/Developer/Toolchains/swift-5.9-DEVELOPMENT-SNAPSHOT-2023-07-25-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-5.9-DEVELOPMENT-SNAPSHOT-2023-07-25-a.xctoolchain/usr/local/lib/swift/host/plugins -validate-clang-modules-once -clang-build-session-file <homepath>/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/SourcePackages -resource-dir /Library/Developer/Toolchains/swift-5.9-DEVELOPMENT-SNAPSHOT-2023-07-25-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I<homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Products/Debug/include -Xcc -I<homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/DerivedSources-normal/arm64 -Xcc -I<homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/DerivedSources/arm64 -Xcc -I<homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name vgtests -frontend-parseable-output -disable-clang-spi -disable-availability-checking -target-sdk-version 14.0 -target-sdk-name macosx14.0 -o <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.o -index-unit-output-path /vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.o -index-store-path <homepath>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Index.noindex/DataStore -index-system-modules
1.	Apple Swift version 5.9-dev (LLVM ea47328e7cf304e, Swift 9c3928cc0c738b5)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "<projectpath>/Sources/main.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(vgtests.(file).foo2(fn:)@<projectpath>/Sources/main.swift:54:6)
5.	While type-checking statement at [<projectpath>/Sources/main.swift:56:3 - line:64:1] RangeText="{
    // try to generate default value for named by having an empty strings value pack which
    // has the same shape than fn parameters
    func returnEmptyString<S>(_ type: S.Type) -> String { "" }
    foo(
        named: repeat returnEmptyString((each Arg).self),
        fn: fn
    )
"
6.	While type-checking expression at [<projectpath>/Sources/main.swift:60:5 - line:63:5] RangeText="foo(
        named: repeat returnEmptyString((each Arg).self),
        fn: fn
    "
7.	While type-checking-target starting at <projectpath>/Sources/main.swift:60: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           0x0000000106fa49ec llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106fa3d90 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000106fa502c SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000018094ea24 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018091fc28 pthread_kill + 288
5  libsystem_c.dylib        0x000000018082dae8 abort + 180
6  libsystem_c.dylib        0x000000018082ce44 err + 0
7  swift-frontend           0x000000010732e26c swift::constraints::ConstraintSystem::simplifySameShapeConstraint(swift::Type, swift::Type, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) (.cold.30) + 0
8  swift-frontend           0x000000010318d134 swift::constraints::ConstraintSystem::simplifySameShapeConstraint(swift::Type, swift::Type, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 2152
9  swift-frontend           0x000000010319291c swift::constraints::ConstraintSystem::simplifyConstraint(swift::constraints::Constraint const&) + 2316
10 swift-frontend           0x00000001031a29dc swift::constraints::ConstraintSystem::simplify() + 404
11 swift-frontend           0x00000001031349b0 swift::constraints::TypeVariableBinding::attempt(swift::constraints::ConstraintSystem&) const + 604
12 swift-frontend           0x00000001031bbc50 swift::constraints::BindingStep<swift::constraints::TypeVarBindingProducer>::take(bool) + 548
13 swift-frontend           0x00000001031a4b8c swift::constraints::ConstraintSystem::solveImpl(llvm::SmallVectorImpl<swift::constraints::Solution>&) + 464
14 swift-frontend           0x000000010323f4a0 swift::constraints::ConstraintSystem::salvage() + 252
15 swift-frontend           0x00000001031a636c swift::constraints::ConstraintSystem::solve(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 704
16 swift-frontend           0x0000000103313ee0 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 320
17 swift-frontend           0x0000000103313d48 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 184
18 swift-frontend           0x0000000103313c08 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::constraints::ContextualTypeInfo, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 84
19 swift-frontend           0x00000001033ef2b0 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 268
20 swift-frontend           0x00000001033f2148 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 328
21 swift-frontend           0x00000001033f0804 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
22 swift-frontend           0x00000001033efe90 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1028
23 swift-frontend           0x000000010379367c llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest>(swift::TypeCheckFunctionBodyRequest const&) + 392
24 swift-frontend           0x0000000103793418 llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckFunctionBodyRequest, (void*)0>(swift::TypeCheckFunctionBodyRequest const&) + 120
25 swift-frontend           0x00000001036db550 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 52
26 swift-frontend           0x0000000103429188 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 340
27 swift-frontend           0x000000010342b4c8 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 388
28 swift-frontend           0x000000010342b258 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 84
29 swift-frontend           0x0000000103428f4c swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 44
30 swift-frontend           0x0000000102491394 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_7>(long, swift::SourceFile&) + 16
31 swift-frontend           0x000000010248bffc swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 76
32 swift-frontend           0x000000010248bf90 swift::CompilerInstance::performSema() + 76
33 swift-frontend           0x0000000102308f74 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
34 swift-frontend           0x00000001022fbbcc swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2848
35 swift-frontend           0x0000000102180ba4 swift::mainEntry(int, char const**) + 3220
36 dyld                     0x00000001805c7f28 start + 2236
  • On command line an error is reported, then a fatalError
<projectpath>/Sources/main.swift:60:5: error: global function 'foo(named:fn:)' requires the type packs 'each Arg' and 'String' have the same shape
    foo(
    ^
<projectpath>/Sources/main.swift:61:16: error: extra argument in call
        named: repeat returnEmptyString((each Arg).self),
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: fatalError

Expected behavior
Compiler doesn't crash and produce an error message.

Environment

  • Toolchain : swift-5.9-DEVELOPMENT-SNAPSHOT-2023-07-25-a for macOS
  • Swift compiler version info: swift-driver version: 1.85 Apple Swift version 5.9 (swiftlang-5.9.0.120.7 clang-1500.0.34.3)
  • Xcode Version 15.0 beta 4 (15A5195m)
  • Deployment target: arm64-apple-macosx13.0

Weird/Interesting things
Using the same snippet, try to exchange the order of foo generic parameters like this : func foo<each Name: StringProtocol, each Arg>, the compiler doesn't crash anymore and produce a different error message.

Xcode:

<projectpath>/Sources/main.swift:62:13: error: pack expansion requires that 'each Arg' and '/* shape: each Arg */ repeat String' have the same shape
        fn: fn
            ^
<projectpath>/Sources/main.swift:60:5: error: global function 'foo(named:fn:)' requires the type packs '/* shape: each Arg */ repeat String' and 'each Arg' have the same shape
    foo(
    ^

Command line still produce fatalError but the same error message:

<projectpath>/Sources/main.swift:62:13: error: pack expansion requires that 'each Arg' and 'Pack{/* shape: each Arg */ repeat String}' have the same shape
        fn: fn
            ^
<projectpath>/Sources/main.swift:60:5: error: global function 'foo(named:fn:)' requires the type packs 'String' and 'each Arg' have the same shape
    foo(
    ^
error: fatalError

Finally: making named parameter a tuple, never result in a crash but inverting the generic parameter still product different error messages.

@martialln martialln added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Aug 1, 2023
@slavapestov slavapestov self-assigned this Aug 22, 2023
@slavapestov
Copy link
Contributor

It no longer crashes for me on main, but I get this diagnostic:

circ.swift:19:5: error: type of expression is ambiguous without a type annotation
    foo(
    ^~~~

@xedin Is this a known issue?

@martialln
Copy link
Author

You are right, I have built my test project with the main nightly and it does not crash anymore.
It still does in the 5.9 nightly which is 1 day older.

Will try to torture this piece of code again soon

@martialln
Copy link
Author

martialln commented Aug 22, 2023

I found another crash, I am not sure if I should create another issue.

@discardableResult
func foo<each Arg, each Name: StringProtocol>(
    named: (repeat each Name),
    fn: @escaping (repeat each Arg) -> Void
) -> (repeat ((each Arg).Type, each Name)) {
    let tuple = (repeat ((each Arg).self, each named))
    return tuple
}

Note that making the type of tuple explicit doesn't help
let tuple: (repeat ((each Arg).Type, each Name)) = (repeat ((each Arg).self, each named))

with stack trace:

Assertion failed: (type->isLegalFormalType()), function isTypeMetadataAccessible, file SIL.cpp, line 107.
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-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file <projectpath>/Sources/main.swift -emit-dependencies-path <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.d -emit-const-values-path <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.swiftconstvalues -emit-reference-dependencies-path <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.swiftdeps -serialize-diagnostics-path <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.dia -target arm64-apple-macos13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -I <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Products/Debug -I /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Products/Debug -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path <home>/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-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -serialize-debugging-options -package-name variadic_generic_tests -const-gather-protocols-file <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/vgtests_const_extract_protocols.json -enable-experimental-feature VariadicGenerics -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file <home>/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc <projectpath> -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I<home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Products/Debug/include -Xcc -I<home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/DerivedSources-normal/arm64 -Xcc -I<home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/DerivedSources/arm64 -Xcc -I<home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name vgtests -frontend-parseable-output -disable-clang-spi -disable-availability-checking -target-sdk-version 14.0 -target-sdk-name macosx14.0 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a.xctoolchain/usr/local/lib/swift/host/plugins -o <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Build/Intermediates.noindex/vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.o -index-unit-output-path /vgtests.build/Debug/vgtests.build/Objects-normal/arm64/main.o -index-store-path <home>/Library/Developer/Xcode/DerivedData/variadic-generic-tests-cvgynpsdlgsgsybdcgrgztjrnoge/Index.noindex/DataStore -index-system-modules
1.	Apple Swift version 5.9-dev (LLVM fd38736063c15cd, Swift a533c63d783f5b8)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "<projectpath>/Sources/main.swift")
4.	While silgen emitFunction SIL function "@$s7vgtests3foo5named2fnxm_q_txQp_tq_xQp_t_yxxQpctRvzRv_q_RhzSyR_r0_lF".
 for 'foo(named:fn:)' (at <projectpath>/Sources/main.swift:45:1)
5.	While verifying SIL function "@$s7vgtests3foo5named2fnxm_q_txQp_tq_xQp_t_yxxQpctRvzRv_q_RhzSyR_r0_lF".
 for 'foo(named:fn:)' (at <projectpath>/Sources/main.swift:45: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           0x0000000109f00d80 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109f00124 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109f013c0 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001ab93aa24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001ab90bc28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001ab819ae8 abort + 180
6  libsystem_c.dylib        0x00000001ab818e44 err + 0
7  swift-frontend           0x000000010a1ea7a4 swift::getGenericSignatureLinkage(swift::CanGenericSignature) (.cold.1) + 0
8  swift-frontend           0x0000000105d8a9f8 swift::getGenericSignatureLinkage(swift::CanGenericSignature) + 0
9  swift-frontend           0x0000000105d8af30 isTypeMetadataForLayoutAccessible(swift::SILModule&, swift::SILType) + 332
10 swift-frontend           0x0000000105d8ae60 isTypeMetadataForLayoutAccessible(swift::SILModule&, swift::SILType) + 124
11 swift-frontend           0x0000000105d8add0 swift::SILModule::isTypeMetadataForLayoutAccessible(swift::SILType) + 304
12 swift-frontend           0x0000000105d8ac80 swift::SILModule::isTypeABIAccessible(swift::SILType, swift::TypeExpansionContext) + 268
13 swift-frontend           0x0000000105d9fcb0 swift::SILFunction::isTypeABIAccessible(swift::SILType) const + 52
14 swift-frontend           0x0000000105ee300c swift::SILInstructionVisitor<(anonymous namespace)::SILVerifier, void>::visit(swift::SILInstruction*) + 42100
15 swift-frontend           0x0000000105ed8a10 (anonymous namespace)::SILVerifier::visitSILBasicBlock(swift::SILBasicBlock*) + 1160
16 swift-frontend           0x0000000105ed6f14 (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 8980
17 swift-frontend           0x0000000105ed0c74 swift::SILFunction::verify(swift::SILPassManager*, bool, bool, bool) const + 204
18 swift-frontend           0x000000010565be84 swift::Lowering::SILGenModule::postEmitFunction(swift::SILDeclRef, swift::SILFunction*) + 240
19 swift-frontend           0x000000010565a2b8 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 4000
20 swift-frontend           0x000000010565bf88 swift::Lowering::SILGenModule::emitOrDelayFunction(swift::SILDeclRef) + 204
21 swift-frontend           0x0000000105659300 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 164
22 swift-frontend           0x000000010565e738 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 976
23 swift-frontend           0x000000010572d08c swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 148
24 swift-frontend           0x000000010566153c llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 396
25 swift-frontend           0x000000010565ef20 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 100
26 swift-frontend           0x00000001050f49fc swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 556
27 swift-frontend           0x0000000105103dd4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
28 swift-frontend           0x00000001050f7584 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 740
29 swift-frontend           0x00000001050f6508 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2496
30 swift-frontend           0x0000000104f2ff04 swift::mainEntry(int, char const**) + 2144
31 dyld                     0x00000001ab5b3f28 start + 2236

The snippets which return directly the tuple instead of putting it in a variable works as expected

@discardableResult
func foo<each Arg, each Name: StringProtocol>(
    named: (repeat each Name),
    fn: @escaping (repeat each Arg) -> Void
) -> (repeat ((each Arg).Type, each Name)) {
    return (repeat ((each Arg).self, each named))
}

func bar(int: Int, bool: Bool) -> Void {}
print(foo(named: ("a", "b"), fn: bar))

Output:
((Swift.Int, "a"), (Swift.Bool, "b"))

Environment

Toolchain : swift-DEVELOPMENT-SNAPSHOT-2023-08-12-a for macOS
Xcode Version 15.0 beta 6 (15A5219j)
Target: arm64-apple-macosx13.0

@xedin
Copy link
Contributor

xedin commented Aug 22, 2023

I don't think I saw this one before.

@martialln
Copy link
Author

martialln commented Aug 24, 2023

I just tested with swift-DEVELOPMENT-SNAPSHOT-2023-08-23-a:

  • First post crash is fixed (fix from main I suppose)
  • Previous post crash:
@discardableResult
func foo<each Name: StringProtocol, each Arg>(
    named: (repeat each Name),
    fn: @escaping (repeat each Arg) -> Void
) -> (repeat ((each Arg).Type, each Name)) {
    let tuple: (repeat ((each Arg).Type, each Name)) = (repeat ((each Arg).self, each named))
    return tuple
    //return (repeat ((each Arg).self, each named))
}
func bar(int: Int, bool: Bool) -> Void {}
print(foo(named: ("a", "b"), fn: bar))

Version 15.0 beta 7 (15A5229h)
Default toolchain : Compile and run correctly
swift-DEVELOPMENT-SNAPSHOT-2023-08-23-a: still crashing

One line return statement is working in all cases.

@simanerush simanerush added parameter packs Feature → generics: Parameter packs assertion failure Bug → crash: An assertion failure SILGen Area → compiler: The SIL generation stage labels Dec 6, 2023
@jckarter jckarter removed the SILGen Area → compiler: The SIL generation stage label Dec 6, 2023
@vanvoorden
Copy link
Contributor

@simanerush I saw a similar error in #71258.

@slavapestov
Copy link
Contributor

I have this commit sitting in my local repo since forever. @simanerush feel free to land it with a test case. I don't even remember if it works though!

commit f0e7f74746b9dd5c3eb4cbc894ee2754c96f6186
Author: Slava Pestov <[email protected]>
Date:   Tue Jan 2 17:08:22 2024 -0500

    SIL: Adjust isTypeMetadataForLayoutAccessible() to visit more lowered positions
    
    Fixes part of https://github.com/apple/swift/issues/67645, rdar://119267393

diff --git a/lib/SIL/IR/SIL.cpp b/lib/SIL/IR/SIL.cpp
index 6186d24c075..98ea4edf2d2 100644
--- a/lib/SIL/IR/SIL.cpp
+++ b/lib/SIL/IR/SIL.cpp
@@ -249,6 +249,24 @@ static bool isTypeMetadataForLayoutAccessible(SILModule &M, SILType type) {
   if (type.is<AnyMetatypeType>())
     return true;
 
+  //   - pack expansion types
+  if (auto expansionType = type.getAs<PackExpansionType>()) {
+    auto patternType = SILType::getPrimitiveType(expansionType.getPatternType(),
+                                                 type.getCategory());
+    return isTypeMetadataForLayoutAccessible(M, patternType);
+  }
+
+  //   - lowered pack types
+  if (auto packType = type.getAs<SILPackType>()) {
+    for (auto eltType : packType.getElementTypes()) {
+      if (!isTypeMetadataForLayoutAccessible(M,
+                                             SILType::getPrimitiveAddressType(eltType)))
+        return false;
+    }
+
+    return true;
+  }
+
   // Otherwise, check that we can fetch the type metadata.
   return M.isTypeMetadataAccessible(type.getASTType());
 }

@simanerush
Copy link
Member

@slavapestov Thank you, I'll take a look!

@simanerush simanerush self-assigned this Jan 31, 2024
simanerush pushed a commit to simanerush/swift that referenced this issue Feb 4, 2024
@AnthonyLatsis AnthonyLatsis added type checker Area → compiler: Semantic analysis generics Feature: generic declarations and types crash Bug: A crash, i.e., an abnormal termination of software compiler The Swift compiler itself swift 5.9 expressions Feature: expressions pack expansions Feature → expressions: Pack expansion expressions and removed triage needed This issue needs more specific labels labels Feb 7, 2024
@AnthonyLatsis
Copy link
Collaborator

@simanerush Could you open a new issue for the tuple crash? The original issue is unrelated and fixed.

@simanerush
Copy link
Member

@AnthonyLatsis will do, thank you!

simanerush pushed a commit to simanerush/swift that referenced this issue Feb 8, 2024
hborla pushed a commit that referenced this issue Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assertion failure Bug → crash: An assertion failure 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 expressions Feature: expressions generics Feature: generic declarations and types pack expansions Feature → expressions: Pack expansion expressions parameter packs Feature → generics: Parameter packs swift 5.9 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

7 participants