Skip to content

Commit 822e3dd

Browse files
authored
Merge pull request #4943 from jrose-apple/swift-3-rawValue-literals
Don't use construction to convert literals in rawValue fix-it.
2 parents 239c5d3 + 509ee4c commit 822e3dd

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,7 +3560,7 @@ bool FailureDiagnosis::diagnoseCalleeResultContextualConversionError() {
35603560
/// Return true if the given type conforms to a known protocol type.
35613561
static bool conformsToKnownProtocol(Type fromType,
35623562
KnownProtocolKind kind,
3563-
ConstraintSystem *CS) {
3563+
const ConstraintSystem *CS) {
35643564
auto proto = CS->TC.getProtocol(SourceLoc(), kind);
35653565
if (!proto)
35663566
return false;
@@ -3573,15 +3573,15 @@ static bool conformsToKnownProtocol(Type fromType,
35733573
return false;
35743574
}
35753575

3576-
static bool isIntegerType(Type fromType, ConstraintSystem *CS) {
3576+
static bool isIntegerType(Type fromType, const ConstraintSystem *CS) {
35773577
return conformsToKnownProtocol(fromType,
35783578
KnownProtocolKind::ExpressibleByIntegerLiteral,
35793579
CS);
35803580
}
35813581

35823582
/// Return true if the given type conforms to RawRepresentable.
35833583
static Type isRawRepresentable(Type fromType,
3584-
ConstraintSystem *CS) {
3584+
const ConstraintSystem *CS) {
35853585
auto rawReprType =
35863586
CS->TC.getProtocol(SourceLoc(), KnownProtocolKind::RawRepresentable);
35873587
if (!rawReprType)
@@ -3604,7 +3604,7 @@ static Type isRawRepresentable(Type fromType,
36043604
/// underlying type conforming to the given known protocol.
36053605
static Type isRawRepresentable(Type fromType,
36063606
KnownProtocolKind kind,
3607-
ConstraintSystem *CS) {
3607+
const ConstraintSystem *CS) {
36083608
Type rawTy = isRawRepresentable(fromType, CS);
36093609
if (!rawTy || !conformsToKnownProtocol(rawTy, kind, CS))
36103610
return Type();
@@ -3635,11 +3635,11 @@ static bool isIntegerToStringIndexConversion(Type fromType, Type toType,
36353635
///
36363636
/// This helps migration with SDK changes.
36373637
static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
3638-
ConstraintSystem *CS,
3638+
const ConstraintSystem *CS,
36393639
Type fromType,
36403640
Type toType,
36413641
KnownProtocolKind kind,
3642-
Expr *expr) {
3642+
const Expr *expr) {
36433643
// The following fixes apply for optional destination types as well.
36443644
bool toTypeIsOptional = !toType->getAnyOptionalObjectType().isNull();
36453645
toType = toType->lookThroughAllAnyOptionalTypes();
@@ -3679,7 +3679,8 @@ static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
36793679
std::string convWrapBefore = toType.getString();
36803680
convWrapBefore += "(rawValue: ";
36813681
std::string convWrapAfter = ")";
3682-
if (!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
3682+
if (!isa<LiteralExpr>(expr) &&
3683+
!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
36833684
// Only try to insert a converting construction if the protocol is a
36843685
// literal protocol and not some other known protocol.
36853686
switch (kind) {

test/FixCode/fixits-apply.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func testMask12(a: MyEventMask2?) {
7777
func testMask13(a: MyEventMask2?) {
7878
testMask1(a: a) // no fix, nullability mismatch.
7979
}
80+
func testMask14() {
81+
sendIt(1)
82+
sendItOpt(2)
83+
}
8084

8185
struct Wrapper {
8286
typealias InnerMask = MyEventMask2

test/FixCode/fixits-apply.swift.result

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func testMask12(a: MyEventMask2?) {
7777
func testMask13(a: MyEventMask2?) {
7878
testMask1(a: a) // no fix, nullability mismatch.
7979
}
80+
func testMask14() {
81+
sendIt(MyEventMask2(rawValue: 1))
82+
sendItOpt(MyEventMask2(rawValue: 2))
83+
}
8084

8185
struct Wrapper {
8286
typealias InnerMask = MyEventMask2

0 commit comments

Comments
 (0)