Skip to content

Commit c3609bc

Browse files
authored
Merge pull request swiftlang#32190 from xedin/rdar-63510989-5.3
[5.3][CSGen] Allow `is` patterns to infer type from enclosing context
2 parents edf8c20 + a73611e commit c3609bc

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/Sema/CSGen.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2535,7 +2535,19 @@ namespace {
25352535
ConstraintKind::CheckedCast, subPatternType, castType,
25362536
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
25372537

2538-
return setType(subPatternType);
2538+
// Allow `is` pattern to infer type from context which is then going
2539+
// to be propaged down to its sub-pattern via conversion. This enables
2540+
// correct handling of patterns like `_ as Foo` where `_` would
2541+
// get a type of `Foo` but `is` pattern enclosing it could still be
2542+
// inferred from enclosing context.
2543+
auto isType = CS.createTypeVariable(
2544+
CS.getConstraintLocator(
2545+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern))),
2546+
TVO_CanBindToNoEscape);
2547+
CS.addConstraint(
2548+
ConstraintKind::Conversion, subPatternType, isType,
2549+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2550+
return setType(isType);
25392551
}
25402552

25412553
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)