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

fix: disallow toplevel infix definitions for vals, vars, givens, methods and implicits #17994

Merged
merged 10 commits into from
Jul 13, 2023

Conversation

arainko
Copy link
Contributor

@arainko arainko commented Jun 17, 2023

Disallow infix toplevel definitions for anything that is not a class, typealias, match type, extension method and a trait (and objects as well but these are handled in their own PR).

I'm kind of on the edge about adding a new error message class since originally I wanted to go with ModifierNotAllowed but I found its error message misleading in this context hence ToplevelDefCantBeInfix, I don't know the exact preferences when it comes to things like these so feel free to point out any irregularities 😄

Part of #17738
Continuation of #17966

@mbovel mbovel self-requested a review June 19, 2023 11:07
Copy link
Contributor

@Sporarum Sporarum left a comment

Choose a reason for hiding this comment

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

Apart from that nitpick, it looks perfect to me !

compiler/src/dotty/tools/dotc/reporting/messages.scala Outdated Show resolved Hide resolved
@Sporarum
Copy link
Contributor

@arainko
I'm sorry we didn't merge this sooner, there are now merge conflicts, could you fix them ?

@mbovel
Once that's done, let's merge this ?

@arainko
Copy link
Contributor Author

arainko commented Jun 30, 2023

No worries at all, I'll fix the conficts as soon as possible (probably tomorrow morning)

@arainko arainko force-pushed the issue-17738-toplevel-infix branch from 891c0d4 to 3f3bd01 Compare July 1, 2023 12:27
Copy link
Member

@mbovel mbovel left a comment

Choose a reason for hiding this comment

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

I'm kind of on the edge about adding a new error message class since originally I wanted to go with ModifierNotAllowed but I found its error message misleading in this context […]

I am also not sure about this. Could we for example have ModifierNotAllowedForDefinition take an additional string argument that would detail the error and be displayed with -explain?

Or could TopLevelDefCantBeInfix extend ModifierNotAllowedForDefinition and share the same message ID?

I'll ask the core contributors what they think about it later today.

@mbovel
Copy link
Member

mbovel commented Jul 4, 2023

After discussing with the other contributors, we came to the conclusion that a single error message would be more appropriate here. Could you please add an argument with a default value to ModifierNotAllowedForDefinition allowing to give more details as I suggested above? Thanks!

Copy link
Contributor

@Sporarum Sporarum left a comment

Choose a reason for hiding this comment

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

I'll request changes, that way we don't merge this by accident, and can auto-merge it safely later

…fierNotAllowed with a custom explanation in its place
@Sporarum Sporarum self-requested a review July 11, 2023 14:37
@Sporarum Sporarum dismissed their stale review July 11, 2023 14:38

Comments addressed, but other remain, so can't be Accepted yet either

@Sporarum
Copy link
Contributor

(That way any review can approve, instead of specifically me)

Comment on lines 565 to 570
val defKind =
if sym.flags.is(Method) then "def"
else if sym.flags.is(Mutable) then "var"
else if sym.flags.is(Given) then "given"
else "val"
fail(ModifierNotAllowedForDefinition(Flags.Infix, s"a toplevel $defKind cannot be infix"))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
val defKind =
if sym.flags.is(Method) then "def"
else if sym.flags.is(Mutable) then "var"
else if sym.flags.is(Given) then "given"
else "val"
fail(ModifierNotAllowedForDefinition(Flags.Infix, s"a toplevel $defKind cannot be infix"))
fail(ModifierNotAllowedForDefinition(Flags.Infix, s"A top-level ${sym.showKind} cannot be infix."))

Looks like sym.showKind could be a good fit here.

Its implemented there: https://github.com/lampepfl/dotty/blob/2b391c82de1861d8ab0196e78dd2975539b8082a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala#L477-L502

Note that the check file would need to be updated as well as the names are not exactly the same as what you currently have.

Copy link
Member

Choose a reason for hiding this comment

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

Minor: my suggestion also makes the explanation a full sentence (we don't have very coherent formatting for error messages, but it seems like full sentences for explanations is more common), and using top-level instead of toplevel as it is more common.

@mbovel mbovel self-requested a review July 12, 2023 21:37
Copy link
Member

@mbovel mbovel left a comment

Choose a reason for hiding this comment

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

Looks good to me. Needs to be squashed when merging.

@mbovel mbovel merged commit fd8bcea into scala:main Jul 13, 2023
@Sporarum
Copy link
Contributor

Thanks again @arainko for your excellent work !

@arainko
Copy link
Contributor Author

arainko commented Jul 14, 2023

I think I should be the one thanking you for having so much patience with me ❤️

@Sporarum
Copy link
Contributor

I've not needed patience with you !

And even if I had, I'm more than happy to do the work to make this place welcoming ^^

@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants