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

danglingParentheses.callSite = false should never result in non-compiling code for multiline lambdas #4569

Open
djneades opened this issue Nov 20, 2024 · 5 comments

Comments

@djneades
Copy link

djneades commented Nov 20, 2024

Configuration (required)

version = "3.8.4-RC2"
runner.dialect = scala3
danglingParentheses.callSite = false

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmt test.sc

Steps

Given code like this:

def fn1(arg: String, arg2: String)(f: String ?=> Unit): Unit =
  f(using arg)

def fn2(arg: String, arg2: String)(f: String => Unit): Unit =
  f(arg)

fn1(
  arg = "blue sleeps faster than tuesday",
  arg2 = "the quick brown fox jumped over the lazy dog"): env ?=>
    val x = env
    println(x)

fn2(
  arg = "blue sleeps faster than tuesday",
  arg2 = "the quick brown fox jumped over the lazy dog"): env =>
    val x = env
    println(x)

Problem

Scalafmt formats code like this:

def fn1(arg: String, arg2: String)(f: String ?=> Unit): Unit =
  f(using arg)

def fn2(arg: String, arg2: String)(f: String => Unit): Unit =
  f(arg)

fn1(
  arg = "blue sleeps faster than tuesday",
  arg2 = "the quick brown fox jumped over the lazy dog"): env ?=>
  val x = env
  println(x)

fn2(
  arg = "blue sleeps faster than tuesday",
  arg2 = "the quick brown fox jumped over the lazy dog"): env =>
  val x = env
  println(x)

Expectation

I should like the formatted output to compile. However, the reformatted code does not because the lambda bodies are insufficiently indented:

> scala-cli test.sc
Compiling project (Scala 3.5.2, JVM (23))
[error] ./test.sc:9:63
[error] end of statement expected but '?=>' found
[error]   arg2 = "the quick brown fox jumped over the lazy dog"): env ?=>
[error]                                                               ^^^
[error] ./test.sc:13:1
[error] '}' expected, but unindent found
[error] fn2(
[error] ^^^
[error] ./.scala-build/q_1deee6db16-831d1d41ff/src_generated/main/test.scala:4:12
[error] Not found: test_sc
[error] def args = test_sc.args$
[error]            ^^^^^^^
[error] ./test.sc:9:59
[error] Not found: type env
[error]   arg2 = "the quick brown fox jumped over the lazy dog"): env ?=>
[error]                                                           ^^^
[error] ./test.sc:11:11
[error] Not found: x
[error]   println(x)
[error]           ^
Error compiling project (Scala 3.5.2, JVM (23))
Compilation failed

Workaround

Set danglingParentheses.callSite = true or set newlines.beforeCurlyLambdaParams = always.

@djneades djneades changed the title danglingParentheses.callSite = false should never result in invalid fewer braces indentation for multiline lambdas danglingParentheses.callSite = false should never result in invalid fewer-braces indentation for multiline lambdas Nov 20, 2024
@kitbellew
Copy link
Collaborator

what about

fn3(
  arg = "blue sleeps faster than tuesday",
  arg2 = "the quick brown fox jumped over the lazy dog"):
    val x = env
    println(x)

does it also require indent relative to arg2 and not fn3?

@kitbellew
Copy link
Collaborator

@djneades
Copy link
Author

@kitbellew Hmm, no, the extra indentation is not required there. Curious. This compiles without problem:

def fn3(arg: String, arg2: String)(f: => Unit): Unit =
  f

fn3(
  arg = "blue sleeps faster than tuesday",
  arg2 = "the quick brown fox jumped over the lazy dog"):
  val x = "Hello"
  println(x)

@djneades djneades changed the title danglingParentheses.callSite = false should never result in invalid fewer-braces indentation for multiline lambdas danglingParentheses.callSite = false should never result in non-compiling code for multiline lambdas Nov 20, 2024
@kitbellew
Copy link
Collaborator

@djneades in reading section 2.2.2, i interpret it as stipulating indentation should be relative to enclosing region (which should be fn).

hence, this behaviour might be a compiler bug.

@djneades
Copy link
Author

djneades commented Nov 20, 2024

@kitbellew Understood, thank you!

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

No branches or pull requests

2 participants