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

Strange behaviour in repl when using fewerBraces, first it does not work then it does #14491

Closed
bjornregnell opened this issue Feb 15, 2022 · 5 comments · Fixed by #14493
Closed

Comments

@bjornregnell
Copy link
Contributor

bjornregnell commented Feb 15, 2022

Compiler version

Scala 3.1.3-RC1-bin-SNAPSHOT-nonbootstrapped-git-ccd39b7 using repl in sbt from current dotty repo.

Minimized code and output

Welcome to Scala 3.1.3-RC1-bin-SNAPSHOT-nonbootstrapped-git-ccd39b7 (11.0.13, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                               
scala> import language.experimental.fewerBraces
                                                                                                                               
scala> Seq(1,2,3).apply:   // press Alt+Enter to get new line
     |   1
-- Error: -----------  // this should work and not give this error
1 |Seq(1,2,3).apply:   
  |^^^^^^^^^^^^^^^^
  |missing arguments for method apply in trait SeqOps
1 error found
                                                                                                                               
scala> Seq(1,2,3).apply:  // don't press Alt+Enter
val res0: Int => Int = Lambda$14686/0x000000080384a040@44471878
                                                                                                                               
scala> Seq(1,2,3).apply:  // press Alt+Enter to get new line
     |   1
     | 
val res1: Int = 2   // now it suddenly works as should
                                                                                                                               
scala> 

Expectation

The first try above should work. Now it first does not work as expected; then it starts working after creating the first lambda by providing an empty arg.

Maybe it is cased by some kind of jline parsing issue?

@bjornregnell bjornregnell added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 15, 2022
@som-snytt
Copy link
Contributor

That is a new twist on #13097

@bjornregnell
Copy link
Contributor Author

Indeed a twisty twist 🔀

@griggt
Copy link
Collaborator

griggt commented Feb 15, 2022

The middle step of creating the lambda does not seem to be necessary:

scala> import language.experimental.fewerBraces
scala> Seq(7,8,9).apply:
     |   1
-- Error: ----------------------------------------------------------------------
1 |Seq(7,8,9).apply:
  |^^^^^^^^^^^^^^^^
  |missing arguments for method apply in trait SeqOps
1 error found

scala> Seq(7,8,9).apply:
     |   1
     | 
val res0: Int = 8

scala> :reset
scala> import language.experimental.fewerBraces

scala> Seq(7,8,9).apply:
     |   1
-- Error: ----------------------------------------------------------------------
1 |Seq(7,8,9).apply:
  |^^^^^^^^^^^^^^^^
  |missing arguments for method apply in trait SeqOps
1 error found

The first time the expression is entered in the REPL the parsed tree looks like

Typed(Select(Apply(Ident(Seq),List(Number(7,Whole(10)), Number(8,Whole(10)), Number(9,Whole(10)))),apply),SingletonTypeTree(Literal(Constant(1))))

whereas when re-entered it parses into

Apply(Select(Apply(Ident(Seq),List(Number(7,Whole(10)), Number(8,Whole(10)), Number(9,Whole(10)))),apply),List(Number(1,Whole(10))))

Seems like some sort of resident compiler glitch.

@griggt griggt added area:parser area:repl and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 15, 2022
@griggt
Copy link
Collaborator

griggt commented Feb 15, 2022

The issue does not manifest if fewer braces support is enabled by starting the REPL with the -language:experimental.fewerBraces command line option rather than using the import during the REPL session. Which leads me to suspect that the compiler context is not being (fully) updated upon first seeing the import statement.

@griggt
Copy link
Collaborator

griggt commented Feb 15, 2022

So, the issue has to do with how import statements from previous REPL lines are brought into the compiler context.

This happens here:

https://github.com/lampepfl/dotty/blob/1476a30b7949d2078aa6ff6660b54774cdeb2f00/compiler/src/dotty/tools/repl/ReplCompiler.scala#L41-L66

where the user defined imports from previous lines are folded into the rootContext for the new compiler run.

However, the REPL is special in that the Parser is run standalone rather than as part of the frontend phases of the compiler, which in turn means that the REPL parser is not seeing an up-to-date context since the next run of the compiler has not yet started.

scala> import language.experimental.fewerBraces

scala> "hello"
val res0: String = hello

scala> Seq(7,8,9).apply:
     |   1
     | 
val res1: Int = 8

The issue does not manifest in the example above because the import is brought into context during compilation of the line containing "hello". Indeed the issue is only present when the code on the REPL line immediately following the fewerBraces import requires fewerBraces to be enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants