Conform variadic types to protocols with initializers

I am unable to conform a variadic type to a protocol requiring a simple initializer (i.e. not referencing the type pack). Let's see with a simple example:

struct Person<each C> {
  var name: String
  var characteristics: (repeat each C)
}

The previous code compiles fine and then I can write:

let personA = Person(name: "John")
let personB = Person(name: "Mary", characteristic: Creative())

I want to be able to initialize a Person with a String literal which will produce a Person< > (i.e. without characteristics).

extension Person: ExpressibleByStringLiteral {
  init(stringLiteral value: String) {
    self.init(name: value) // ❌ Doesn't compile
  }
}

Even when I try to specify each C in the extension I am unable to do so with error Pack expansion requires that 'each C' and '' have the same shape.

extension Person< >: ExpressibleByStringLiteral {
  // ...
}
2 Likes

I suggest this might work for you

extension Person: ExpressibleByStringLiteral where each C: ExpressibleByStringLiteral  {
  // ...
}

Sadly that doesn't compile, @Genaro-Chris, since a pack reference requires expansion with repeat. Besides, your suggestion doesn't really tackle the real underlying issue.

I believe the problem is that the compiler is unable make a conditional conformance to an empty pack when each C is not referenced.

This compiles:

struct Person<each C> {
    var name: String
    var characteristics: (repeat each C)
}

extension Person< >: ExpressibleByStringLiteral {
    init(stringLiteral value: String) {
        self.name = value
        self.characteristics = () as! (repeat each C)
    }
}

let x: Person< > = "Tom Bombadil"
3 Likes

That does indeed compile and work. Thank you for the suggestion @xwu! However, it also generates the following warning (which is not true, by the way):

Cast from '()' to unrelated type '(repeat each C)' always fails

In our company we have a "zero-warnings to the CI" policy. Do you know of any way to mute that warning?

1 Like

One way is unsafeBitCast.

A safer way is this function:

func hardCast<T, U>(_ t: T, to: U.Type) -> U {
  return t as! U
}

Which lets you write the following:

self.characteristics = hardCast((), to: (repeat each C).self)

If you wanted to get (overly) fancy, you could even supply a default value of U.self for the to: argument. That way, at call-sites like this where the return type can be inferred, you could simply write hardCast(()).

3 Likes

Unfortunately an extension of a specialized type doesn't work correctly when the type is variadic: `extension S<Bool, Int>` compiles as `extension S` for `struct S<each T>` · Issue #70432 · apple/swift · GitHub

So extension Person< >: ExpressibleByStringLiteral defines an unconditional conformance, which will crash if the protocol requirement is invoked on a different specialization of Person with a non-empty pack.

This is a bug because the cast can succeed when C is the empty pack. Please file a bug.

However, this is not a very good fix because as I wrote above it introduces a type safety hole.

4 Likes

Done. The issue is here.

@Slava_Pestov I have two further questions:

  • Any reason why the Swift 5.10 toolchain have so many issues with variadic generics and parameter packs? I am unable to run my small project in 5.10 but I have no problems in 5.9
  • What does it mean the statement "Implemented (Swift Next)" as appears in the Pack Iteration proposal? It doesn't seem to be available in the 5.10 toolchain.
2 Likes

I'm not aware of any regressions from 5.9 to 5.10 with the parameter pack implementation. One difference is that developer toolchains are built with asserts. In some manifestations of Fix openType() handling of parameter packs by slavapestov · Pull Request #70457 · apple/swift · GitHub, the code worked in a noassert build even though the invariant was violated. I don't know if that's what you're seeing though.

I believe pack iteration is on the main branch but not release/5.10. Can you try a main developer snapshot?

2 Likes

I just tried and you are correct, it is in main but not 5.10. Will Pack iteration be part of 5.10?

Yes, it seems assertions are to blame.

