Skip to content

embedded: print an error for class existentials where not all protocols of a composition are class bound #77066

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
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
16 changes: 15 additions & 1 deletion lib/SIL/Utils/InstructionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/NullablePtr.h"
Expand Down Expand Up @@ -671,7 +672,20 @@ RuntimeEffect swift::getRuntimeEffect(SILInstruction *inst, SILType &impactType)
RuntimeEffect::MetaData | RuntimeEffect::Existential;

case SILInstructionKind::InitExistentialRefInst:
impactType = inst->getOperand(0)->getType();
impactType = cast<InitExistentialRefInst>(inst)->getType();
// Make sure to get a diagnostic error in embedded swift for class existentials
// where not all protocols of a composition are class bound. For example:
// let existential: any ClassBound & NotClassBound = MyClass()
// In future we might support this case and then we can remove this check.
for (auto protoRef : cast<InitExistentialRefInst>(inst)->getConformances()) {
if (protoRef.isConcrete()) {
ProtocolConformance *conf = protoRef.getConcrete();
if (isa<NormalProtocolConformance>(conf) &&
!conf->getProtocol()->requiresClass()) {
return RuntimeEffect::MetaData | RuntimeEffect::Existential;
}
}
}
return RuntimeEffect::MetaData | RuntimeEffect::ExistentialClassBound;

case SILInstructionKind::InitExistentialMetatypeInst:
Expand Down
28 changes: 28 additions & 0 deletions test/embedded/existential-composition.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %target-swift-emit-ir -parse-as-library -module-name main -verify %s -enable-experimental-feature Embedded -wmo

// REQUIRES: OS=macosx || OS=linux-gnu

protocol ClassBound: AnyObject {
func foo()
}

protocol OtherProtocol {
func bar()
}

class MyClass: ClassBound, OtherProtocol {
func foo() { print("MyClass.foo()") }
func bar() { print("MyClass.bar()") }
}

// Currently we don't support this
func test(existential: any ClassBound & OtherProtocol) {
}

@main
struct Main {
static func main() {
test(existential: MyClass()) // expected-error {{cannot use a value of protocol type 'any ClassBound & OtherProtocol' in embedded Swift}}
// expected-note@-4 {{called from here}}
}
}