Skip to content

Single line case is multi-line #14721

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

Closed
som-snytt opened this issue Mar 21, 2022 · 0 comments · Fixed by #14738
Closed

Single line case is multi-line #14721

som-snytt opened this issue Mar 21, 2022 · 0 comments · Fixed by #14738
Assignees
Milestone

Comments

@som-snytt
Copy link
Contributor

Compiler version

Scala code runner version 3.1.3-RC1-bin-SNAPSHOT-git-aa59abe -- Copyright 2002-2022, LAMP/EPFL

Minimized code

import scala.util.chaining.given

class C:
  def op: Unit = throw new RuntimeException("a").tap(_ => println("throw a"))
  def handler: Unit = throw new RuntimeException("b").tap(_ => println("throw b"))
  def test: Unit =
    try op
    catch case _: NullPointerException =>
    handler
  end test
  def test1: Unit =
    try op
    //catch case _: NullPointerException =>
    catch
    case _: NullPointerException =>
    end try
    handler
  end test1

@main def test() = println {
  val c = new C()
  c.test
  //c.test1
}
Compilation output
    def test: Unit =
      try op catch {
        {
          case _:NullPointerException => handler
        }
      }
    def test1: Unit =
      {
        try op catch {
          {
            case _:NullPointerException =>
              <empty>
          }
        }
        handler
      }

Expectation

In test, the call to handler is in the case, as opposed to following it as in test1.

This is probably because ExprCaseClause must consume an Expr.

The single line catch case in test1 is not allowed with an empty body before end try.

That may be a feature, but the doc for new control syntax says it must "follow on a single line"; maybe that wording just needs to be tweaked to say "start on the same line".

However, it's also very confusing to read. It doesn't conform to intuitions acquired from optional braces (which doesn't govern this syntax). Normally, the aligned token that is not case incurs the outdent. (And now I see that is not only when the case is aligned with match.)

  def f(x: Int): Int =
    x match
    case 1 =>
    println("no") ; 42
    case _ => 27

I love single line catch case quiet syntax, but would love it more on a single line.

Sample in the wild where it's not obvious what was intended.

@som-snytt som-snytt added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 21, 2022
@odersky odersky added area:parser and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 21, 2022
@odersky odersky self-assigned this Mar 21, 2022
odersky added a commit to dotty-staging/dotty that referenced this issue Mar 21, 2022
Fixes scala#14721

Surprisingly, caught two instances in the compiler code base itself.
@Kordyjan Kordyjan added this to the 3.1.3 milestone Aug 1, 2023
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.

3 participants