Skip to content

Sema: Narrow down the derivation of == for RawRepresentable enums to non-resilient modules #40382

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
37 changes: 24 additions & 13 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4123,6 +4123,11 @@ void ConformanceChecker::checkNonFinalClassWitness(ValueDecl *requirement,
}
}

static bool isSwiftRawRepresentableEnum(Type adoptee) {
auto *enumDecl = dyn_cast<EnumDecl>(adoptee->getAnyNominal());
return (enumDecl && enumDecl->hasRawType() && !enumDecl->isObjC());
}

// If the given witness matches a generic RawRepresentable function conforming
// with a given protocol e.g. `func == <T : RawRepresentable>(lhs: T, rhs: T) ->
// Bool where T.RawValue : Equatable`
Expand Down Expand Up @@ -4201,13 +4206,8 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
!requirement->getAttrs().isUnavailable(getASTContext());

auto &ctx = getASTContext();
bool isEquatableConformance = Conformance->getProtocol() ==
ctx.getProtocol(KnownProtocolKind::Equatable);

auto decl = Conformance->getDeclContext()->getSelfNominalTypeDecl();
auto *enumDecl = dyn_cast_or_null<EnumDecl>(decl);
bool isSwiftRawRepresentableEnum =
enumDecl && enumDecl->hasRawType() && !enumDecl->isObjC();
bool isEquatableConformance = (Conformance->getProtocol() ==
ctx.getProtocol(KnownProtocolKind::Equatable));

