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

Inconsistent results with function arguments and significant indentation #18345

Closed
JD557 opened this issue Aug 5, 2023 · 4 comments · Fixed by #18349
Closed

Inconsistent results with function arguments and significant indentation #18345

JD557 opened this issue Aug 5, 2023 · 4 comments · Fixed by #18349

Comments

@JD557
Copy link
Contributor

JD557 commented Aug 5, 2023

Compiler version

3.3.0

Minimized code

Assuming the following definitions:

extension (vec: Seq[Int])
  def iterate[T](body: (() => Int) => T): T =
    val iterator = vec.iterator
    body(() => iterator.nextOption().getOrElse(0))

def withSequence[T](n: Int)(body: Seq[Int] => T): T =
  body((0 to n))

This code works fine with the old style:

withSequence(2) {
  _.iterate { next =>
    next() + next() + next() + next()
  }
}

But converting it to significant indentation fails with cryptic problems

Output

Example 1

withSequence(2): _.iterate: next =>
  next() + next() + next() + next()

Fails with

; expected but . found

end of statement expected but '.' found

Found:    (Seq[Int] => Any) => Any
Required: 

Example 2

withSequence(2):
  _.iterate:
    next =>
      next() + next() + next() + next()

Fails with

indented definitions expected, _ found

Example 3

withSequence(2): x =>
  x.iterate:
    next =>
      next() + next() + next() + next()

Fails with

Recursive value $t needs type

Example 4

Surprisingly, the last two examples work fine when stored in a variable

val foo = withSequence(2): x =>
  x.iterate:
    next =>
      next() + next() + next() + next()

val bar = withSequence(2):
  _.iterate:
    next =>
      next() + next() + next() + next()

Expectation

I would expect at least examples 2 and 3 to behave the same when stored in a variable or not.

I would also argue that all examples should compile, although maybe I'm missing some precedence rules that would make some of them ambiguous.

@JD557 JD557 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 5, 2023
@JD557 JD557 changed the title Inconsistent type inference with function arguments and significant indentation Inconsistent results with function arguments and significant indentation Aug 5, 2023
@dwijnand
Copy link
Member

dwijnand commented Aug 7, 2023

I don't have a firm grip on the indentation syntax for method closures. Can someone else triage this?

@anatoliykmetyuk anatoliykmetyuk added area:parser and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 7, 2023
@odersky
Copy link
Contributor

odersky commented Aug 7, 2023

All examples compile on current nightly on my system.

@dwijnand dwijnand closed this as not planned Won't fix, can't repro, duplicate, stale Aug 7, 2023
odersky added a commit to dotty-staging/dotty that referenced this issue Aug 7, 2023
@JD557
Copy link
Contributor Author

JD557 commented Aug 7, 2023

For reference, I suspect there might still be something going on here.

Here's a gist with all examples ready for scala-cli: https://gist.github.com/JD557/2c7e4817660da73e7eaaca0228e8df42

I can still reproduce the first problem in nightly with scala-cli https://gist.githubusercontent.com/JD557/2c7e4817660da73e7eaaca0228e8df42/raw/02eba9011520ecf9e93f99b6dc1fe80af768226f/example-1.sc (although adding a new line like in https://github.com/lampepfl/dotty/pull/18349/files#diff-61e3395826536ea12dde7c2a261d47f4651bfd03d97737b418123c802b5307afR11-R12 fixes the problem).

Surprisingly, I cannot reproduce the problems from examples 2 and 3 with scala-cli (either with nightly or with the current version).

However, the problems still appear on Scastie, even on 3.3.1-RC1 (example 2, example 3).

Maybe the way scala-cli and scastie are converting scripts in a different way (e.g. wrapping it in an object) that is leading to the different behaviors... Unfortunately, I'm not sure how to get access to scastie's output .scala file.

@odersky
Copy link
Contributor

odersky commented Aug 7, 2023

The first example is not supposed to work. The others should work.

odersky added a commit that referenced this issue Aug 7, 2023
Kordyjan pushed a commit that referenced this issue Dec 1, 2023
Closes #18345

[Cherry-picked 072b2e5]
Kordyjan pushed a commit that referenced this issue Dec 7, 2023
Closes #18345

[Cherry-picked 072b2e5]
Kordyjan pushed a commit that referenced this issue Dec 7, 2023
Closes #18345

[Cherry-picked 072b2e5]
Kordyjan added a commit that referenced this issue Dec 8, 2023
Backports #18349 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
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