Skip to content

[CSGen] Allow is patterns to infer type from enclosing context #32159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2020
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}}
}
}