Skip to content

Commit 6f24696

Browse files
authored
Merge pull request #81612 from calda/cal--trailing-comma-missing-from-6.1
Add trailing comma support in cases missing from Swift 6.1
2 parents 794fe00 + 9a3f33d commit 6f24696

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,7 @@ Parser::parseAttributeArguments(SourceLoc attrLoc, StringRef attrName,
21372137
}
21382138

21392139
return parseList(tok::r_paren, parensRange.Start, parensRange.End,
2140-
/*allow sep after last*/ true,
2140+
/*AllowSepAfterLast=*/true,
21412141
{diag::attr_expected_rparen, {attrName, isModifier}},
21422142
parseArg);
21432143
}
@@ -3245,7 +3245,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
32453245
StringRef AttrName = "@_originallyDefinedIn";
32463246
bool SuppressLaterDiags = false;
32473247
bool ParsedUnrecognizedPlatformName = false;
3248-
if (parseList(tok::r_paren, LeftLoc, RightLoc, false,
3248+
if (parseList(tok::r_paren, LeftLoc, RightLoc,
3249+
/*AllowSepAfterLast=*/false,
32493250
diag::originally_defined_in_missing_rparen,
32503251
[&]() -> ParserStatus {
32513252
SWIFT_DEFER {
@@ -4997,7 +4998,7 @@ ParserResult<LifetimeEntry> Parser::parseLifetimeEntry(SourceLoc loc) {
49974998
SourceLoc rParenLoc;
49984999
bool foundParamId = false;
49995000
status = parseList(
5000-
tok::r_paren, lParenLoc, rParenLoc, /*AllowSepAfterLast*/ false,
5001+
tok::r_paren, lParenLoc, rParenLoc, /*AllowSepAfterLast=*/false,
50015002
diag::expected_rparen_after_lifetime_dependence, [&]() -> ParserStatus {
50025003
ParserStatus listStatus;
50035004
foundParamId = true;
@@ -9543,6 +9544,11 @@ ParserStatus Parser::parsePrimaryAssociatedTypeList(
95439544

95449545
// Parse the comma, if the list continues.
95459546
HasNextParam = consumeIf(tok::comma);
9547+
9548+
// The list ends if we find a trailing comma
9549+
if (startsWithGreater(Tok)) {
9550+
break;
9551+
}
95469552
} while (HasNextParam);
95479553

95489554
return Result;

lib/Parse/ParseType.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,10 @@ ParserStatus Parser::parseGenericArguments(SmallVectorImpl<TypeRepr *> &Args,
742742
// Parse the comma, if the list continues.
743743
if (!consumeIf(tok::comma))
744744
break;
745+
746+
// If the comma was a trailing comma, finish parsing the list of types
747+
if (startsWithGreater(Tok))
748+
break;
745749
}
746750
}
747751

@@ -1161,7 +1165,7 @@ ParserResult<TypeRepr> Parser::parseTypeTupleBody() {
11611165
SmallVector<TupleTypeReprElement, 8> ElementsR;
11621166

11631167
ParserStatus Status = parseList(tok::r_paren, LPLoc, RPLoc,
1164-
/*AllowSepAfterLast=*/false,
1168+
/*AllowSepAfterLast=*/true,
11651169
diag::expected_rparen_tuple_type_list,
11661170
[&] () -> ParserStatus {
11671171
TupleTypeReprElement element;
@@ -1605,7 +1609,8 @@ bool Parser::canParseGenericArguments() {
16051609
if (!canParseType())
16061610
return false;
16071611
// Parse the comma, if the list continues.
1608-
} while (consumeIf(tok::comma));
1612+
// This could be the trailing comma.
1613+
} while (consumeIf(tok::comma) && !startsWithGreater(Tok));
16091614

16101615
if (!startsWithGreater(Tok)) {
16111616
return false;

test/Casting/ParameterizedExistentials.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ struct GenericHolder<T>: Holder {
4646
init(value: T) { self.value = value}
4747
}
4848

49-
protocol PairType<T, U> {
49+
protocol PairType<
50+
T,
51+
U,
52+
> {
5053
associatedtype T
5154
associatedtype U
5255

test/Parse/trailing-comma.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ let values: [Int,] = [] // expected-note {{to match this opening '['}} expected-
99
// Tuple and Tuple Pattern
1010

1111
let _ = (a: 1, b: 2, c: 3,)
12+
let _: (a: Int, b: Int, c: Int,) = (a: 1, b: 2, c: 3,)
13+
14+
// Closures
15+
let _: (String, Int, Float,) -> Void
1216

1317
let (_, _,) = (0,1,)
1418

@@ -34,7 +38,7 @@ struct S<T1, T2,> { }
3438

3539
func foo<T1, T2,>() { }
3640

37-
protocol P<T1, T2> {
41+
protocol P<T1, T2,> {
3842
associatedtype T1
3943
associatedtype T2
4044
}
@@ -98,7 +102,7 @@ struct Foo {
98102

99103
}
100104

101-
func f(in: @differentiable(reverse,) (Int) -> Int) { } // expected-warning {{@differentiable' has been renamed to '@differentiable(reverse)' and will be removed in the next release}} expected-error {{unexpected ',' separator}} expected-error {{expected ',' separator}} expected-error {{unnamed parameters must be written with the empty name '_'}}
105+
func f(in: @differentiable(reverse,) (Int) -> Int) { } // expected-warning {{@differentiable' has been renamed to '@differentiable(reverse)' and will be removed in the next release}} expected-error {{expected ',' separator}} expected-error {{unnamed parameters must be written with the empty name '_'}}
102106

103107
@derivative(of: Self.other,) // expected-error {{unexpected ',' separator}}
104108
func foo() {}
@@ -135,4 +139,12 @@ if true, { } // expected-error {{expected '{' after 'if' condition}}
135139

136140
guard true, else { } // expected-error {{expected expression in conditional}}
137141

138-
while true, { } // expected-error {{expected '{' after 'while' condition}}
142+
while true, { } // expected-error {{expected '{' after 'while' condition}}
143+
144+
if #available(OSX 51,) { // expected-error {{expected platform name}}
145+
}
146+
147+
@available(OSX 10.7, iOS 7.0, *,) // expected-error {{expected platform name}} expected-error {{expected declaration}}
148+
@_originallyDefinedIn(module: "HighLevel", OSX 10.9, iOS 13.0,) // expected-error {{unexpected ',' separator}}
149+
@backDeployed(before: OSX 10.9,) // expected-error {{unexpected ',' separator}}
150+
public struct StructWithAvailability {}

test/decl/ext/typealias.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,16 @@ extension FooIntBarFloatDoubleInner {
7878
}
7979
}
8080

81+
struct Foo2<T1, T2, T3,> {}
82+
83+
typealias Bar2<
84+
T1,
85+
T2,
86+
> = Foo2<
87+
T1,
88+
T2,
89+
Bool,
90+
>
91+
92+
let _ = Foo2<Int, Bool, String,>.self
93+
let _ = Bar2<Int, Bool,>()

test/type/types.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,18 @@ do {
219219
subscript(_: inout Double...) -> Bool { true } // expected-error {{'inout' may only be used on function or initializer parameters}}
220220
}
221221
}
222+
223+
let tupleTypeWithTrailingComma: (
224+
bar: String,
225+
quux: String,
226+
)
227+
228+
let _ = (bar: String, quux: String,).self
229+
230+
let closureTypeWithTrailingCommas: (
231+
String,
232+
String,
233+
) -> (
234+
bar: String,
235+
quux: String,
236+
)

0 commit comments

Comments
 (0)