Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,17 @@ namespace {
ConstraintKind::CheckedCast, subPatternType, castType,
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));

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

case PatternKind::Bool:
Expand Down
17 changes: 17 additions & 0 deletions test/Constraints/patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,20 @@ func rdar_60048356() {
}
}
}

// rdar://problem/63510989 - valid pattern doesn't type-check
func rdar63510989() {
enum Value : P {
func p() {}
}

enum E {
case foo(P?)
}

func test(e: E) {
if case .foo(_ as Value) = e {} // Ok
if case .foo(let v as Value) = e {} // Ok
// expected-warning@-1 {{immutable value 'v' was never used; consider replacing with '_' or removing it}}
}
}