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

Named tuples are ambigious in (new) given syntax #22237

Open
SrTobi opened this issue Dec 18, 2024 · 6 comments
Open

Named tuples are ambigious in (new) given syntax #22237

SrTobi opened this issue Dec 18, 2024 · 6 comments
Labels
area:implicits related to implicits area:named-tuples Issues tied to the named tuples feature. area:tooling itype:question

Comments

@SrTobi
Copy link
Contributor

SrTobi commented Dec 18, 2024

Compiler version

3.6.2 with -experimental

Minimized example

I'm currently implementing the new given syntax parsing for the intellij-scala plugin and I stumbled about this:

given (x: Int) = ???

Output

The compiler gives ')' expected, but ':' found as error message.

Expectation

According to the grammar the above code should be allowed
as named tuples are part of SimpleType.

Here the relevant grammar:

GivenDef          ::=  [id ':'] GivenSig
GivenSig          ::=  GivenImpl
                    |  '(' ')' '=>' GivenImpl
                    |  GivenConditional '=>' GivenSig
GivenImpl         ::=  GivenType ([‘=’ Expr] | TemplateBody)
                    |  ...
GivenConditional  ::=  DefTypeParamClause
                    |  DefTermParamClause
                    |  ...
                    |  GivenType 
GivenType         ::=  AnnotType1 {id [nl] AnnotType1}
AnnotType         ::=  SimpleType {Annotation}

DefTermParamClause and GivenType can look the same ((x: Int) for example)

I know that it doesn't make too much sense to have a named tuple in that position, still I'd like to know if that is intentional or not. Just so I don't have to implement and test our parser twice 😀

In the case that is should stay disallowed, I'd suggest changing the error message either to => expected after the ) or something like named tuples are not allowed in this position

@SrTobi SrTobi added the stat:needs triage Every issue needs to have an "area" and "itype" label label Dec 18, 2024
@EugeneFlesselle
Copy link
Contributor

The -experimental option will allow @experimental definitions. An import scala.language.experimental.namedTuples is necessary to use named tuples. Having imported it, the given (x: Int) = ??? parses as expected, on the current main branch at least.

@KacperFKorban
Copy link
Member

@SrTobi The following code compiles correctly:

//> using scala 3.nightly

import scala.language.experimental.namedTuples

given (x: Int) = ???

@SrTobi
Copy link
Contributor Author

SrTobi commented Dec 18, 2024

Ah ok... I was confused by the fact that val x: (y: Int) = ??? compiles when -experimental is used.

Also the example doesn't even relate to what I mean with the above mentioned ambiguity. The example I should have given (pun intended) is: given (x: Int) => Int = ???
Here (x: Int) can be both DefTermParamClause and GivenType in GivenConditional. Currently it's parsed as a DefTermParamClause and I'm going forward with implementing it that way on our side.

Not sure if you want to change anything in the grammar. if not feel free to close this issue. Thx

@EugeneFlesselle
Copy link
Contributor

given (x: Int) => Int = ???

Good point, this one indeed appears to be ambiguous. Though any changes to the grammar will most likely reflect the current behavior.

@sjrd
Copy link
Member

sjrd commented Dec 18, 2024

GivenConditional is supposed to win here. If you really want a given of a function type, you need to put the function type in parentheses. (but that should almost never happen in real programs, because why would you even want a given of a function type other than to design a puzzler?)

@SrTobi
Copy link
Contributor Author

SrTobi commented Dec 18, 2024

@sjrd it's not about function type vs given conditional+givenImpl, but about parameter clause vs named tuple within a given conditional

@Gedochao Gedochao added itype:question area:implicits related to implicits area:named-tuples Issues tied to the named tuples feature. area:tooling and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:implicits related to implicits area:named-tuples Issues tied to the named tuples feature. area:tooling itype:question
Projects
None yet
Development

No branches or pull requests

5 participants