Skip to content

GenProto: drop conditional Copyable reqs from protocol conformance descriptors #72137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2113,17 +2113,28 @@ namespace {
if (!normal)
return;

std::optional<Requirement> scratchRequirement;
auto condReqs = normal->getConditionalRequirements();
SmallVector<Requirement, 2> condReqs;
{
// FIXME(kavon): probably need to emit the inverse requirements in the
// metadata so the runtime knows not to check for Copyable? For now
// filter them out.
auto origCondReqs = normal->getConditionalRequirements();
for (auto req : origCondReqs) {
if (req.getKind() == RequirementKind::Conformance &&
req.getProtocolDecl()->getInvertibleProtocolKind())
continue;
condReqs.push_back(req);
}
}

if (condReqs.empty()) {
// For a protocol P that conforms to another protocol, introduce a
// conditional requirement for that P's Self: P. This aligns with
// SILWitnessTable::enumerateWitnessTableConditionalConformances().
if (auto selfProto = normal->getDeclContext()->getSelfProtocolDecl()) {
auto selfType = selfProto->getSelfInterfaceType()->getCanonicalType();
scratchRequirement.emplace(RequirementKind::Conformance, selfType,
selfProto->getDeclaredInterfaceType());
condReqs = *scratchRequirement;
condReqs.emplace_back(RequirementKind::Conformance, selfType,
selfProto->getDeclaredInterfaceType());
}

if (condReqs.empty())
Expand Down
70 changes: 70 additions & 0 deletions test/Interpreter/moveonly_generics_casting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all -enable-experimental-feature NoncopyableGenerics) | %FileCheck %s
// RUN: %target-run-simple-swift(-O -Xfrontend -sil-verify-all -enable-experimental-feature NoncopyableGenerics) | %FileCheck %s

protocol P {
func speak()
}

extension P {
func speak() { print("hello") }
}

struct Noncopyable: ~Copyable {}
struct Ordinary {}

struct Dog<T: ~Copyable>: Copyable {}
extension Dog: P where T: Copyable {}

enum Cat<Left: ~Copyable, Right: ~Copyable>: Copyable {
case meows
init() { self = .meows }
}
extension Cat: P where Left: Copyable {}

func attemptCall(_ a: Any) {
if let value = a as? P {
value.speak()
return
}
print("failed to cast (attemptCall)")
}

defer { main() }
func main() {
// CHECK: hello
attemptCall(Dog<Ordinary>())

// FIXME: this is NOT suppose to succeed! (rdar://123466649)
// CHECK: hello
attemptCall(Dog<Noncopyable>())

// CHECK: hello
attemptCall(Cat<Ordinary, Noncopyable>())

// FIXME: this is NOT suppose to succeed! (rdar://123466649)
// CHECK: hello
attemptCall(Cat<Noncopyable, Ordinary>())

// FIXME: this should succeeed!!
// CHECK: failed to cast (test_radar124171788)
test_radar124171788(.nothing)
}

// coverage for rdar://124171788
enum Maybe<Wrapped: ~Copyable>: ~Copyable {
case just(Wrapped)
case nothing
}
extension Maybe: Copyable {}
extension Maybe: CustomStringConvertible {
var description: String {
"cast succeeded"
}
}
func test_radar124171788(_ v: Maybe<Int>) {
if let foo = v as? CustomDebugStringConvertible {
print("\(foo.debugDescription)")
return
}
print("failed to cast (test_radar124171788)")
}