Skip to content

Commit b839e07

Browse files
committed
[Parser] Adds a fix-it to convert the subscript into two arrays
1 parent 8092dd9 commit b839e07

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,9 @@ ERROR(extra_rbracket,PointsToFirstBadToken,
763763
ERROR(extra_colon,PointsToFirstBadToken,
764764
"unexpected ':' in type; did you mean to write a dictionary type?", ())
765765
WARNING(subscript_collection_element_invalid_index, none,
766-
"index '%0' in subscript expression is out of bounds", (StringRef))
766+
"index '%0' in subscript expression is out of bounds", (StringRef))
767+
NOTE(subscript_collection_element_fix_it, none, "did you mean to write two "
768+
"array literals instead?", ())
767769

768770
// Tuple Types
769771
ERROR(expected_rparen_tuple_type_list,none,

lib/Parse/ParseExpr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3509,6 +3509,12 @@ void Parser::validateCollectionElement(ParserResult<Expr> element) {
35093509
diag::subscript_collection_element_invalid_index,
35103510
indexExpr->getDigitsText());
35113511
diag.highlight(subscriptExpr->getSourceRange());
3512+
diag.flush();
3513+
3514+
auto note = diagnose(subscriptExpr->getLoc(),
3515+
diag::subscript_collection_element_fix_it);
3516+
note.fixItInsertAfter(baseExpr->getRBracketLoc(), ",");
3517+
35123518
element.setIsParseError();
35133519
}
35143520
}

test/type/array.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ func passAssocType<T : HasAssocType>(_ t: T) {
117117
// SR-11134
118118

119119
let sr_11134_1 = [["a"][0]] // Ok
120-
let sr_11134_2 = [["a"][1]] // expected-warning {{index '1' in subscript expression is out of bounds}}
120+
let sr_11134_2 = [["a"][1]] // expected-warning {{index '1' in subscript expression is out of bounds}} // expected-note {{did you mean to write two array literals instead?}}{{24-24=,}}
121+
let sr_11134_3 = [[1, 1, 1], [], [4, 5, 6, 7], [0], [], [] [42]] // expected-warning {{index '42' in subscript expression is out of bounds}} // expected-note {{did you mean to write two array literals instead?}}{{59-59=,}}
121122

122-
let sr_11134_3 = [[1, 1, 1], [], [4, 5, 6, 7], [0], [], [] [42]] // expected-warning {{index '42' in subscript expression is out of bounds}}

0 commit comments

Comments
 (0)