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

Restrict outdent in parens for certain region prefixes #22530

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,14 @@ object Scanners {
case _ => false
currentRegion match
case r: Indented if isEnclosedInParens(r.outer) =>
insert(OUTDENT, offset)
// For region prefix COLONeol, only OUTDENT if COMMA at EOL
if r.prefix == COLONeol then
val lookahead = LookaheadScanner()
lookahead.nextToken()
if lookahead.isAfterLineEnd then
insert(OUTDENT, offset)
else
insert(OUTDENT, offset)
case _ =>
peekAhead()
if isAfterLineEnd
Expand Down
25 changes: 25 additions & 0 deletions tests/neg/i22527.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

//rule of thumb is COLONeol was at EOL, so COMMA must be at EOL
def test: Unit =
assert(
identity:
true, "ok" // error end of statement expected but ',' found
)

def callme[A](x: => A, msg: String) = try x.toString catch case t: RuntimeException => msg

// not all indented regions require COMMA at EOL for OUTDENT
def orElse(x: Int): Unit =
callme(
if x > 0 then
class X extends AnyRef, Serializable // error Not found: Serializable - did you mean Specializable?
true // error ',' or ')' expected, but 'true' found
else
false, "fail")

def g: Unit =
identity(
x =
class X extends AnyRef, Serializable // error
27 // error
)
63 changes: 63 additions & 0 deletions tests/pos/i22527.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

def f: Unit =
identity(
identity:
class X extends AnyRef, Serializable
42
)

def test: Unit =
assert(
identity:
true,
"ok"
)

def toss: Unit =
assert(
throw
null,
"ok"
)
def raise: Unit =
assert(
throw
null, "ok" // ok now
)

def callme[A](x: => A, msg: String) = try x.toString catch case t: RuntimeException => msg

def orElse(x: Int): Unit =
callme(
if x > 0 then
true
else
false, "fail")

def onlyIf(x: Int): Unit =
callme(
if (x > 0)
true, "fail") // warn value discard

def h(xs: List[Int]) =
xs.foldLeft(0)
(
(acc, x) =>
acc
+ x,
)

def sum(x: Int, y: Int, z: Int) = x+y+z

def k(xs: List[Int], y: Int, z: Int) =
xs.foldLeft(0)
(
(acc, x) =>
sum(
x
+ y
+ z,
y,
z,
)
)