Skip to content

Commit b9e1d4c

Browse files
authored
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
1 parent 3619738 commit b9e1d4c

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
@@ -3573,7 +3573,7 @@ bool FailureDiagnosis::diagnoseCalleeResultContextualConversionError() {
35733573
/// Return true if the given type conforms to a known protocol type.
35743574
static bool conformsToKnownProtocol(Type fromType,
35753575
KnownProtocolKind kind,
3576-
ConstraintSystem *CS) {
3576+
const ConstraintSystem *CS) {
35773577
auto proto = CS->TC.getProtocol(SourceLoc(), kind);
35783578
if (!proto)
35793579
return false;
@@ -3586,15 +3586,15 @@ static bool conformsToKnownProtocol(Type fromType,
35863586
return false;
35873587
}
35883588

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

35953595
/// Return true if the given type conforms to RawRepresentable.
35963596
static Type isRawRepresentable(Type fromType,
3597-
ConstraintSystem *CS) {
3597+
const ConstraintSystem *CS) {
35983598
auto rawReprType =
35993599
CS->TC.getProtocol(SourceLoc(), KnownProtocolKind::RawRepresentable);
36003600
if (!rawReprType)
@@ -3617,7 +3617,7 @@ static Type isRawRepresentable(Type fromType,
36173617
/// underlying type conforming to the given known protocol.
36183618
static Type isRawRepresentable(Type fromType,
36193619
KnownProtocolKind kind,
3620-
ConstraintSystem *CS) {
3620+
const ConstraintSystem *CS) {
36213621
Type rawTy = isRawRepresentable(fromType, CS);
36223622
if (!rawTy || !conformsToKnownProtocol(rawTy, kind, CS))
36233623
return Type();
@@ -3648,11 +3648,11 @@ static bool isIntegerToStringIndexConversion(Type fromType, Type toType,
36483648
///
36493649
/// This helps migration with SDK changes.
36503650
static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
3651-
ConstraintSystem *CS,
3651+
const ConstraintSystem *CS,
36523652
Type fromType,
36533653
Type toType,
36543654
KnownProtocolKind kind,
3655-
Expr *expr) {
3655+
const Expr *expr) {
36563656
// The following fixes apply for optional destination types as well.
36573657
bool toTypeIsOptional = !toType->getAnyOptionalObjectType().isNull();
36583658
toType = toType->lookThroughAllAnyOptionalTypes();
@@ -3692,7 +3692,8 @@ static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
36923692
std::string convWrapBefore = toType.getString();
36933693
convWrapBefore += "(rawValue: ";
36943694
std::string convWrapAfter = ")";
3695-
if (!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
3695+
if (!isa<LiteralExpr>(expr) &&
3696+
!CS->TC.isConvertibleTo(fromType, rawTy, CS->DC)) {
36963697
// Only try to insert a converting construction if the protocol is a
36973698
// literal protocol and not some other known protocol.
36983699
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)