Skip to content

Commit a0acfd6

Browse files
authored
Merge pull request #10696 from xedin/rdar-32241441
2 parents 9a3d68e + 5f57183 commit a0acfd6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,8 +1402,17 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
14021402
goto recur;
14031403
}
14041404

1405-
diagnose(EEP->getLoc(), diag::enum_element_pattern_member_not_found,
1406-
EEP->getName().str(), type);
1405+
auto diag = diagnose(EEP->getLoc(),
1406+
diag::enum_element_pattern_member_not_found,
1407+
EEP->getName().str(), type);
1408+
1409+
// If we have an optional type let's try to see if the case
1410+
// exists in its base type, if so we can suggest a fix-it for that.
1411+
if (auto baseType = type->getOptionalObjectType()) {
1412+
if (lookupEnumMemberElement(*this, dc, baseType, EEP->getName(),
1413+
EEP->getLoc()))
1414+
diag.fixItInsertAfter(EEP->getEndLoc(), "?");
1415+
}
14071416
}
14081417
return true;
14091418
}

test/Constraints/patterns.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,25 @@ switch staticMembers {
314314
}
315315

316316
_ = 0
317+
318+
// rdar://problem/32241441 - Add fix-it for cases in switch with optional chaining
319+
320+
struct S_32241441 {
321+
enum E_32241441 {
322+
case foo
323+
case bar
324+
}
325+
326+
var type: E_32241441 = E_32241441.foo
327+
}
328+
329+
func rdar32241441() {
330+
let s: S_32241441? = S_32241441()
331+
332+
switch s?.type {
333+
case .foo: // expected-error {{enum case 'foo' not found in type 'S_32241441.E_32241441?'}} {{12-12=?}}
334+
break;
335+
case .bar: // expected-error {{enum case 'bar' not found in type 'S_32241441.E_32241441?'}} {{12-12=?}}
336+
break;
337+
}
338+
}

0 commit comments

Comments
 (0)