Skip to content

Commit f60567e

Browse files
committed
[sema] Follow-up on the raw-representable casting fixits, produce the fixit for optional destination types as well.
1 parent 9db508a commit f60567e

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct MyEventMask2 : OptionSet {
3636
var rawValue: UInt64 { return 0 }
3737
}
3838
func sendIt(_: MyEventMask2) {}
39+
func sendItOpt(_: MyEventMask2?) {}
3940
func testMask1(a: Int) {
4041
sendIt(a)
4142
}
@@ -48,6 +49,9 @@ func testMask3(a: MyEventMask2) {
4849
func testMask4(a: MyEventMask2) {
4950
testMask2(a: a)
5051
}
52+
func testMask5(a: Int) {
53+
sendItOpt(a)
54+
}
5155

5256
enum MyEnumType : UInt32 {
5357
case invalid

test/FixCode/fixits-apply.swift.result

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct MyEventMask2 : OptionSet {
3636
var rawValue: UInt64 { return 0 }
3737
}
3838
func sendIt(_: MyEventMask2) {}
39+
func sendItOpt(_: MyEventMask2?) {}
3940
func testMask1(a: Int) {
4041
sendIt(MyEventMask2(rawValue: UInt64(a)))
4142
}
@@ -48,6 +49,9 @@ func testMask3(a: MyEventMask2) {
4849
func testMask4(a: MyEventMask2) {
4950
testMask2(a: a.rawValue)
5051
}
52+
func testMask5(a: Int) {
53+
sendItOpt(MyEventMask2(rawValue: UInt64(a)))
54+
}
5155

5256
enum MyEnumType : UInt32 {
5357
case invalid

0 commit comments

Comments
 (0)