if (findBestWitness(requirement,
considerRenames ? &ignoringNames : nullptr,
Expand All @@ -4217,12 +4217,23 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
const auto &best = matches[bestIdx];
auto witness = best.Witness;

if (canDerive && isSwiftRawRepresentableEnum && isEquatableConformance) {
// For swift enum types that can derive equatable conformance,
// if the best witness is default generic conditional conforming
// `func == <T : RawRepresentable>(lhs: T, rhs: T) -> Bool where
// T.RawValue : Equatable` let's return as missing and derive
// the conformance since it is possible.
if (canDerive &&
isEquatableConformance &&
isSwiftRawRepresentableEnum(Adoptee) &&
!Conformance->getDeclContext()->getParentModule()->isResilient()) {
// For swift enum types that can derive an Equatable conformance,
// if the best witness is the default implementation
//
// func == <T : RawRepresentable>(lhs: T, rhs: T) -> Bool
// where T.RawValue : Equatable
//
// let's return as missing and derive the conformance, since it will be
// more efficient than comparing rawValues.
//
// However, we only do this if the module is non-resilient. If it is
// resilient, this change can break ABI by publishing a synthesized ==
// declaration that may not exist in versions of the framework built
// with an older compiler.
if (isRawRepresentableGenericFunction(ctx, witness, Conformance)) {
return ResolveWitnessResult::Missing;
}
Expand Down
63 changes: 36 additions & 27 deletions test/SILGen/enum_equatable_witness.swift
Original file line number Diff line number Diff line change
@@ -1,70 +1,79 @@
// RUN: %target-swift-emit-silgen -module-name main %s -verify | %FileCheck %s
// RUN: %target-swift-emit-silgen -module-name main %s -verify | %FileCheck %s --check-prefix=FRAGILE
// RUN: %target-swift-emit-silgen -module-name main %s -verify -enable-library-evolution | %FileCheck %s --check-prefix=RESILIENT

// SR-9425
enum MyState : String {
public enum MyState : String {
case closed = "closed"
case opened = "opened"
}

@inline(never)
func check_state(_ state : MyState) -> Int {
// CHECK: function_ref @$s4main7MyStateO21__derived_enum_equalsySbAC_ACtFZ
// CHECK-LABEL: sil [ossa] @$s4main11check_stateySiAA7MyStateOF : $@convention(thin) (MyState) -> Int {
public func check_state(_ state : MyState) -> Int {
// FRAGILE: function_ref @$s4main7MyStateO21__derived_enum_equalsySbAC_ACtFZ
// RESILIENT: function_ref @$ss2eeoiySbx_xtSYRzSQ8RawValueRpzlF
return state == .opened ? 1 : 0
}

// generic-enum.swift
enum GenericMyState<T> : String {
public enum GenericMyState<T> : String {
case closed
case opened
}

@inline(never)
func check_generic_state(_ state : GenericMyState<Int>) -> Int {
// CHECK: function_ref @$s4main14GenericMyStateO21__derived_enum_equalsySbACyxG_AEtFZ
// CHECK-LABEL: sil [ossa] @$s4main19check_generic_stateySiAA14GenericMyStateOySiGF : $@convention(thin) (GenericMyState<Int>) -> Int {
public func check_generic_state(_ state : GenericMyState<Int>) -> Int {
// FRAGILE: function_ref @$s4main14GenericMyStateO21__derived_enum_equalsySbACyxG_AEtFZ
// RESILIENT: function_ref @$ss2eeoiySbx_xtSYRzSQ8RawValueRpzlF
return state == .opened ? 1 : 0
}

// regular-enum.swift
enum Regular {
public enum Regular {
case closed
case opened
}

@inline(never)
func check_regular(_ state : Regular) -> Int {
// CHECK: function_ref @$s4main7RegularO21__derived_enum_equalsySbAC_ACtFZ
// CHECK-LABEL: sil [ossa] @$s4main13check_regularySiAA7RegularOF : $@convention(thin) (Regular) -> Int {
public func check_regular(_ state : Regular) -> Int {
// FRAGILE: function_ref @$s4main7RegularO21__derived_enum_equalsySbAC_ACtFZ
// RESILIENT: function_ref @$ss2eeoiySbx_xtSYRzSQ8RawValueRpzlF
return state == .closed ? 1 : 0
}

// string-enum.swift
enum Alphabet : String {
public enum Alphabet : String {
case A = "A", B = "B", C = "C", D = "D", E = "E", F = "F", G = "G", H = "H", I = "I", J = "J"
}

@inline(never)
func check_alphabet(_ state : Alphabet) -> Int {
// CHECK: function_ref @$s4main8AlphabetO21__derived_enum_equalsySbAC_ACtFZ
// CHECK-LABEL: sil [ossa] @$s4main14check_alphabetySiAA8AlphabetOF : $@convention(thin) (Alphabet) -> Int {
public func check_alphabet(_ state : Alphabet) -> Int {
// FRAGILE: function_ref @$s4main8AlphabetO21__derived_enum_equalsySbAC_ACtFZ
// RESILIENT: function_ref @$ss2eeoiySbx_xtSYRzSQ8RawValueRpzlF
return state == .E ? 1 : 0
}

@inline(never)
func compareIt(_ state : Alphabet, _ rhs: Alphabet) -> Bool {
// CHECK: function_ref @$s4main8AlphabetO21__derived_enum_equalsySbAC_ACtFZ
// CHECK-LABEL: sil [ossa] @$s4main9compareItySbAA8AlphabetO_ADtF : $@convention(thin) (Alphabet, Alphabet) -> Bool {
public func compareIt(_ state : Alphabet, _ rhs: Alphabet) -> Bool {
// FRAGILE: function_ref @$s4main8AlphabetO21__derived_enum_equalsySbAC_ACtFZ
// RESILIENT: function_ref @$ss2eeoiySbx_xtSYRzSQ8RawValueRpzlF
return state == rhs
}

// int-enum.swift
enum AlphabetInt : Int {
public enum AlphabetInt : Int {
case A = 10, B = 100, C = 12, D = 456, E = 1, F = 3, G = 77, H = 2, I = 27, J = 42
}

@inline(never)
func check_alphabet_int(_ state : AlphabetInt) -> Int {
// CHECK: function_ref @$s4main11AlphabetIntO21__derived_enum_equalsySbAC_ACtFZ
// CHECK-LABEL: sil [ossa] @$s4main18check_alphabet_intySiAA11AlphabetIntOF : $@convention(thin) (AlphabetInt) -> Int {
public func check_alphabet_int(_ state : AlphabetInt) -> Int {
// FRAGILE: function_ref @$s4main11AlphabetIntO21__derived_enum_equalsySbAC_ACtFZ
// RESILIENT: function_ref @$ss2eeoiySbx_xtSYRzSQ8RawValueRpzlF
return state == .E ? 1 : 0
}

@inline(never)
func compareIt(_ state : AlphabetInt, _ rhs: AlphabetInt) -> Bool {
// CHECK: function_ref @$s4main11AlphabetIntO21__derived_enum_equalsySbAC_ACtFZ
// CHECK-LABEL: sil [ossa] @$s4main9compareItySbAA11AlphabetIntO_ADtF : $@convention(thin) (AlphabetInt, AlphabetInt) -> Bool {
public func compareIt(_ state : AlphabetInt, _ rhs: AlphabetInt) -> Bool {
// FRAGILE: function_ref @$s4main11AlphabetIntO21__derived_enum_equalsySbAC_ACtFZ
// RESILIENT: function_ref @$ss2eeoiySbx_xtSYRzSQ8RawValueRpzlF
return state == rhs
}