Skip to content

Commit 29bf82e

Browse files
[CSSimplify] Fix sub-pattern matching logic related closure and enum element (#62777)
* [CSSimplify] fix pattern matching logic which is as part of the enum element for closure * add test * Fixed logic to apply to N levels of nesting * update tests that take into account N levels of nesting. * fix indentation * use dynamic variables and uppdate some logic * Revert "use dynamic variables and uppdate some logic" This reverts commit 279dc4f. * fix logic to only see pattern matches * clean up unnecessary logic Co-authored-by: Luciano Almeida <[email protected]> * Clean up unnecessary logic(2) * remove dropping * Fix documentation comments to match current context * clean up unnecessary logic * remove comments Co-authored-by: Luciano Almeida <[email protected]>
1 parent 24bd6fc commit 29bf82e

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,25 +2055,10 @@ static bool isInPatternMatchingContext(ConstraintLocatorBuilder locator) {
20552055
SmallVector<LocatorPathElt, 4> path;
20562056
(void)locator.getLocatorParts(path);
20572057

2058-
while (!path.empty() && path.back().is<LocatorPathElt::TupleType>())
2059-
path.pop_back();
2060-
2061-
if (!path.empty()) {
2062-
// Direct pattern matching between tuple pattern and tuple type.
2063-
if (path.back().is<LocatorPathElt::PatternMatch>()) {
2064-
return true;
2065-
} else if (path.size() > 1) {
2066-
// sub-pattern matching as part of the enum element matching
2067-
// where sub-element is a tuple pattern e.g.
2068-
// `case .foo((a: 42, _)) = question`
2069-
auto lastIdx = path.size() - 1;
2070-
if (path[lastIdx - 1].is<LocatorPathElt::PatternMatch>() &&
2071-
path[lastIdx].is<LocatorPathElt::FunctionArgument>())
2072-
return true;
2073-
}
2074-
}
2075-
2076-
return false;
2058+
auto pathElement = llvm::find_if(path, [](LocatorPathElt &elt) {
2059+
return elt.is<LocatorPathElt::PatternMatch>();
2060+
});
2061+
return pathElement != path.end();
20772062
}
20782063

20792064
namespace {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-typecheck-verify-swift
2+
enum TestType {
3+
case foo
4+
case bar(Bool, (a: String, (b: String, (String, (c: String, Bool), String), String)))
5+
}
6+
7+
func test(type: TestType) -> String {
8+
let str: String = {
9+
switch type {
10+
case .foo:
11+
return ""
12+
case .bar(_, (_, (_, (_, (let c, _), _), _))):
13+
return c
14+
}
15+
}()
16+
17+
return str
18+
}

0 commit comments

Comments
 (0)