Files
swift-mirror/validation-test/Evolution/test_protocol_add_reparented.swift
Kavon Farvardin 006a0a494a Reparenting: require an extension
This change forces you to write ``@reparented` relationships
on an extension of a protocol, rather than on the protocol
itself.

The ProtocolConformance needs to be associated with some
GenericContext and IRGen expects it to be an ExtensionDecl.
That environment defines under what conditions the conformance
exists. We also need to define witnesses for the new parent's
requirements in an extension anyway, so it's a natural fit.

The previous workaround for this was kind of awful, as it'd
require searching all the protocol's extensions and "guess"
which extension they want to represent the conformance. While
we could try to synthesize an extension, there's two
challenges there:

1. Due to SuppressedAssociatedTypes, it's not so simple to
synthesize an unconstrained ExtensionDecl.
2. We currently rely on same-type requirements to pin the
associated types to particular witnesses of those requirements
in the extension. So it's not purely unconstrained! For example,

```
extension Seq: @reparented BorrowSeq where Iter == MyIter {}
```

The constraints that are disallowed (but not yet diagnosed)
are conditional conformance requirements, as the default
conformance for a reparenting cannot depend on those.

Thus, it's better that programmers to specify the extension.
2026-02-03 16:40:21 -08:00

66 lines
1.8 KiB
Swift

// RUN: %target-resilience-test --skip-application-back-deploy --additional-compile-flags '-enable-experimental-feature Reparenting'
// REQUIRES: executable_test
// REQUIRES: swift_feature_Reparenting
import StdlibUnittest
import protocol_add_reparented
var ProtocolAddReparentedTest = TestSuite("ProtocolAddReparented")
#if BEFORE
let clientVersion = 0
#else
let clientVersion = 1
#endif
// A type on the client side that is never updated for the new world
struct Eagle: Bird {
func eat(_ food: Int) -> Int { return food }
}
#if !BEFORE
// A type on the client side that was updated to be aware of the new-world, providing its own witnesses.
struct Plus100Eagle: Bird {
func eat(_ food: Int) -> Int { return food }
public func eatSeed(_ food: Int) -> Int { return eat(food + libraryVersion() + 100) }
public typealias Kind = UInt
public var seedKinds: UInt {
get { return UInt(libraryVersion() + 100) }
}
}
extension UInt: @retroactive AsInt {
public func asInt() -> Int { return Int(self) }
}
#endif
ProtocolAddReparentedTest.test("Some Types") {
let e = Eagle()
let ans = libraryVersion() + clientVersion
expectEqual(SomeType.feed(e, clientVersion), ans)
expectEqual(SomeType.count(e, clientVersion), ans)
#if !BEFORE
let 💯 = Plus100Eagle()
expectEqual(SomeType.feed(💯, clientVersion), ans + 100)
expectEqual(SomeType.count(💯, clientVersion), ans + 100)
#endif
}
ProtocolAddReparentedTest.test("Existential Types") {
let e = Eagle()
let ans = libraryVersion() + clientVersion
expectEqual(ExistentialType.feed(e, clientVersion), ans)
expectEqual(ExistentialType.count(e, clientVersion), ans)
#if !BEFORE
let 💯 = Plus100Eagle()
expectEqual(ExistentialType.feed(💯, clientVersion), ans + 100)
expectEqual(ExistentialType.count(💯, clientVersion), ans + 100)
#endif
}
runAllTests()