Skip to content

Sema: Fix crash when synthesizing RawRepresentable conformance with non-Equatable raw type #29173

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
29 changes: 12 additions & 17 deletions lib/Sema/DerivedConformanceRawRepresentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,17 @@ deriveRawRepresentable_init(DerivedConformance &derived) {
return initDecl;
}

static bool canSynthesizeRawRepresentable(DerivedConformance &derived) {
auto enumDecl = cast<EnumDecl>(derived.Nominal);
bool DerivedConformance::canDeriveRawRepresentable(DeclContext *DC,
NominalTypeDecl *type) {
auto enumDecl = dyn_cast<EnumDecl>(type);
if (!enumDecl)
return false;

Type rawType = enumDecl->getRawType();
if (!rawType)
return false;
auto parentDC = cast<DeclContext>(derived.ConformanceDecl);
rawType = parentDC->mapTypeIntoContext(rawType);

rawType = DC->mapTypeIntoContext(rawType);

auto inherited = enumDecl->getInherited();
if (!inherited.empty() && inherited.front().wasValidated() &&
Expand All @@ -460,7 +463,7 @@ static bool canSynthesizeRawRepresentable(DerivedConformance &derived) {
if (!equatableProto)
return false;

if (TypeChecker::conformsToProtocol(rawType, equatableProto, enumDecl, None)
if (TypeChecker::conformsToProtocol(rawType, equatableProto, DC, None)
.isInvalid())
return false;

Expand Down Expand Up @@ -488,12 +491,8 @@ static bool canSynthesizeRawRepresentable(DerivedConformance &derived) {

ValueDecl *DerivedConformance::deriveRawRepresentable(ValueDecl *requirement) {

// We can only synthesize RawRepresentable for enums.
if (!isa<EnumDecl>(Nominal))
return nullptr;

// Check other preconditions for synthesized conformance.
if (!canSynthesizeRawRepresentable(*this))
// Check preconditions for synthesized conformance.
if (!canDeriveRawRepresentable(cast<DeclContext>(ConformanceDecl), Nominal))
return nullptr;

if (requirement->getBaseName() == Context.Id_rawValue)
Expand All @@ -509,12 +508,8 @@ ValueDecl *DerivedConformance::deriveRawRepresentable(ValueDecl *requirement) {

Type DerivedConformance::deriveRawRepresentable(AssociatedTypeDecl *assocType) {

// We can only synthesize RawRepresentable for enums.
if (!isa<EnumDecl>(Nominal))
return nullptr;

// Check other preconditions for synthesized conformance.
if (!canSynthesizeRawRepresentable(*this))
// Check preconditions for synthesized conformance.
if (!canDeriveRawRepresentable(cast<DeclContext>(ConformanceDecl), Nominal))
return nullptr;

if (assocType->getName() == Context.Id_RawValue) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/DerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ bool DerivedConformance::derivesProtocolConformance(DeclContext *DC,
// The presence of a raw type is an explicit declaration that
// the compiler should derive a RawRepresentable conformance.
case KnownProtocolKind::RawRepresentable:
return enumDecl->hasRawType();
return canDeriveRawRepresentable(DC, Nominal);

// Enums without associated values can implicitly derive Equatable
// conformance.
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/DerivedConformances.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class DerivedConformance {
/// \returns the derived member, which will also be added to the type.
Type deriveCaseIterable(AssociatedTypeDecl *assocType);

/// Determine if a RawRepresentable requirement can be derived for a type.
///
/// This is implemented for non-empty enums without associated values,
/// that declare a raw type in the inheritance clause.
static bool canDeriveRawRepresentable(DeclContext *DC, NominalTypeDecl *type);

/// Derive a RawRepresentable requirement for an enum, if it has a valid
/// raw type and raw values for all of its cases.
///
Expand Down
2 changes: 0 additions & 2 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4050,8 +4050,6 @@ static void diagnoseConformanceFailure(Type T,
// conformance to RawRepresentable was inferred.
if (auto enumDecl = T->getEnumOrBoundGenericEnum()) {
if (Proto->isSpecificProtocol(KnownProtocolKind::RawRepresentable) &&
DerivedConformance::derivesProtocolConformance(DC, enumDecl,
Proto) &&
enumDecl->hasRawType() &&
!enumDecl->getRawType()->is<ErrorType>()) {

Expand Down
42 changes: 41 additions & 1 deletion test/Sema/enum_raw_representable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class Outer {
// scenario too.
let a: Int = E.a // expected-error {{cannot convert value of type 'Outer.E' to specified type 'Int'}}

enum E : Array<Int> { // expected-error {{raw type 'Array<Int>' is not expressible by a string, integer, or floating-point literal}}
enum E : Array<Int> {
// expected-error@-1 {{raw type 'Array<Int>' is not expressible by a string, integer, or floating-point literal}}
// expected-error@-2 {{'Outer.E' declares raw type 'Array<Int>', but does not conform to RawRepresentable and conformance could not be synthesized}}
case a
}
}
Expand Down Expand Up @@ -189,3 +191,41 @@ enum ArrayOfNewEquatable : Array<NotEquatable> { }
// expected-error@-2{{'ArrayOfNewEquatable' declares raw type 'Array<NotEquatable>', but does not conform to RawRepresentable and conformance could not be synthesized}}
// expected-error@-3{{RawRepresentable conformance cannot be synthesized because raw type 'Array<NotEquatable>' is not Equatable}}
// expected-error@-4{{an enum with no cases cannot declare a raw type}}

// rdar://58127114
struct NotEquatableInteger : ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int

init(integerLiteral: Int) {}
}

enum NotEquatableRawType1 : NotEquatableInteger {
// expected-error@-1 {{'NotEquatableRawType1' declares raw type 'NotEquatableInteger', but does not conform to RawRepresentable and conformance could not be synthesized}}
// expected-error@-2 {{RawRepresentable conformance cannot be synthesized because raw type 'NotEquatableInteger' is not Equatable}}
case a = 123
}


enum NotEquatableRawType2 : NotEquatableInteger {
// expected-error@-1 {{'NotEquatableRawType2' declares raw type 'NotEquatableInteger', but does not conform to RawRepresentable and conformance could not be synthesized}}
// expected-error@-2 {{RawRepresentable conformance cannot be synthesized because raw type 'NotEquatableInteger' is not Equatable}}
typealias RawValue = NotEquatableInteger

case a = 123
}

struct NotEquatableString : ExpressibleByStringLiteral {
init(stringLiteral: String) {}
}

// FIXME: This could be diagnosed a bit better. The notes are disembodied
enum NotEquatableRawType3: NotEquatableString {
// expected-error@-1 {{RawRepresentable conformance cannot be synthesized because raw type 'NotEquatableString' is not Equatable}}
// expected-error@-2 {{'NotEquatableRawType3' declares raw type 'NotEquatableString', but does not conform to RawRepresentable and conformance could not be synthesized}}
case a
typealias RawValue = NotEquatableString
init?(rawValue: Int) { self = .a }
// expected-note@-1 {{candidate has non-matching type '(rawValue: Int)'}}
var rawValue: Int { 0 }
// expected-note@-1 {{candidate has non-matching type 'Int'}}
}