Skip to content

Commit 1983172

Browse files
committed
[Sema] Rename bindSwitchCasePatternVars -> diagnoseCaseVarMutabilityMismatch
Now that we wire up the parents up-front, this no longer needs to set the parents. As such, remove the logic and rename to reflect the fact that it now just diagnoses mutability mismatches.
1 parent dc13b1f commit 1983172

File tree

3 files changed

+11
-42
lines changed

3 files changed

+11
-42
lines changed

lib/Sema/CSSyntacticElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,7 @@ class SyntacticElementSolutionApplication
20052005
}
20062006
}
20072007

2008-
bindSwitchCasePatternVars(context.getAsDeclContext(), caseStmt);
2008+
diagnoseCaseVarMutabilityMismatch(context.getAsDeclContext(), caseStmt);
20092009

20102010
for (auto *expected : caseStmt->getCaseBodyVariables()) {
20112011
assert(expected->hasName());

lib/Sema/TypeCheckStmt.cpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,9 +1581,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
15811581

15821582
// First pass: check all of the bindings.
15831583
for (auto *caseBlock : make_range(casesBegin, casesEnd)) {
1584-
// Bind all of the pattern variables together so we can follow the
1585-
// "parent" pointers later on.
1586-
bindSwitchCasePatternVars(DC, caseBlock);
1584+
diagnoseCaseVarMutabilityMismatch(DC, caseBlock);
15871585

15881586
auto caseLabelItemArray = caseBlock->getMutableCaseLabelItems();
15891587
for (auto &labelItem : caseLabelItemArray) {
@@ -3238,7 +3236,8 @@ void swift::checkUnknownAttrRestrictions(
32383236
}
32393237
}
32403238

3241-
void swift::bindSwitchCasePatternVars(DeclContext *dc, CaseStmt *caseStmt) {
3239+
void swift::diagnoseCaseVarMutabilityMismatch(DeclContext *dc,
3240+
CaseStmt *caseStmt) {
32423241
llvm::SmallDenseMap<Identifier, std::pair<VarDecl *, bool>, 4> latestVars;
32433242
auto recordVar = [&](Pattern *pattern, VarDecl *var) {
32443243
if (!var->hasName())
@@ -3248,16 +3247,10 @@ void swift::bindSwitchCasePatternVars(DeclContext *dc, CaseStmt *caseStmt) {
32483247
// parent of this new variable.
32493248
auto &entry = latestVars[var->getName()];
32503249
if (entry.first) {
3251-
assert(!var->getParentVarDecl() ||
3252-
var->getParentVarDecl() == entry.first);
3253-
var->setParentVarDecl(entry.first);
3254-
32553250
// Check for a mutability mismatch.
3256-
if (pattern && entry.second != var->isLet()) {
3251+
if (entry.second != var->isLet()) {
32573252
// Find the original declaration.
3258-
auto initialCaseVarDecl = entry.first;
3259-
while (auto parentVar = initialCaseVarDecl->getParentVarDecl())
3260-
initialCaseVarDecl = parentVar;
3253+
auto initialCaseVarDecl = entry.first->getCanonicalVarDecl();
32613254

32623255
auto diag = var->diagnose(diag::mutability_mismatch_multiple_pattern_list,
32633256
var->isLet(), initialCaseVarDecl->isLet());
@@ -3268,10 +3261,10 @@ void swift::bindSwitchCasePatternVars(DeclContext *dc, CaseStmt *caseStmt) {
32683261
if (VP->getSingleVar() == var)
32693262
foundVP = VP;
32703263
});
3271-
if (foundVP)
3264+
if (foundVP) {
32723265
diag.fixItReplace(foundVP->getLoc(),
32733266
initialCaseVarDecl->isLet() ? "let" : "var");
3274-
3267+
}
32753268
var->setInvalid();
32763269
initialCaseVarDecl->setInvalid();
32773270
}
@@ -3283,8 +3276,6 @@ void swift::bindSwitchCasePatternVars(DeclContext *dc, CaseStmt *caseStmt) {
32833276
entry.first = var;
32843277
};
32853278

3286-
// Wire up the parent var decls for each variable that occurs within
3287-
// the patterns of each case item. in source order.
32883279
for (auto &caseItem : caseStmt->getMutableCaseLabelItems()) {
32893280
// Resolve the pattern.
32903281
auto *pattern = caseItem.getPattern();
@@ -3300,11 +3291,6 @@ void swift::bindSwitchCasePatternVars(DeclContext *dc, CaseStmt *caseStmt) {
33003291
recordVar(pattern, var);
33013292
});
33023293
}
3303-
3304-
// Wire up the case body variables to the latest patterns.
3305-
for (auto bodyVar : caseStmt->getCaseBodyVariables()) {
3306-
recordVar(nullptr, bodyVar);
3307-
}
33083294
}
33093295

33103296
FuncDecl *TypeChecker::getForEachIteratorNextFunction(

lib/Sema/TypeChecker.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,26 +1416,9 @@ bool checkFallthroughStmt(FallthroughStmt *stmt);
14161416
void checkUnknownAttrRestrictions(
14171417
ASTContext &ctx, CaseStmt *caseBlock, bool &limitExhaustivityChecks);
14181418

1419-
/// Bind all of the pattern variables that occur within a case statement and
1420-
/// all of its case items to their "parent" pattern variables, forming chains
1421-
/// of variables with the same name.
1422-
///
1423-
/// Given a case such as:
1424-
/// \code
1425-
/// case .a(let x), .b(let x), .c(let x):
1426-
/// \endcode
1427-
///
1428-
/// Each case item contains a (different) pattern variable named.
1429-
/// "x". This function will set the "parent" variable of the
1430-
/// second and third "x" variables to the "x" variable immediately
1431-
/// to its left. A fourth "x" will be the body case variable,
1432-
/// whose parent will be set to the "x" within the final case
1433-
/// item.
1434-
///
1435-
/// Each of the "x" variables must eventually have the same type, and agree on
1436-
/// let vs. var. This function does not perform any of that validation, leaving
1437-
/// it to later stages.
1438-
void bindSwitchCasePatternVars(DeclContext *dc, CaseStmt *stmt);
1419+
/// Diagnoses any mutability mismatches for any same-named variables bound by
1420+
/// given CaseStmt.
1421+
void diagnoseCaseVarMutabilityMismatch(DeclContext *dc, CaseStmt *stmt);
14391422

14401423
/// If \p attr was added by an access note, wraps the error in
14411424
/// \c diag::wrap_invalid_attr_added_by_access_note and limits it as an access

0 commit comments

Comments
 (0)