Assertion failed: ((!replacement || replacement->hasError() || gp->isParameterPack() == replacement->is<PackType>()) && "replacement for pack parameter must be a pack type"), function operator(), file SubstitutionMap.cpp, line 237.
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-12-21-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text/Tests/Models/Core/EntitiesTests.swift -emit-dependencies-path /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/Objects-normal/arm64/EntitiesTests.d -emit-const-values-path /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/Objects-normal/arm64/EntitiesTests.swiftconstvalues -emit-reference-dependencies-path /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/Objects-normal/arm64/EntitiesTests.swiftdeps -serialize-diagnostics-path /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/Objects-normal/arm64/EntitiesTests.dia -target arm64-apple-macos14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -I /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Products/Debug -I /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Products/Debug/PackageFrameworks -F /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Products/Debug/PackageFrameworks -F /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Products/Debug -F /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -no-color-diagnostics -enable-testing -g -module-cache-path /Users/marcos/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -profile-generate -profile-coverage-mapping -swift-version 5 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -external-plugin-path /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins#/Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -serialize-debugging-options -package-name raycast_text -const-gather-protocols-file /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/Objects-normal/arm64/ModelsTests_const_extract_protocols.json -empty-abi-descriptor -validate-clang-modules-once -clang-build-session-file /Users/marcos/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-12-21-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Products/Debug/include -Xcc -I/Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/DerivedSources-normal/arm64 -Xcc -I/Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/DerivedSources/arm64 -Xcc -I/Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -module-name ModelsTests -frontend-parseable-output -disable-clang-spi -target-sdk-version 14.2 -target-sdk-name macosx14.2 -external-plugin-path /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/lib/swift/host/plugins#/Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-15.1.0-Release.Candidate.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-12-21-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-12-21-a.xctoolchain/usr/local/lib/swift/host/plugins -parse-as-library -o /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Build/Intermediates.noindex/raycast-text.build/Debug/ModelsTests.build/Objects-normal/arm64/EntitiesTests.o -index-unit-output-path /raycast-text.build/Debug/ModelsTests.build/Objects-normal/arm64/EntitiesTests.o -index-store-path /Users/marcos/Library/Developer/Xcode/DerivedData/raycast-text-gnpqynpmanzrgnbabkadlsbdfyqh/Index.noindex/DataStore -index-system-modules
1.	Apple Swift version 5.11-dev (LLVM 13124099c3f0229, Swift d6871edc839adec)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text/Tests/Models/Core/EntitiesTests.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(ModelsTests.(file).EntitiesTests.testBuilders()@/Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text/Tests/Models/Core/EntitiesTests.swift:40:8)
5.	While type-checking statement at [/Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text/Tests/Models/Core/EntitiesTests.swift:40:30 - line:43:3] RangeText="{
    let template = Template.TextBlock(inline: Template.Text(marks: Template.Bold(), Template.Italic()))
    let node = TextBlock(inline: Text("Hello "), Text("World", .bold))
  "
