Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatOps vertical multiline: explicit implicits #3478

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1233,9 +1233,10 @@ class FormatOps(
case Decision(FormatToken(LeftParenOrBracket(), _, m), ss)
if allParenOwners.contains(m.leftOwner) =>
ss.filter(!_.isActiveFor(SplitTag.VerticalMultilineSingleLine))
case Decision(ftd @ FormatToken(soft.ImplicitOrUsing(), _, _), _)
case Decision(ftd @ FormatToken(soft.ImplicitOrUsing(), _, m), _)
if style.newlines.forceAfterImplicitParamListModifier &&
!tokens.isRightCommentThenBreak(ftd) =>
!tokens.isRightCommentThenBreak(ftd) &&
hasImplicitParamList(m.leftOwner) =>
Seq(Split(Newline, 0))
}

Expand Down
24 changes: 8 additions & 16 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4277,17 +4277,14 @@ object a:
)(b: B,
bs: B*
)(implicit
implicit
c: C) {}
implicit c: C) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, how does it parse? Two implicits in a row doesn't parse for me 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, then we should change our scalameta parser :) or the grammar:

under https://dotty.epfl.ch/docs/internals/syntax.html#type-and-value-parameters

ClsParamClauses   ::=  {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
...
ClsParam          ::=  ...
                       [{Modifier} (‘val’ | ‘var’) | ‘inline’] Param

and under https://dotty.epfl.ch/docs/internals/syntax.html#bindings-and-imports

Modifier          ::=  LocalModifier
...
LocalModifier     ::=  ...
                    |  ‘implicit’

so nothing really prevents multiple implicits. I don't know if {...} means multiple different ones, but even in that case the first parameter is definitely covered.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the grammar then. Both the compiler and scalameta fail to parse it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, if scalameta did fail to parse it, then scalafmt wouldn't be able to format it. perhaps a different dialect?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in any case, this specific change doesn't really touch the multiple implicits logic, it's just some of the tests which are affected. unless, of course, the compiler doesn't allow explicit implicits all of a sudden, like:

...(
  implicit a: A,
  implicit b: B
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ach wait, that's my bad. It is possible for classes and not for defs, which I didn't realize. But it fails for both classes and defs when trying to compiler, so this seems like an issue in scalameta parser only.

But yeah, it's not strictly related to this PR I was just confused how this even parsed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we open an issue in the parser and describe how it works? does it allow explicit implicits, does it require unique modifiers per parameter, does it only check consecutive ones? are there modifiers which aren't allowed at all?

ideally, they'd revise the grammar...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is an omission really and we can open an issue in the parser, but most other modifiers should be handled properly
just implicit/using are a weird case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgodzik could you please check this:

class A(implicit implicit val a: String):
  // foo

in my Intellij IDE it works (and without val doesn't).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an error from the compiler: Repeated modifier implicit

class F(
a: A,
as: A*
)(b: B,
bs: B*
)(implicit
c: C,
implicit
d: D) {}
)(implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -4296,8 +4293,7 @@ object a:
)(implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
<<< interleaved, vertical multiline, implicitParamListModifierForce = [before]
maxColumn = 40
verticalMultiline.atDefnSite = true
Expand Down Expand Up @@ -4381,18 +4377,15 @@ object a:
bs: B*
)(
implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(
implicit
c: C,
implicit
d: D) {}
implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -4402,8 +4395,7 @@ object a:
implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
<<< interleaved, short
maxColumn = 23
===
Expand Down
24 changes: 8 additions & 16 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4088,17 +4088,14 @@ object a:
)(b: B,
bs: B*
)(implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(implicit
c: C,
implicit
d: D) {}
)(implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -4107,8 +4104,7 @@ object a:
)(implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
<<< interleaved, vertical multiline, implicitParamListModifierForce = [before]
maxColumn = 40
verticalMultiline.atDefnSite = true
Expand Down Expand Up @@ -4192,18 +4188,15 @@ object a:
bs: B*
)(
implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(
implicit
c: C,
implicit
d: D) {}
implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -4213,8 +4206,7 @@ object a:
implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
<<< interleaved, short
maxColumn = 23
===
Expand Down
24 changes: 8 additions & 16 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4316,17 +4316,14 @@ object a:
)(b: B,
bs: B*
)(implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(implicit
c: C,
implicit
d: D) {}
)(implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -4335,8 +4332,7 @@ object a:
)(implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
<<< interleaved, vertical multiline, implicitParamListModifierForce = [before]
maxColumn = 40
verticalMultiline.atDefnSite = true
Expand Down Expand Up @@ -4420,18 +4416,15 @@ object a:
bs: B*
)(
implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(
implicit
c: C,
implicit
d: D) {}
implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -4441,8 +4434,7 @@ object a:
implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
<<< interleaved, short
maxColumn = 23
===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4408,17 +4408,14 @@ object a:
)(b: B,
bs: B*
)(implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(implicit
c: C,
implicit
d: D) {}
)(implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -4427,8 +4424,7 @@ object a:
)(implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
<<< interleaved, vertical multiline, implicitParamListModifierForce = [before]
maxColumn = 40
verticalMultiline.atDefnSite = true
Expand Down Expand Up @@ -4512,18 +4508,15 @@ object a:
bs: B*
)(
implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(
implicit
c: C,
implicit
d: D) {}
implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -4533,8 +4526,7 @@ object a:
implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
<<< interleaved, short
maxColumn = 23
===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,14 @@ object a {
)(b: B,
bs: B*
)(implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(implicit
c: C,
implicit
d: D) {}
)(implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -404,8 +401,7 @@ object a {
)(implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
}
<<< explicit implicits, implicitParamListModifierForce = [before]
maxColumn = 40
Expand Down Expand Up @@ -461,18 +457,15 @@ object a {
bs: B*
)(
implicit
implicit
c: C) {}
implicit c: C) {}
class F(
a: A,
as: A*
)(b: B,
bs: B*
)(
implicit
c: C,
implicit
d: D) {}
implicit c: C,
implicit d: D) {}
class F(
a: A,
as: A*
Expand All @@ -482,6 +475,5 @@ object a {
implicit
c: C,
d: D,
implicit
e: E) {}
implicit e: E) {}
}