@@ -2071,22 +2071,45 @@ void swift::checkUnknownAttrRestrictions(
2071
2071
}
2072
2072
2073
2073
void swift::bindSwitchCasePatternVars (CaseStmt *caseStmt) {
2074
- llvm::SmallDenseMap<Identifier, VarDecl *, 4 > latestVars;
2074
+ llvm::SmallDenseMap<Identifier, std::pair< VarDecl *, bool > , 4 > latestVars;
2075
2075
auto recordVar = [&](VarDecl *var) {
2076
2076
if (!var->hasName ())
2077
2077
return ;
2078
2078
2079
2079
// If there is an existing variable with this name, set it as the
2080
2080
// parent of this new variable.
2081
2081
auto &entry = latestVars[var->getName ()];
2082
- if (entry) {
2082
+ if (entry. first ) {
2083
2083
assert (!var->getParentVarDecl () ||
2084
- var->getParentVarDecl () == entry);
2085
- var->setParentVarDecl (entry);
2084
+ var->getParentVarDecl () == entry.first );
2085
+ var->setParentVarDecl (entry.first );
2086
+
2087
+ // Check for a mutability mismatch.
2088
+ if (entry.second != var->isLet ()) {
2089
+ // Find the original declaration.
2090
+ auto initialCaseVarDecl = entry.first ;
2091
+ while (auto parentVar = initialCaseVarDecl->getParentVarDecl ())
2092
+ initialCaseVarDecl = parentVar;
2093
+
2094
+ auto diag = var->diagnose (diag::mutability_mismatch_multiple_pattern_list,
2095
+ var->isLet (), initialCaseVarDecl->isLet ());
2096
+
2097
+ VarPattern *foundVP = nullptr ;
2098
+ var->getParentPattern ()->forEachNode ([&](Pattern *P) {
2099
+ if (auto *VP = dyn_cast<VarPattern>(P))
2100
+ if (VP->getSingleVar () == var)
2101
+ foundVP = VP;
2102
+ });
2103
+ if (foundVP)
2104
+ diag.fixItReplace (foundVP->getLoc (),
2105
+ initialCaseVarDecl->isLet () ? " let" : " var" );
2106
+ }
2107
+ } else {
2108
+ entry.second = var->isLet ();
2086
2109
}
2087
2110
2088
2111
// Record this variable as the latest with this name.
2089
- entry = var;
2112
+ entry. first = var;
2090
2113
};
2091
2114
2092
2115
// Wire up the parent var decls for each variable that occurs within
0 commit comments