6.	While type-checking declaration 0x12397d898 (at /Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text/Tests/Models/Core/EntitiesTests.swift:42:5)
7.	While evaluating request PatternBindingEntryRequest((unknown decl), 0)
8.	While type-checking expression at [/Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text/Tests/Models/Core/EntitiesTests.swift:42:16 - line:42:70] RangeText="TextBlock(inline: Text("Hello "), Text("World", .bold)"
9.	While type-checking-target starting at /Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text/Tests/Models/Core/EntitiesTests.swift:42:16
10.	While evaluating request LookupConformanceInModuleRequest(looking up conformance to Models.(file)._NodeInline@/Users/marcos/Downloads/Raycast/Editor/Variadic/raycast-text/Sources/Models/Core/Node.swift:43:17 for Text<_> in ModelsTests)
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           0x000000010a3d486c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010a3d2fc8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010a3d4ebc SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001874c9a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000187499cc0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001873a5a40 abort + 180
6  libsystem_c.dylib        0x00000001873a4d30 err + 0
7  swift-frontend           0x000000010a8feb68 void llvm::function_ref<void (swift::GenericTypeParamType*, bool)>::callback_fn<swift::SubstitutionMap::get(swift::GenericSignature, swift::InFlightSubstitution&)::$_1>(long, swift::GenericTypeParamType*, bool) (.cold.4) + 0
8  swift-frontend           0x0000000106738e98 swift::Type llvm::function_ref<swift::Type (swift::SubstitutableType*)>::callback_fn<swift::MapTypeOutOfContext>(long, swift::SubstitutableType*) + 0
9  swift-frontend           0x000000010660ad68 swift::GenericSignatureImpl::forEachParam(llvm::function_ref<void (swift::GenericTypeParamType*, bool)>) const + 744
10 swift-frontend           0x00000001067370fc swift::SubstitutionMap::get(swift::GenericSignature, swift::InFlightSubstitution&) + 156
11 swift-frontend           0x0000000106736fec swift::SubstitutionMap::get(swift::GenericSignature, llvm::function_ref<swift::Type (swift::SubstitutableType*)>, llvm::function_ref<swift::ProtocolConformanceRef (swift::CanType, swift::Type, swift::ProtocolDecl*)>) + 80
12 swift-frontend           0x0000000106772588 swift::TypeBase::getContextSubstitutionMap(swift::ModuleDecl*, swift::DeclContext const*, swift::GenericEnvironment*) + 120
13 swift-frontend           0x000000010662970c swift::LookupConformanceInModuleRequest::evaluate(swift::Evaluator&, swift::LookupConformanceDescriptor) const + 4544
14 swift-frontend           0x00000001066a1380 swift::SimpleRequest<swift::LookupConformanceInModuleRequest, swift::ProtocolConformanceRef (swift::LookupConformanceDescriptor), (swift::RequestFlags)17>::evaluateRequest(swift::LookupConformanceInModuleRequest const&, swift::Evaluator&) + 36
15 swift-frontend           0x0000000106646a78 llvm::Expected<swift::LookupConformanceInModuleRequest::OutputType> swift::Evaluator::getResultUncached<swift::LookupConformanceInModuleRequest>(swift::LookupConformanceInModuleRequest const&) + 268
16 swift-frontend           0x0000000106628458 swift::LookupConformanceInModuleRequest::OutputType swift::evaluateOrDefault<swift::LookupConformanceInModuleRequest>(swift::Evaluator&, swift::LookupConformanceInModuleRequest, swift::LookupConformanceInModuleRequest::OutputType) + 44
17 swift-frontend           0x0000000106627fc8 swift::ModuleDecl::lookupConformance(swift::Type, swift::ProtocolDecl*, bool) + 368
18 swift-frontend           0x0000000105f69e54 swift::constraints::ConstraintSystem::lookupConformance(swift::Type, swift::ProtocolDecl*) + 124
19 swift-frontend           0x0000000105e91800 swift::constraints::ConstraintSystem::simplifyConformsToConstraint(swift::Type, swift::ProtocolDecl*, swift::constraints::ConstraintKind, swift::constraints::ConstraintLocatorBuilder, swift::optionset::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>) + 1488
20 swift-frontend           0x0000000105e9dbd0 swift::constraints::ConstraintSystem::simplifyConformsToConstraint(swift::Type, swift::Type, swift::constraints::ConstraintKind, swift::constraints::ConstraintLocatorBuilder, swift::optionset::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>) + 176
21 swift-frontend           0x0000000105eb4fd4 swift::constraints::ConstraintSystem::simplifyConstraint(swift::constraints::Constraint const&) + 244
22 swift-frontend           0x0000000105ec5650 swift::constraints::ConstraintSystem::simplify() + 412
23 swift-frontend           0x0000000105e56584 swift::constraints::TypeVariableBinding::attempt(swift::constraints::ConstraintSystem&) const + 1104
24 swift-frontend           0x0000000105edd14c swift::constraints::BindingStep<swift::constraints::TypeVarBindingProducer>::take(bool) + 604
25 swift-frontend           0x0000000105ec7688 swift::constraints::ConstraintSystem::solveImpl(llvm::SmallVectorImpl<swift::constraints::Solution>&) + 464
26 swift-frontend           0x0000000105ec9a8c swift::constraints::ConstraintSystem::solve(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 76
27 swift-frontend           0x0000000105ec9634 swift::constraints::ConstraintSystem::solveImpl(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 488
28 swift-frontend           0x0000000105ec8df4 swift::constraints::ConstraintSystem::solve(swift::constraints::SyntacticElementTarget&, swift::FreeTypeVariableBinding) + 152
29 swift-frontend           0x000000010603e084 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 296
30 swift-frontend           0x000000010603df04 swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 184
31 swift-frontend           0x000000010603f078 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 124
32 swift-frontend           0x000000010603f334 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 384
33 swift-frontend           0x000000010612d770 swift::PatternBindingEntryRequest::evaluate(swift::Evaluator&, swift::PatternBindingDecl*, unsigned int) const + 1672
34 swift-frontend           0x00000001064f4350 llvm::Expected<swift::PatternBindingEntryRequest::OutputType> swift::Evaluator::getResultUncached<swift::PatternBindingEntryRequest>(swift::PatternBindingEntryRequest const&) + 380
35 swift-frontend           0x00000001064f40f8 llvm::Expected<swift::PatternBindingEntryRequest::OutputType> swift::Evaluator::getResultCached<swift::PatternBindingEntryRequest, (void*)0>(swift::PatternBindingEntryRequest const&) + 120
36 swift-frontend           0x00000001064ab9f4 swift::PatternBindingEntryRequest::OutputType swift::evaluateOrDefault<swift::PatternBindingEntryRequest>(swift::Evaluator&, swift::PatternBindingEntryRequest, swift::PatternBindingEntryRequest::OutputType) + 52
37 swift-frontend           0x0000000106082130 swift::ASTVisitor<(anonymous namespace)::DeclChecker, void, void, void, void, void, void>::visit(swift::Decl*) + 876
38 swift-frontend           0x000000010607e074 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 324
39 swift-frontend           0x000000010607df1c swift::TypeChecker::typeCheckDecl(swift::Decl*) + 128
40 swift-frontend           0x00000001061246fc swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 328
41 swift-frontend           0x0000000106122d00 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 136
42 swift-frontend           0x000000010612240c swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1136
43 swift-frontend           0x000000010658c700 llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest>(swift::TypeCheckFunctionBodyRequest const&) + 392
44 swift-frontend           0x000000010658c49c llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckFunctionBodyRequest, (void*)0>(swift::TypeCheckFunctionBodyRequest const&) + 120
45 swift-frontend           0x00000001064c9f4c swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 52
46 swift-frontend           0x0000000106633c64 swift::SourceFile::typeCheckDelayedFunctions() + 108
47 swift-frontend           0x000000010615cc34 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 276
48 swift-frontend           0x000000010615efec llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 388
49 swift-frontend           0x000000010615ed7c llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 84
50 swift-frontend           0x000000010615ca38 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 44
51 swift-frontend           0x0000000105148fdc bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_7>(long, swift::SourceFile&) + 16
52 swift-frontend           0x00000001051446b8 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 76
53 swift-frontend           0x000000010514464c swift::CompilerInstance::performSema() + 76
54 swift-frontend           0x0000000104f63f1c withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
55 swift-frontend           0x0000000104f57320 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 704
56 swift-frontend           0x0000000104f562e4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2632
57 swift-frontend           0x0000000104defce0 swift::mainEntry(int, char const**) + 2192
58 dyld                     0x00000001871190e0 start + 2360
1 Like
struct Person<each C> {
  var name: String
  var characteristics: (repeat each C)
}
extension Person<any ExpressibleByStringLiteral>: ExpressibleByStringLiteral  {
    init(stringLiteral value: String) {
        self.name = value
        self.characteristics = {} as! (repeat each C)
    }
}


func people() {
    var a: Person<Any> = ""
    var b: Person = a
    var c: Person<any ExpressibleByStringLiteral> = ""
    var d: Person<String> = ""
    b.name = d.name
}

For me this compiled (couple nights ago) and I was wondering about what is going on with var b here. Basically we have an unspecialized Person type becoming a specialized Person<Any> type? I thought it was odd but I haven't really touched variadic types often at all especially not in such a context. Nor have I touched parameter packs yet, this was the first time.

This is an existing language feature where you can omit generic arguments from a type annotation on a var if they can be inferred from the initial value expression. For example,

var a: Array<Int> = [1,2,3]
var b: Array = a
3 Likes