swiftlang / swift

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

`extension S<Bool, Int>` compiles as `extension S` for `struct S<each T>` #70432

Open jepers opened 9 months ago

jepers commented 9 months ago

Description

The attached small program involving a generic struct S with a type parameter pack and an extension S<Bool, Int> { ... } should not compile, but it compiles without errors and runs, as if the extension had been extension S { ... }.

Reproduction

% swiftc --version
swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: x86_64-apple-macosx14.0
% cat > test.swift
struct S<each T> {
  typealias Storage = (repeat each T)
  let elements: (repeat each T)

  init(_ element: repeat each T) {
    self.elements = (repeat each element)
  }
}

extension S<Bool, Int> {
  func foo() { print("Hmm:", type(of: self), "...") }
}

func testS() {
  let e = UInt8(123)
  let s = S(e, e, e)
  print(type(of: s))
  s.foo() // Works, even though S<UInt8, UInt8, UInt8> != S<Bool, Int>
}

testS()
^C
% swiftc test.swift && ./test
S<Pack{UInt8, UInt8, UInt8}>
Hmm: S<Pack{UInt8, UInt8, UInt8}> ...

Expected behavior

Should result in a compile time error.

Environment

% swiftc --version
swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: x86_64-apple-macosx14.0

Additional information

https://forums.swift.org/t/extensions-and-variadic-generic-types/68955

slavapestov commented 9 months ago

We should reject this until the general case is implemented:

extension S where (repeat each T) == (...) { ... }