Skip to content

Commit 2f3a0f0

Browse files
authored
Merge pull request #32159 from xedin/rdar-63510989
[CSGen] Allow `is` patterns to infer type from enclosing context
2 parents d3e64ed + b14d965 commit 2f3a0f0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/Sema/CSGen.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2545,7 +2545,17 @@ namespace {
25452545
ConstraintKind::CheckedCast, subPatternType, castType,
25462546
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
25472547

2548-
return setType(subPatternType);
2548+
// Allow `is` pattern to infer type from context which is then going
2549+
// to be propaged down to its sub-pattern via conversion. This enables
2550+
// correct handling of patterns like `_ as Foo` where `_` would
2551+
// get a type of `Foo` but `is` pattern enclosing it could still be
2552+
// inferred from enclosing context.
2553+
auto isType = CS.createTypeVariable(CS.getConstraintLocator(pattern),
2554+
TVO_CanBindToNoEscape);
2555+
CS.addConstraint(
2556+
ConstraintKind::Conversion, subPatternType, isType,
2557+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2558+
return setType(isType);
25492559
}
25502560

25512561
case PatternKind::Bool:

test/Constraints/patterns.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,20 @@ func rdar_60048356() {
478478
}
479479
}
480480
}
481+
482+
// rdar://problem/63510989 - valid pattern doesn't type-check
483+
func rdar63510989() {
484+
enum Value : P {
485+
func p() {}
486+
}
487+
488+
enum E {
489+
case foo(P?)
490+
}
491+
492+
func test(e: E) {
493+
if case .foo(_ as Value) = e {} // Ok
494+
if case .foo(let v as Value) = e {} // Ok
495+
// expected-warning@-1 {{immutable value 'v' was never used; consider replacing with '_' or removing it}}
496+
}
497+
}

0 commit comments

Comments
 (0)