Skip to content

Commit 0695917

Browse files
committed
[CS] Don't form conversion between switch subject and pattern
This is wrong because there's nowhere to put any conversion that is introduced, meaning that we'll likely crash in SILGen. Change the constraint to equality, which matches what we do outside of the constraint system. rdar://107709341
1 parent 7a137d6 commit 0695917

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ ERROR(cannot_match_expr_tuple_pattern_with_nontuple_value,none,
197197
ERROR(cannot_match_unresolved_expr_pattern_with_value,none,
198198
"pattern cannot match values of type %0",
199199
(Type))
200+
ERROR(cannot_match_value_with_pattern,none,
201+
"pattern of type %1 cannot match %0",
202+
(Type, Type))
200203

201204
ERROR(cannot_reference_compare_types,none,
202205
"cannot check reference equality of functions; operands here have types "

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,8 @@ ContextualFailure::getDiagnosticFor(ContextualTypePurpose context,
35493549
return diag::cannot_convert_discard_value;
35503550

35513551
case CTP_CaseStmt:
3552+
return diag::cannot_match_value_with_pattern;
3553+
35523554
case CTP_ThrowStmt:
35533555
case CTP_ForEachStmt:
35543556
case CTP_ForEachSequence:

lib/Sema/CSSyntacticElement.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,11 @@ class SyntacticElementConstraintGenerator
657657

658658
// Convert the contextual type to the pattern, which establishes the
659659
// bindings.
660-
cs.addConstraint(ConstraintKind::Conversion, context.getType(), patternType,
661-
locator);
660+
auto *loc = cs.getConstraintLocator(
661+
locator, {LocatorPathElt::PatternMatch(pattern),
662+
LocatorPathElt::ContextualType(context.purpose)});
663+
cs.addConstraint(ConstraintKind::Equal, context.getType(), patternType,
664+
loc);
662665

663666
// For any pattern variable that has a parent variable (i.e., another
664667
// pattern variable with the same name in the same case), require that

test/Constraints/rdar107709341.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// rdar://107709341 – Make sure we don't crash.
4+
func foo(_ x: Int) {
5+
// FIXME: We ought to have a better diagnostic
6+
let _ = { // expected-error {{unable to infer closure type in the current context}}
7+
switch x {
8+
case Optional<Int>.some(x):
9+
break
10+
default:
11+
break
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)