Skip to content

Commit d0ac02d

Browse files
authored
Merge pull request #39399 from xedin/result-builder-switch
[TypeChecker] Prevent pattern resolution from triggering type-checking for unresolved references
2 parents 90c8eb3 + 3316c47 commit d0ac02d

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,14 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
16611661
// Build a constraint system in which we can check the body of the function.
16621662
ConstraintSystem cs(func, options);
16631663

1664+
if (cs.isDebugMode()) {
1665+
auto &log = llvm::errs();
1666+
1667+
log << "--- Applying result builder to function ---\n";
1668+
func->dump(log);
1669+
log << '\n';
1670+
}
1671+
16641672
if (auto result = cs.matchResultBuilder(
16651673
func, builderType, resultContextType, resultConstraintKind,
16661674
cs.getConstraintLocator(func->getBody()))) {

lib/Sema/TypeCheckPattern.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ filterForEnumElement(DeclContext *DC, SourceLoc UseLoc,
8484
for (const LookupResultEntry &result : foundElements) {
8585
ValueDecl *e = result.getValueDecl();
8686
assert(e);
87-
if (e->isInvalid()) {
88-
continue;
89-
}
87+
9088
// Skip if the enum element was referenced as an instance member
9189
if (unqualifiedLookup) {
9290
if (!result.getBaseDecl() ||
@@ -96,8 +94,10 @@ filterForEnumElement(DeclContext *DC, SourceLoc UseLoc,
9694
}
9795

9896
if (auto *oe = dyn_cast<EnumElementDecl>(e)) {
99-
// Ambiguities should be ruled out by parsing.
100-
assert(!foundElement && "ambiguity in enum case name lookup?!");
97+
// Note that there could be multiple elements with the same
98+
// name, such results in a re-declaration error, so let's
99+
// just always pick the last element, just like in `foundConstant`
100+
// case.
101101
foundElement = oe;
102102
continue;
103103
}

test/Constraints/result_builder.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,26 @@ func testSwitch(_ e: E) {
582582
}
583583
}
584584

585+
func testExistingPatternsInCaseStatements() {
586+
tuplify(true) { c in
587+
switch false {
588+
case (c): 1 // Ok
589+
default: 0
590+
}
591+
}
592+
593+
var arr: [Int] = []
594+
595+
tuplify(true) { c in
596+
let n = arr.endIndex
597+
598+
switch arr.startIndex {
599+
case (n): 1 // Ok
600+
default: 0
601+
}
602+
}
603+
}
604+
585605
// CHECK: testSwitch
586606
// CHECK-SAME: first(main.Either<Swift.String, (Swift.Int, Swift.String)>.first("a"))
587607
testSwitch(getE(0))

0 commit comments

Comments
 (0)