Improve diagnostic in cases like 'if let foo.bar'

This commit is contained in:
Cal Stephens
2022-03-13 12:20:01 -07:00
parent 38e2b70d5d
commit ac7529f5bd
3 changed files with 11 additions and 2 deletions

View File

@@ -1635,6 +1635,13 @@ Parser::parseStmtConditionElement(SmallVectorImpl<StmtConditionElement> &result,
declRefExpr->setImplicit();
Init = makeParserResult(declRefExpr);
} else if (!ThePattern.isNull()) {
// If the pattern is present but isn't an identifier, the user wrote
// something invalid like `let foo.bar`. Emit a special diagnostic for this,
// with a fix-it prepending "<#identifier#> = "
auto diagLoc = ThePattern.get()->getSemanticsProvidingPattern()->getStartLoc();
diagnose(diagLoc, diag::conditional_var_valid_identifiers_only)
.fixItInsert(diagLoc, "<#identifier#> = ");
} else {
diagnose(Tok, diag::conditional_var_initializer_required);
}