Skip to content

Parse unary operators as regular identifiers when backquoted #15198

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

Merged
merged 1 commit into from
Jun 12, 2022

Conversation

adampauls
Copy link
Contributor

A sheepish attempt at retrying #14299. In that PR, I suggested allowing unaries like + to be interpreted as regular Apply nodes when followed by type parameters. @odersky commented that

What is the reason for the change? I guess we all agree that a + operation as in the test is very bad style. Why encourage it with the change to the parsing rules?

I find it also concerning that

+(6)
//and
+[Int](6)

now are parsed differently. This goes counter to the intuition that type arguments are optional.

@som-snytt pointed out that

There is an historically very consistent expectation that backticks should do something here.

I strongly agree with that last statement -- it makes sense to me that anything in backticks is "just some identifier" and never receives syntactic privilege. This is already the case for keywords and in pattern matching. I was rather surprised to learn that backquotes don't suppress special unary operator parsing as it is.

I suspect this PR will not be accepted because it is technically a breaking change, although I suspect it will affect no one, and I would gladly guard it behind a setting it if that's the path to acceptance. I thought I'd try anyways!

(This can be made non-breaking by allowing

`+`[Int](6) // parses as apply 
`+`(6) // parses as unary

but I suspect that is a non-starter because of the surprise).

@adampauls adampauls changed the title Parse unary operators as regular identifiers when backquoted and followed by type parameters Parse unary operators as regular identifiers when backquoted May 16, 2022
@bishabosha bishabosha requested a review from odersky May 20, 2022 08:22
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 think that's a useful change. But no need for the isNonBackquotedIdent abstraction.

@odersky odersky removed their assignment May 26, 2022
@som-snytt
Copy link
Contributor

My previous observation

There is an historically very consistent expectation that backticks should do something here.

ought to have read, "very consistent yet very mistaken expectation".

The identifiers taken as prefix operators are already regular identifiers. The proposal is to make them ineligible as prefix operators when backquoted.

We will need an answer on StackOverflow for "What are all the uses of backquotes in Scala?"

@adampauls
Copy link
Contributor Author

Thanks for the suggestions. I also added a note to the PrefixOperator line in both syntax.md files.

@adampauls adampauls requested a review from odersky May 26, 2022 15:15
@adampauls
Copy link
Contributor Author

@odersky friendly bump on this.

@@ -254,7 +254,7 @@ InfixExpr ::= PrefixExpr
| InfixExpr MatchClause
MatchClause ::= ‘match’ <<< CaseClauses >>> Match(expr, cases)
PrefixExpr ::= [PrefixOperator] SimpleExpr PrefixOp(expr, op)
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ -- unless backquoted
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is not necessary, since the lexemes are spelled out. The spec text does say, "one of the identifiers...", so it must add, "which must not be enclosed in backquotes." (Where backquote is allowed, it must be shown in a production.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which spec are you referring to?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And do you think I should remove the comment? I think it's kind of ambiguous without it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean the spec under scala 2 https://scala-lang.org/files/archive/spec/2.13/06-expressions.html#prefix-operations

Personally, I find it more precise as it is. If it doesn't say you can use

'`' '~' '`'

then you can't, modulo bugs. Backquotes are not some metacharacter with respect to syntax. Or they are not onions, as in, "Would you like onions with that?"

However, there are two groups of consumers for the two syntax pages, so maybe they would say the comment is clarifying. I don't know which group, but hopefully they will voice their preference.

Copy link
Contributor

@som-snytt som-snytt left a comment

Choose a reason for hiding this comment

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

It's so obvious when you put it like that. Until now, the spec text and syntax summary were at odds. I'll update the spec under Scala 2 and backport.

@odersky odersky merged commit 5015425 into scala:main Jun 12, 2022
@Kordyjan Kordyjan added this to the 3.2.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.

4 participants