Skip to content

Use the existing '.rawValue' fix-it to handle unwrapping objects too. #4910

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
54 changes: 37 additions & 17 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3582,15 +3582,14 @@ bool FailureDiagnosis::diagnoseCalleeResultContextualConversionError() {


/// Return true if the given type conforms to a known protocol type.
static bool isExpressibleByLiteralType(Type fromType,
KnownProtocolKind kind,
ConstraintSystem *CS) {
auto integerType =
CS->TC.getProtocol(SourceLoc(), kind);
if (!integerType)
static bool conformsToKnownProtocol(Type fromType,
KnownProtocolKind kind,
ConstraintSystem *CS) {
auto proto = CS->TC.getProtocol(SourceLoc(), kind);
if (!proto)
return false;

if (CS->TC.conformsToProtocol(fromType, integerType, CS->DC,
if (CS->TC.conformsToProtocol(fromType, proto, CS->DC,
ConformanceCheckFlags::InExpression)) {
return true;
}
Expand All @@ -3599,9 +3598,9 @@ static bool isExpressibleByLiteralType(Type fromType,
}

static bool isIntegerType(Type fromType, ConstraintSystem *CS) {
return isExpressibleByLiteralType(fromType,
KnownProtocolKind::ExpressibleByIntegerLiteral,
CS);
return conformsToKnownProtocol(fromType,
KnownProtocolKind::ExpressibleByIntegerLiteral,
CS);
}

/// Return true if the given type conforms to RawRepresentable.
Expand Down Expand Up @@ -3631,7 +3630,7 @@ static Type isRawRepresentable(Type fromType,
KnownProtocolKind kind,
ConstraintSystem *CS) {
Type rawTy = isRawRepresentable(fromType, CS);
if (!rawTy || !isExpressibleByLiteralType(rawTy, kind, CS))
if (!rawTy || !conformsToKnownProtocol(rawTy, kind, CS))
return Type();

return rawTy;
Expand All @@ -3642,7 +3641,7 @@ static Type isRawRepresentable(Type fromType,
static bool isIntegerToStringIndexConversion(Type fromType, Type toType,
ConstraintSystem *CS) {
auto kind = KnownProtocolKind::ExpressibleByIntegerLiteral;
return (isExpressibleByLiteralType(fromType, kind, CS) &&
return (conformsToKnownProtocol(fromType, kind, CS) &&
toType->getCanonicalType().getString() == "String.CharacterView.Index");
}

Expand Down Expand Up @@ -3697,14 +3696,23 @@ static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
}
};

if (isExpressibleByLiteralType(fromType, kind, CS)) {
if (conformsToKnownProtocol(fromType, kind, CS)) {
if (auto rawTy = isRawRepresentable(toType, kind, CS)) {
// Produce before/after strings like 'Result(rawValue: RawType(<expr>))'
// or just 'Result(rawValue: <expr>)'.
std::string convWrapBefore = toType.getString();
convWrapBefore += "(rawValue: ";
std::string convWrapAfter = ")";
if (rawTy->getCanonicalType() != fromType->getCanonicalType()) {
if (!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
// Only try to insert a converting construction if the protocol is a
// literal protocol and not some other known protocol.
switch (kind) {
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, _) \
case KnownProtocolKind::name: break;
#define PROTOCOL_WITH_NAME(name, _) \
case KnownProtocolKind::name: return false;
#include "swift/AST/KnownProtocols.def"
}
convWrapBefore += rawTy->getString();
convWrapBefore += "(";
convWrapAfter += ")";
Expand All @@ -3715,11 +3723,20 @@ static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
}

if (auto rawTy = isRawRepresentable(fromType, kind, CS)) {
if (isExpressibleByLiteralType(toType, kind, CS)) {
if (conformsToKnownProtocol(toType, kind, CS)) {
std::string convWrapBefore;
std::string convWrapAfter = ".rawValue";
if (rawTy->getCanonicalType() != toType->getCanonicalType()) {
convWrapBefore += rawTy->getString();
if (!CS->TC.isConvertibleTo(rawTy, toType, CS->DC)) {
// Only try to insert a converting construction if the protocol is a
// literal protocol and not some other known protocol.
switch (kind) {
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, _) \
case KnownProtocolKind::name: break;
#define PROTOCOL_WITH_NAME(name, _) \
case KnownProtocolKind::name: return false;
#include "swift/AST/KnownProtocols.def"
}
convWrapBefore += toType->getString();
convWrapBefore += "(";
convWrapAfter += ")";
}
Expand Down Expand Up @@ -4180,6 +4197,9 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
tryRawRepresentableFixIts(diag, CS, exprType, contextualType,
KnownProtocolKind::ExpressibleByStringLiteral,
expr) ||
tryRawRepresentableFixIts(diag, CS, exprType, contextualType,
KnownProtocolKind::AnyObject,
expr) ||
tryIntegerCastFixIts(diag, CS, exprType, contextualType, expr) ||
addTypeCoerceFixit(diag, CS, exprType, contextualType, expr);
break;
Expand Down
23 changes: 23 additions & 0 deletions test/FixCode/fixits-apply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ func testConvertSomeName(s: String) {
testPassSomeName("\(s)}")
}

class WrappedClass {}
class WrappedClassSub: WrappedClass {}
struct ClassWrapper : RawRepresentable {
var rawValue: WrappedClass
}
func testPassAnyObject(_: AnyObject) {}
func testPassAnyObjectOpt(_: AnyObject?) {}
func testPassWrappedSub(_: WrappedClassSub) {}
func testConvertClassWrapper(_ x: ClassWrapper, _ sub: WrappedClassSub) {
testPassAnyObject(x)
testPassAnyObjectOpt(x)
testPassWrappedSub(x)

let iuo: ClassWrapper! = x
testPassAnyObject(iuo)
testPassAnyObjectOpt(iuo)

let _: ClassWrapper = sub
let _: ClassWrapper = x.rawValue
// FIXME: This one inserts 'as!', which is incorrect.
let _: ClassWrapper = sub as AnyObject
}

enum MyEnumType : UInt32 {
case invalid
}
Expand Down
27 changes: 25 additions & 2 deletions test/FixCode/fixits-apply.swift.result
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func testMask2(a: UInt64) {
sendIt(MyEventMask2(rawValue: a))
}
func testMask3(a: MyEventMask2) {
testMask1(a: UInt64(a.rawValue))
testMask1(a: Int(a.rawValue))
}
func testMask4(a: MyEventMask2) {
testMask2(a: a.rawValue)
Expand All @@ -69,7 +69,7 @@ func testMask10(a: Int?) {
sendIt(a) // no fix, nullability mismatch.
}
func testMask11(a: MyEventMask2?) {
testMask7(a: a.map { UInt64($0.rawValue) })
testMask7(a: a.map { Int($0.rawValue) })
}
func testMask12(a: MyEventMask2?) {
testMask8(a: a.map { $0.rawValue })
Expand All @@ -96,6 +96,29 @@ func testConvertSomeName(s: String) {
testPassSomeName(SomeName(rawValue: "\(s)}"))
}

class WrappedClass {}
class WrappedClassSub: WrappedClass {}
struct ClassWrapper : RawRepresentable {
var rawValue: WrappedClass
}
func testPassAnyObject(_: AnyObject) {}
func testPassAnyObjectOpt(_: AnyObject?) {}
func testPassWrappedSub(_: WrappedClassSub) {}
func testConvertClassWrapper(_ x: ClassWrapper, _ sub: WrappedClassSub) {
testPassAnyObject(x.rawValue)
testPassAnyObjectOpt(x.rawValue)
testPassWrappedSub(x)

let iuo: ClassWrapper! = x
testPassAnyObject(iuo)
testPassAnyObjectOpt(iuo.map { $0.rawValue })

let _: ClassWrapper = ClassWrapper(rawValue: sub)
let _: ClassWrapper = ClassWrapper(rawValue: x.rawValue)
// FIXME: This one inserts 'as!', which is incorrect.
let _: ClassWrapper = sub as AnyObject as! ClassWrapper
}

enum MyEnumType : UInt32 {
case invalid
}
Expand Down