Skip to content

Commit 509ee4c

Browse files
committed
Don't use construction to convert literals in rawValue fix-it. (#4934)
Input: panel.styleMask = 8345 Old output: panel.styleMask = NSWindowStyleMask(rawValue: UInt(8345)) New output: panel.styleMask = NSWindowStyleMask(rawValue: 8345) rdar://problem/26681232 (cherry picked from commit b9e1d4c)
1 parent 4dea103 commit 509ee4c

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
@@ -3571,7 +3571,7 @@ bool FailureDiagnosis::diagnoseCalleeResultContextualConversionError() {
35713571
/// Return true if the given type conforms to a known protocol type.
35723572
static bool conformsToKnownProtocol(Type fromType,
35733573
KnownProtocolKind kind,
3574-
ConstraintSystem *CS) {
3574+
const ConstraintSystem *CS) {
35753575
auto proto = CS->TC.getProtocol(SourceLoc(), kind);
35763576
if (!proto)
35773577
return false;
@@ -3584,15 +3584,15 @@ static bool conformsToKnownProtocol(Type fromType,
35843584
return false;
35853585
}
35863586

3587-
static bool isIntegerType(Type fromType, ConstraintSystem *CS) {
3587+
static bool isIntegerType(Type fromType, const ConstraintSystem *CS) {
35883588
return conformsToKnownProtocol(fromType,
35893589
KnownProtocolKind::ExpressibleByIntegerLiteral,
35903590
CS);
35913591
}
35923592

35933593
/// Return true if the given type conforms to RawRepresentable.
35943594
static Type isRawRepresentable(Type fromType,
3595-
ConstraintSystem *CS) {
3595+
const ConstraintSystem *CS) {
35963596
auto rawReprType =
35973597
CS->TC.getProtocol(SourceLoc(), KnownProtocolKind::RawRepresentable);
35983598
if (!rawReprType)
@@ -3615,7 +3615,7 @@ static Type isRawRepresentable(Type fromType,
36153615
/// underlying type conforming to the given known protocol.
36163616
static Type isRawRepresentable(Type fromType,
36173617
KnownProtocolKind kind,
3618-
ConstraintSystem *CS) {
3618+
const ConstraintSystem *CS) {
36193619
Type rawTy = isRawRepresentable(fromType, CS);
36203620
if (!rawTy || !conformsToKnownProtocol(rawTy, kind, CS))
36213621
return Type();
@@ -3646,11 +3646,11 @@ static bool isIntegerToStringIndexConversion(Type fromType, Type toType,
36463646
///
36473647
/// This helps migration with SDK changes.
36483648
static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
3649-
ConstraintSystem *CS,
3649+
const ConstraintSystem *CS,
36503650
Type fromType,
36513651
Type toType,
36523652
KnownProtocolKind kind,
3653-
Expr *expr) {
3653+
const Expr *expr) {
36543654
// The following fixes apply for optional destination types as well.
36553655
bool toTypeIsOptional = !toType->getAnyOptionalObjectType().isNull();
36563656
toType = toType->lookThroughAllAnyOptionalTypes();
@@ -3690,7 +3690,8 @@ static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
36903690
std::string convWrapBefore = toType.getString();
36913691
convWrapBefore += "(rawValue: ";
36923692
std::string convWrapAfter = ")";
3693-
if (!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
3693+
if (!isa<LiteralExpr>(expr) &&
3694+
!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
36943695
// Only try to insert a converting construction if the protocol is a
36953696
// literal protocol and not some other known protocol.
36963697
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)