Skip to content

Sema: Don't derive witnesses in swiftinterface files #38937

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
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
39 changes: 23 additions & 16 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3963,22 +3963,25 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
// Determine whether we can derive a witness for this requirement.
bool canDerive = false;

// Can a witness for this requirement be derived for this nominal type?
if (auto derivable = DerivedConformance::getDerivableRequirement(
nominal,
requirement)) {
if (derivable == requirement) {
// If it's the same requirement, we can derive it here.
canDerive = true;
} else {
// Otherwise, go satisfy the derivable requirement, which can introduce
// a member that could in turn satisfy *this* requirement.
auto derivableProto = cast<ProtocolDecl>(derivable->getDeclContext());
auto conformance =
TypeChecker::conformsToProtocol(Adoptee, derivableProto,
DC->getParentModule());
if (conformance.isConcrete()) {
(void)conformance.getConcrete()->getWitnessDecl(derivable);
auto *SF = DC->getParentSourceFile();
if (!(SF == nullptr || SF->Kind == SourceFileKind::Interface)) {
// Can a witness for this requirement be derived for this nominal type?
if (auto derivable = DerivedConformance::getDerivableRequirement(
nominal,
requirement)) {
if (derivable == requirement) {
// If it's the same requirement, we can derive it here.
canDerive = true;
} else {
// Otherwise, go satisfy the derivable requirement, which can introduce
// a member that could in turn satisfy *this* requirement.
auto derivableProto = cast<ProtocolDecl>(derivable->getDeclContext());
auto conformance =
TypeChecker::conformsToProtocol(Adoptee, derivableProto,
DC->getParentModule());
if (conformance.isConcrete()) {
(void)conformance.getConcrete()->getWitnessDecl(derivable);
}
}
}
}
Expand Down Expand Up @@ -4288,6 +4291,10 @@ ResolveWitnessResult ConformanceChecker::resolveWitnessViaDerivation(
ValueDecl *requirement) {
assert(!isa<AssociatedTypeDecl>(requirement) && "Use resolveTypeWitnessVia*");

auto *SF = DC->getParentSourceFile();
if (SF != nullptr && SF->Kind == SourceFileKind::Interface)
return ResolveWitnessResult::Missing;

// Find the declaration that derives the protocol conformance.
NominalTypeDecl *derivingTypeDecl = nullptr;
auto *nominal = Adoptee->getAnyNominal();
Expand Down
44 changes: 44 additions & 0 deletions test/ModuleInterface/DerivedWitnesses.swiftinterface
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s

// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.5 (swiftlang-1300.0.29.102 clang-1300.0.28.1)
// swift-module-flags: -target x86_64-apple-macosx11.0 -enable-objc-interop -enable-library-evolution -module-name DerivedWitnesses
import Swift

public enum HasSynthesizedEquals : Int {
case x
case y

public static func == (a: HasSynthesizedEquals, b: HasSynthesizedEquals) -> Bool
public func hash(into hasher: inout Hasher)
public var hashValue: Int {
get
}

public init?(rawValue: Int)
public typealias RawValue = Int
public var rawValue: Int {
get
}
}

public enum UsesDefaultEquals : Int {
case x
case y

public init?(rawValue: Int)
public typealias RawValue = Int
public var rawValue: Int {
get
}
}

// CHECK-LABEL: sil shared [transparent] [serialized] [thunk] [ossa] @$s16DerivedWitnesses20HasSynthesizedEqualsOSQAASQ2eeoiySbx_xtFZTW : $@convention(witness_method: Equatable) (@in_guaranteed HasSynthesizedEquals, @in_guaranteed HasSynthesizedEquals, @thick HasSynthesizedEquals.Type) -> Bool {
// CHECK: bb0(%0 : $*HasSynthesizedEquals, %1 : $*HasSynthesizedEquals, %2 : $@thick HasSynthesizedEquals.Type):
// CHECK: function_ref @$s16DerivedWitnesses20HasSynthesizedEqualsO2eeoiySbAC_ACtFZ : $@convention(method) (HasSynthesizedEquals, HasSynthesizedEquals, @thin HasSynthesizedEquals.Type) -> Bool
// CHECK: return

// CHECK-LABEL: sil shared [transparent] [serialized] [thunk] [ossa] @$s16DerivedWitnesses17UsesDefaultEqualsOSQAASQ2eeoiySbx_xtFZTW : $@convention(witness_method: Equatable) (@in_guaranteed UsesDefaultEquals, @in_guaranteed UsesDefaultEquals, @thick UsesDefaultEquals.Type) -> Bool {
// CHECK: bb0(%0 : $*UsesDefaultEquals, %1 : $*UsesDefaultEquals, %2 : $@thick UsesDefaultEquals.Type):
// CHECK: function_ref @$ss2eeoiySbx_xtSYRzSQ8RawValueRpzlF : $@convention(thin) <τ_0_0 where τ_0_0 : RawRepresentable, τ_0_0.RawValue : Equatable> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_0) -> Bool
// CHECK: return