Skip to content

Commit 2e20aa5

Browse files
authored
Merge pull request #71285 from hamishknight/fix-loc
[CS] Fix locator for `if` expressions
2 parents fb5228d + 185a7d5 commit 2e20aa5

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

lib/Sema/CSSyntacticElement.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,14 +1005,11 @@ class SyntacticElementConstraintGenerator
10051005
}
10061006

10071007
void visitSwitchStmt(SwitchStmt *switchStmt) {
1008-
auto *switchLoc = cs.getConstraintLocator(
1009-
locator, LocatorPathElt::SyntacticElement(switchStmt));
1010-
10111008
SmallVector<ElementInfo, 4> elements;
10121009
{
10131010
auto *subjectExpr = switchStmt->getSubjectExpr();
10141011
{
1015-
elements.push_back(makeElement(subjectExpr, switchLoc));
1012+
elements.push_back(makeElement(subjectExpr, locator));
10161013

10171014
SyntacticElementTarget target(subjectExpr, context.getAsDeclContext(),
10181015
CTP_Unused, Type(),
@@ -1022,32 +1019,29 @@ class SyntacticElementConstraintGenerator
10221019
}
10231020

10241021
for (auto rawCase : switchStmt->getRawCases())
1025-
elements.push_back(makeElement(rawCase, switchLoc));
1022+
elements.push_back(makeElement(rawCase, locator));
10261023
}
10271024

1028-
createConjunction(elements, switchLoc);
1025+
createConjunction(elements, locator);
10291026
}
10301027

10311028
void visitDoCatchStmt(DoCatchStmt *doStmt) {
1032-
auto *doLoc = cs.getConstraintLocator(
1033-
locator, LocatorPathElt::SyntacticElement(doStmt));
1034-
10351029
SmallVector<ElementInfo, 4> elements;
10361030

10371031
// First, let's record a body of `do` statement. Note we need to add a
10381032
// SyntaticElement locator path element here to avoid treating the inner
10391033
// brace conjunction as being isolated if 'doLoc' is for an isolated
10401034
// conjunction (as is the case with 'do' expressions).
10411035
auto *doBodyLoc = cs.getConstraintLocator(
1042-
doLoc, LocatorPathElt::SyntacticElement(doStmt->getBody()));
1036+
locator, LocatorPathElt::SyntacticElement(doStmt->getBody()));
10431037
elements.push_back(makeElement(doStmt->getBody(), doBodyLoc));
10441038

10451039
// After that has been type-checked, let's switch to
10461040
// individual `catch` statements.
10471041
for (auto *catchStmt : doStmt->getCatches())
1048-
elements.push_back(makeElement(catchStmt, doLoc));
1042+
elements.push_back(makeElement(catchStmt, locator));
10491043

1050-
createConjunction(elements, doLoc);
1044+
createConjunction(elements, locator);
10511045
}
10521046

10531047
void visitCaseStmt(CaseStmt *caseStmt) {
@@ -1546,7 +1540,9 @@ bool ConstraintSystem::generateConstraints(SingleValueStmtExpr *E) {
15461540

15471541
// Generate the conjunction for the branches.
15481542
auto context = SyntacticElementContext::forSingleValueStmtExpr(E, join);
1549-
SyntacticElementConstraintGenerator generator(*this, context, loc);
1543+
auto *stmtLoc =
1544+
getConstraintLocator(loc, LocatorPathElt::SyntacticElement(S));
1545+
SyntacticElementConstraintGenerator generator(*this, context, stmtLoc);
15501546
generator.visit(S);
15511547
return generator.hadError;
15521548
}

test/Constraints/issue-71282.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// https://github.com/apple/swift/issues/71282
4+
5+
enum E {
6+
case e
7+
}
8+
9+
// Make sure we don't crash.
10+
func foo(_ x: E) {
11+
return if .random() {
12+
()
13+
switch x {
14+
case .e:
15+
()
16+
}
17+
} else { // expected-error {{non-expression branch of 'if' expression may only end with a 'throw'}}
18+
()
19+
}
20+
}

0 commit comments

Comments
 (0)