Skip to content

Same-element requirements are not yet supported #74979

Open
@rayx

Description

@rayx

Description

When calling a variadic generic type's method on a specialized version of that type, compiler emits error:

error: pack expansion requires that 'each T' and 'Foo, Bar' have the same shape

Reproduction

Put the following code in a swift package and run swift build:

// 1. Entities

protocol Entity {
    var name: String { get }
}

struct Foo: Entity {
    var name: String = "foo instance"
}

struct Bar: Entity {
    var name: String = "bar instance"
}

// 2. DataStoreChange (it's a variadic generic type)

struct DataStoreChange<each T: Entity> {
    var changedEntities: (repeat [each T])

    // Note: I'd like to use the commented line, but that would cause caompier crash
    // (see issue #74960). So I'll work around the issue by just return self.
    // This is ok because the purpose of the code is to demonstrate another issue.
    func combine(another: Self) -> Self {
        //Self(changedEntities: repeat (each changedEntities + each another.changedEntities))
        self
    }

    // other methods ...
}

// 3. DataStoreChangeV1 (it's a specicalized version of DataStoreChange)

typealias DataStoreChangeV1 = DataStoreChange<Foo, Bar>

extension DataStoreChangeV1 {
    func add(bars: [Bar]) -> DataStoreChangeV1 {
        let tmp = DataStoreChangeV1(changedEntities: ([], bars))
        return combine(another: tmp) // error: pack expansion requires that 'each T' and 'Foo, Bar' have the same shape
    }
}

swift build output:

Building for debugging...
/var/tmp/sandbox/variadicBug2/Sources/variadicBug2/variadicBug2.swift:41:16: error: pack expansion requires that 'each T' and 'Foo, Bar' have the same shape
        return combine(another: tmp) // error: pack expansion requires that 'each T' and 'Foo, Bar' have the same shape

PS: don't forget to add macOS requirement .macOS(.v14) to Package.swift.

Expected behavior

I think the code should compile.

Environment

% swift --version
Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-darwin23.2.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfgeneric constraintsFeature → generics: generic constraintsgenericsFeature: generic declarations and typesparameter packsFeature → generics: Parameter packsswift 6.1type checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions