swift-syntax icon indicating copy to clipboard operation
swift-syntax copied to clipboard

Trailing comma implementation

Open mateusrodriguesxyz opened this issue 1 year ago • 15 comments

This is a prototype implementation for Allow trailing comma in tuples, arguments and if/guard/while conditions gated behind -enable-experimental-feature TrailingComma.

mateusrodriguesxyz avatar Feb 29 '24 13:02 mateusrodriguesxyz

@ahoppen I've updated the implementation for conditionals to use a much simpler lookahead approach

mateusrodriguesxyz avatar Mar 07 '24 21:03 mateusrodriguesxyz

@ahoppen I've fixed minor problems with parseArgumentListElements and updated parseConditionList and atStartOfStatementBody implementations to address our conversation. I've also updated the tests and included some substructure assertions to ensure that the expected block is parsed as the statement body.

mateusrodriguesxyz avatar Mar 14 '24 18:03 mateusrodriguesxyz

Could you let me know the plan for other comma separated lists like func parameter list, tuple type, generic paremeter/argument list etc? I believe your proposal includes parameter list support at least, right?

rintaro avatar Mar 19 '24 11:03 rintaro

I just suggested to keep the PR as-is for now and implement trailing closures for other constructs in a follow-up PR (https://github.com/apple/swift-syntax/pull/2515#discussion_r1530160204). I’ve already spent considerable time reviewing this PR and it’s easier review-wise if we can consider the implications of trailing commas for other constructs in a separate PR.

ahoppen avatar Mar 19 '24 11:03 ahoppen

I believe your proposal includes parameter list support at least, right?

That's right. The current proposal and implementation includes tuples, functions parameter/argument list and if, guard and while conditions. I'm keeping minimal and included the ones that I consider the most demanded use cases.

mateusrodriguesxyz avatar Mar 19 '24 11:03 mateusrodriguesxyz

BTW @ahoppen thank you so much for all the help and patience while reviewing this PR and sorry for all the troubles. That's my first time trying to contribute to the language and whatever the outcome I'm happy with all that I've learned.

mateusrodriguesxyz avatar Mar 19 '24 11:03 mateusrodriguesxyz

The current proposal and implementation includes tuples, functions parameter/argument list and if, guard and while conditions.

I don't see implementation/tests for functions parameter list. e.g. from your proposal

func foo(
    a: Int = 0, 
    b: Int = 0, 
) {
}

Am I missing anything?

rintaro avatar Mar 19 '24 12:03 rintaro

To be clear I'm not arguing that this PR should include parameter list support. I just want to make sure I'm not missing anything, and know the plan.

rintaro avatar Mar 19 '24 12:03 rintaro

I don't see implementation/tests for functions parameter list. e.g. from your proposal

I think I didn't add this test case because arguments and parameters are parsed by the same parsePostfixExpressionSuffix so testing arguments covers parameters, but I can add it to make it clear.

mateusrodriguesxyz avatar Mar 19 '24 13:03 mateusrodriguesxyz

arguments and parameters are parsed by the same parsePostfixExpressionSuffix

Parameters are parsed with parseParameterClause() not parsePostfixExpressionSuffix() :)

rintaro avatar Mar 19 '24 13:03 rintaro

Parameters are parsed with parseParameterClause() not parsePostfixExpressionSuffix() :)

Now that's strange because I never touched this functions but if I add your example as a test case parsePostfixExpressionSuffix is also called and it does parse correctly so I assumed that was it 🤔 It was my intention from the beginning to also support parameters.

mateusrodriguesxyz avatar Mar 19 '24 13:03 mateusrodriguesxyz

That's... concerning (for current parser) 😅

FWIW parsePostfixExpressionSuffix is called whenever parsing an expression. In this case parsing 0 (default parameter value) causes parsePostfixExpressionSuffix

rintaro avatar Mar 19 '24 13:03 rintaro

That's... concerning (for current parser) 😅

I think parseParameterClausealready supports trailing comma because !self.at(.endOfFile, .rightParen) breaks the while even if the last parameter has as trailing comma so I think parsePostfixExpressionSuffix was the one blocking it from working it.

https://github.com/apple/swift-syntax/blob/18cd7f90af1615eec95e173d84823a970b7469c6/Sources/SwiftParser/Parameters.swift#L291-L302

mateusrodriguesxyz avatar Mar 19 '24 13:03 mateusrodriguesxyz

Yeah, I mean we've just discovered a bug in the current parser. We should disallow it unless trailingComma experimental feature is enabled. (I don't think it's a scope of this PR)

rintaro avatar Mar 19 '24 14:03 rintaro

I've update https://github.com/apple/swift/pull/71975 implementation to match this PR.

mateusrodriguesxyz avatar Mar 21 '24 15:03 mateusrodriguesxyz

I've update atStartOfStatementBody implementation and resolved the conflicts. I think that's it? @ahoppen @rintaro Thank you both for the review!

mateusrodriguesxyz avatar Apr 05 '24 17:04 mateusrodriguesxyz

One last thing: Could you squash your commits? It makes for a nicer Git history https://github.com/apple/swift-syntax/blob/main/CONTRIBUTING.md#authoring-commits

Other than that, there’s anything to be done from my side. I would like to wait for @rintaro’s approval as well though.

ahoppen avatar Apr 05 '24 20:04 ahoppen

@rintaro do you have any more to add before I squash the commits?

mateusrodriguesxyz avatar Apr 17 '24 11:04 mateusrodriguesxyz

@swift-ci Please test

rintaro avatar Apr 18 '24 16:04 rintaro

@rintaro I had forget to format the source code after I merged with main, sorry. I think it will pass all the checks now.

mateusrodriguesxyz avatar Apr 18 '24 20:04 mateusrodriguesxyz

@swift-ci Please test

rintaro avatar Apr 18 '24 21:04 rintaro

@swift-ci Please test Windows

ahoppen avatar Apr 18 '24 21:04 ahoppen

One last thing: Could you squash your commits?

@ahoppen done!

mateusrodriguesxyz avatar Apr 19 '24 02:04 mateusrodriguesxyz

@swift-ci please test

kimdv avatar Apr 21 '24 20:04 kimdv

@swift-ci please test windows

kimdv avatar Apr 21 '24 20:04 kimdv