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

Just warn on type ascription on a pattern #17454

Merged
merged 2 commits into from
May 12, 2023

Conversation

prolativ
Copy link
Contributor

Raise a warning instead of an error for a type ascription on a pattern other than a variable or a number literal.

This partially reverts the changes from #16150.
This change is motivated by not breaking source compatibility for a number of projects in the Open Community Build.

@prolativ
Copy link
Contributor Author

This PR undid the changes in the syntax specification as introduced in the mentioned PR, indicating that the parser should not directly raise an error for some type ascriptions on patterns, but as spotted in #17443 the specification might need to be updated further in a different way to make things consistent

…n other than a variable or a number literal.

This partially reverts the changes from scala#16150.
This change is motivated by not breaking source compatibility for a number of projects in the Open Community Build.
@prolativ prolativ force-pushed the pattern-type-ascription-warning branch from 6d8d23b to b65ef59 Compare May 10, 2023 12:58
Copy link
Member

@dwijnand dwijnand left a comment

Choose a reason for hiding this comment

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

Regrettably LGTM

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

I am not sure what the warning should express. Is it an implementation restriction? (which would mean we try to lift it in the future). In this case it should be labelled "Implementation restriction: ". Or is it something that will be an error in the future? In that case we should make it an errorOrMigrationWarning instead.

That also affects syntax. If it is something we want to restrict in the future we should keep the current syntax around. Parsing could well be different from syntax. I think it's friendlier to accept Pattern2 on the LHS but then reject semantically, than to implement the restricted syntax verbatim.

if in.isColon then
val isVariableOrNumber = isVarPattern(p) || p.isInstanceOf[Number]
if !isVariableOrNumber then
warning(em"Only variable and number literal patterns can have type ascriptions")
Copy link
Contributor

Choose a reason for hiding this comment

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

We should have a proper test for this. tests/neg/t5702-neg-bad-and-wild.check does not count since it is a syntax error in other ways already.

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 tests/neg-custom-args/fatal-warnings/i15893.scala and tests/neg-custom-args/fatal-warnings/i10994.scala are enough as a test.

Copy link
Contributor

Choose a reason for hiding this comment

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

I overlooked i10994.scala. Yes, agreed, that's enough.

@Kordyjan
Copy link
Contributor

Or is it something that will be an error in the future?

Yes, that's the case. This should be an error, and we have made it an error. The problem is we were too eager, and this should have first gone through the stage of being a warning, as multiple libraries are using the forbidden syntax. I have nothing against reverting this PR soon and going back to reporting an error in 3.4.0

@odersky
Copy link
Contributor

odersky commented May 10, 2023

So then it should be an errrorOrMigrationWarning with the error enabled under -source future.

@odersky
Copy link
Contributor

odersky commented May 10, 2023

I think the changes should then be:

  • leave the restricted syntax, since we really want to make it an error ASAP, and the syntax should reflect that.
  • use the suggested changes in the parser, so that the warning or error is more specific than just => expected (which would happen if we implemented the restricted syntax literally)
  • use an errorOrMigrationWarning instead of an error and explain that this syntax will be an error in future language versions.

…uage specification

* Make the parser's warning a migration warning
@prolativ prolativ force-pushed the pattern-type-ascription-warning branch from 8a6bf55 to 07c395b Compare May 11, 2023 11:29
@prolativ prolativ requested a review from odersky May 11, 2023 11:30
@Kordyjan Kordyjan enabled auto-merge May 12, 2023 08:09
@Kordyjan Kordyjan added the backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. label May 12, 2023
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

LGTM!

@Kordyjan Kordyjan merged commit cba5c9a into scala:main May 12, 2023
@Kordyjan Kordyjan deleted the pattern-type-ascription-warning branch May 12, 2023 08:32
Kordyjan added a commit that referenced this pull request May 12, 2023
@Kordyjan Kordyjan added backport:done This PR was successfully backported. and removed backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. labels May 15, 2023
@Kordyjan Kordyjan modified the milestones: 3.3.1, 3.3.0 Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:done This PR was successfully backported.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants