Files
swift-mirror/test/Interpreter/variadic_generic_tuples.swift
Slava Pestov f17baefd7a IRGen: Fix miscompile when a generic parameter is fixed to a tuple containing a pack
If you had something like:

    struct G<T> {
      func f<each U>(_: repeat each U) where T == (repeat each U) {}
    }

We would fulfill 'each U' from the metadata for 'G<(repeat each U)>',
by taking apart the tuple metadata for `(repeat each U)` and forming
a pack.

However this code path was only intended to kick in for a tuple
conformance witness thunk. In the general case, this optimization
is not correct, because if 'each U' is substituted with a
one-element pack, the generic argument of `G<(repeat each U)>` is
just that one element's metadata, and not a tuple. In fact, we
cannot distinguish the one-element tuple case, because the wrapped
element may itself be a tuple.

The fix is to just split off FulfillmentMap::searchTupleTypeMetadata()
from searchTypeMetadata(), and only call the former when we're in
the specific situation that requires it.

- Fixes https://github.com/swiftlang/swift/issues/78191.
- Fixes rdar://problem/135325886.
2025-05-16 17:34:11 -04:00

104 lines
4.1 KiB
Swift

// RUN: %target-run-simple-swift
// REQUIRES: executable_test
import StdlibUnittest
var tuples = TestSuite("VariadicGenericTuples")
// Metadata instantiation for tuples containing pack expansions
func makeTuple<each T>(_: repeat (each T).Type) -> Any.Type {
return (repeat Array<each T>).self
}
tuples.test("makeTuple") {
expectEqual("()", _typeName(makeTuple()))
// Note that we unwrap the one-element tuple!
expectEqual("Swift.Array<Swift.Int>", _typeName(makeTuple(Int.self)))
expectEqual("(Swift.Array<Swift.Int>, Swift.Array<Swift.String>)", _typeName(makeTuple(Int.self, String.self)))
expectEqual("(Swift.Array<Swift.Int>, Swift.Array<Swift.String>, Swift.Array<Swift.Float>)", _typeName(makeTuple(Int.self, String.self, Float.self)))
}
func makeTuple2<each T>(_: repeat (each T).Type) -> Any.Type {
return (Int, repeat Array<each T>).self
}
tuples.test("makeTuple2") {
// Note that we unwrap the one-element tuple!
expectEqual("Swift.Int", _typeName(makeTuple2()))
expectEqual("(Swift.Int, Swift.Array<Swift.Bool>)", _typeName(makeTuple2(Bool.self)))
expectEqual("(Swift.Int, Swift.Array<Swift.Bool>, Swift.Array<Swift.Character>)", _typeName(makeTuple2(Bool.self, Character.self)))
expectEqual("(Swift.Int, Swift.Array<Swift.Bool>, Swift.Array<Swift.Character>, Swift.Array<Swift.Double>)", _typeName(makeTuple2(Bool.self, Character.self, Double.self)))
}
func makeTuple3<each T, each U>(t: repeat (each T).Type, u: repeat (each U).Type) -> Any.Type {
return (repeat each T, repeat each U).self
}
tuples.test("makeTuple3") {
expectEqual("()", _typeName(makeTuple3()))
// Note that we unwrap the one-element tuple!
expectEqual("Swift.Int", _typeName(makeTuple3(t: Int.self)))
expectEqual("Swift.Int", _typeName(makeTuple3(u: Int.self)))
expectEqual("(Swift.Int, Swift.Float)", _typeName(makeTuple3(t: Int.self, u: Float.self)))
}
func makeTuple<each Element>(
_ element: repeat each Element
) -> (repeat each Element) {
return (repeat each element)
}
func expandTupleElements<each T: Equatable>(_ value: repeat each T) {
let values = makeTuple(repeat each value)
_ = (repeat expectEqual(each value, each values))
}
tuples.test("expandTuple") {
expandTupleElements(1, "hello", true)
}
func tupleLabelMix<each T, each U>(t: repeat (each T).Type, u: repeat (each U).Type) -> Any.Type {
return (Float, hello: Int, repeat each T, swift: String, repeat each U, Bool, world: UInt8).self
}
func oneElementLabeledTuple<each T>(t: repeat (each T).Type) -> Any.Type {
return (label: Int, repeat each T).self
}
tuples.test("labels") {
expectEqual("(Swift.Float, hello: Swift.Int, swift: Swift.String, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix()))
expectEqual("(Swift.Float, hello: Swift.Int, swift: Swift.String, Swift.Double, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix(u: Double.self)))
expectEqual("(Swift.Float, hello: Swift.Int, swift: Swift.String, Swift.Double, Swift.Int32, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix(u: Double.self, Int32.self)))
expectEqual("(Swift.Float, hello: Swift.Int, Swift.Character, swift: Swift.String, Swift.Double, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix(t: Character.self, u: Double.self)))
expectEqual("(Swift.Float, hello: Swift.Int, Swift.Character, Swift.Substring, swift: Swift.String, Swift.Double, Swift.Int32, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix(t: Character.self, Substring.self, u: Double.self, Int32.self)))
// FIXME: One-element labeled tuples
expectEqual("Swift.Int", _typeName(oneElementLabeledTuple()))
expectEqual("(label: Swift.Int, Swift.String)", _typeName(oneElementLabeledTuple(t: String.self)))
}
// https://github.com/swiftlang/swift/issues/78191
tuples.test("fulfillment") {
struct S<T> {
let t: T
func f<each A, each B>(_ b: S<(repeat each B)>) -> S<(repeat each A, repeat each B)>
where T == (repeat each A) {
return S<(repeat each A, repeat each B)>(t: (repeat each t, repeat each b.t))
}
}
let s = S(t: "hello")
expectEqual("S<(String, Int, Int)>(t: (\"hello\", 1, 2))", String(describing: s.f(S(t: (1, 2)))))
}
runAllTests()