Skip to content

Commit 3665e8a

Browse files
akyrtzitkremenek
authored andcommitted
Follow-up on the raw-representable casting fixits, produce the fixit for optional destination types as well. (#2893)
* [sema] Follow-up on the raw-representable casting fixits, produce the fixit for optional destination types as well. * [test] Add an extra test case for f60567e, per feedback by BenL.
1 parent b1aafe3 commit 3665e8a

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,6 +3158,9 @@ static void tryRawRepresentableFixIts(InFlightDiagnostic &diag,
31583158
Type toType,
31593159
KnownProtocolKind kind,
31603160
Expr *expr) {
3161+
// The following fixes apply for optional destination types as well.
3162+
toType = toType->lookThroughAllAnyOptionalTypes();
3163+
31613164
if (isLiteralConvertibleType(fromType, kind, CS)) {
31623165
if (auto rawTy = isRawRepresentable(toType, kind, CS)) {
31633166
// Produce before/after strings like 'Result(rawValue: RawType(<expr>))'
@@ -3821,9 +3824,7 @@ static bool diagnoseSingleCandidateFailures(CalleeCandidateInfo &CCI,
38213824
candidate.getDecl() && isa<ConstructorDecl>(candidate.getDecl()) &&
38223825
candidate.level == 1) {
38233826
CallArgParam &arg = args[0];
3824-
auto resTy = candidate.getResultType();
3825-
if (auto unwrappedOpt = resTy->getOptionalObjectType())
3826-
resTy = unwrappedOpt;
3827+
auto resTy = candidate.getResultType()->lookThroughAllAnyOptionalTypes();
38273828
auto rawTy = isRawRepresentable(resTy, CCI.CS);
38283829
if (rawTy && resTy->getCanonicalType() == arg.Ty.getCanonicalTypeOrNull()) {
38293830
auto getInnerExpr = [](Expr *E) -> Expr* {

test/FixCode/fixits-apply.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct MyEventMask2 : OptionSet {
3636
var rawValue: UInt64 { return 0 }
3737
}
3838
func sendIt(_: MyEventMask2) {}
39+
func sendItOpt(_: MyEventMask2?) {}
40+
func sendItOpt3(_: MyEventMask2???) {}
3941
func testMask1(a: Int) {
4042
sendIt(a)
4143
}
@@ -48,6 +50,12 @@ func testMask3(a: MyEventMask2) {
4850
func testMask4(a: MyEventMask2) {
4951
testMask2(a: a)
5052
}
53+
func testMask5(a: Int) {
54+
sendItOpt(a)
55+
}
56+
func testMask6(a: Int) {
57+
sendItOpt(a)
58+
}
5159

5260
enum MyEnumType : UInt32 {
5361
case invalid

test/FixCode/fixits-apply.swift.result

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct MyEventMask2 : OptionSet {
3636
var rawValue: UInt64 { return 0 }
3737
}
3838
func sendIt(_: MyEventMask2) {}
39+
func sendItOpt(_: MyEventMask2?) {}
40+
func sendItOpt3(_: MyEventMask2???) {}
3941
func testMask1(a: Int) {
4042
sendIt(MyEventMask2(rawValue: UInt64(a)))
4143
}
@@ -48,6 +50,12 @@ func testMask3(a: MyEventMask2) {
4850
func testMask4(a: MyEventMask2) {
4951
testMask2(a: a.rawValue)
5052
}
53+
func testMask5(a: Int) {
54+
sendItOpt(MyEventMask2(rawValue: UInt64(a)))
55+
}
56+
func testMask6(a: Int) {
57+
sendItOpt(MyEventMask2(rawValue: UInt64(a)))
58+
}
5159

5260
enum MyEnumType : UInt32 {
5361
case invalid

0 commit comments

Comments
 (0)