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 4 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
19 changes: 16 additions & 3 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,14 @@ object Scanners {
if skipping then
if r.enclosing.isClosedByUndentAt(nextWidth) then
insert(OUTDENT, offset)
else if r.isInstanceOf[InBraces] && !closingRegionTokens.contains(token) then
report.warning("Line is indented too far to the left, or a `}` is missing", sourcePos())
else
val checkable = r match
case _: InBraces => true
case InParens(LPAREN, _) => true
case _ => false
if checkable && !closingRegionTokens.contains(token) then
report.warning(s"Line is indented too far to the left, or a ${showToken(r.closedBy)} is missing",
sourcePos())
else if lastWidth < nextWidth
|| lastWidth == nextWidth && (lastToken == MATCH || lastToken == CATCH) && token == CASE then
if canStartIndentTokens.contains(lastToken) then
Expand Down Expand Up @@ -747,7 +753,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
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
addParamssText(
addParamssText(keywordStr("extension "), leadingParamss)
~~ (defKeyword ~~ valDefText(nameIdText(tree))).close,
trailingParamss)
trailingParamss)
else
addParamssText(defKeyword ~~ valDefText(nameIdText(tree)), tree.paramss)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CompletionExtensionSuite extends BaseCompletionSuite:

@Test def `filter-by-type-old` =
check(
"""|package example
"""|package example
|
|object enrichments:
| implicit class A(num: Int):
Expand All @@ -99,7 +99,6 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
"""|identity: String (implicit)
|""".stripMargin, // identity2 won't be available
filter = _.contains("(implicit)")

)

@Test def `filter-by-type-subtype` =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
| **/
|}
|""".stripMargin,
"",
"",
includeCommitCharacter = true
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ class CompletionSuite extends BaseCompletionSuite:
| val t: TT@@
|}
|""".stripMargin,
"TTT[A <: Int] = List[A]"
"TTT[A <: Int] = List[A]"
)

@Test def `type-lambda` =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ class HoverDocSuite extends BaseHoverSuite:

@Test def `doc` =
check(
"""object a {
| <<java.util.Collections.empty@@List[Int]>>
|}
|""".stripMargin,
"""|**Expression type**:
|```scala
|java.util.List[Int]
|```
|**Symbol signature**:
|```scala
|final def emptyList[T](): java.util.List[T]
|```
|Found documentation for java/util/Collections#emptyList().
|""".stripMargin,
"""|object a {
| <<java.util.Collections.empty@@List[Int]>>
|}
|""".stripMargin,
"""|**Expression type**:
|```scala
|java.util.List[Int]
|```
|**Symbol signature**:
|```scala
|final def emptyList[T](): java.util.List[T]
|```
|Found documentation for java/util/Collections#emptyList().
|""".stripMargin,
)

@Test def `doc-parent` =
Expand All @@ -47,7 +47,6 @@ class HoverDocSuite extends BaseHoverSuite:
|```
|Found documentation for scala/collection/LinearSeqOps#headOption().
|""".stripMargin,

)

@Test def `java-method` =
Expand All @@ -61,7 +60,7 @@ class HoverDocSuite extends BaseHoverSuite:
|```
|Found documentation for java/lang/String#substring().
|""".stripMargin
)
)

@Test def `object` =
check(
Expand Down
20 changes: 10 additions & 10 deletions scaladoc/test/dotty/tools/scaladoc/site/TemplateFileTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,26 @@ class TemplateFileTests:
testTemplate(
"""# Hello {{ msg }}!""",
ext = "md"
) { t =>
): t =>
assertEquals(
"""<section id="hello-there">
|<h1 class="h500"><a href="#hello-there" class="anchor"></a>Hello there!</h1>
|</section>""".stripMargin,
t.resolveInner(RenderingContext(Map("msg" -> "there"))).code.trim())
}
|<h1 class="h500"><a href="#hello-there" class="anchor"></a>Hello there!</h1>
|</section>""".stripMargin,
t.resolveInner(RenderingContext(Map("msg" -> "there"))).code.trim()
)

@Test
def mixedTemplates() : Unit =
testTemplate(
"""# Hello {{ msg }}!""",
ext = "md"
) { t =>
): t =>
assertEquals(
"""<section id="hello-there2">
|<h1 class="h500"><a href="#hello-there2" class="anchor"></a>Hello there2!</h1>
|</section>""".stripMargin,
t.resolveInner(RenderingContext(Map("msg" -> "there2"))).code.trim())
}
|<h1 class="h500"><a href="#hello-there2" class="anchor"></a>Hello there2!</h1>
|</section>""".stripMargin,
t.resolveInner(RenderingContext(Map("msg" -> "there2"))).code.trim()
)

@Test
def htmlOnly(): Unit =
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
)
6 changes: 2 additions & 4 deletions tests/neg/syntax-error-recovery.check
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,5 @@
| longer explanation available when compiling with `-explain`
-- Warning: tests/neg/syntax-error-recovery.scala:61:2 -----------------------------------------------------------------
61 | println(bam)
| ^^^^^^^
| Alphanumeric method println is not declared infix; it should not be used as infix operator.
| Instead, use method syntax .println(...) or backticked identifier `println`.
| The latter can be rewritten automatically under -rewrite -source 3.4-migration.
| ^
| Line is indented too far to the left, or a ')' is missing
77 changes: 77 additions & 0 deletions tests/pos/i22527.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

//> using options -Werror

import annotation.*

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")

@nowarn("msg=Unit")
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,
)
)

object `arrow eol`:
def f(g: Int => Int, i: Int): Int = g(i)
def g(map: Int => Int): Int => Int = map
def test =
f(
g: x =>
x + 1, 42
)
22 changes: 22 additions & 0 deletions tests/warn/i22527.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Warning: tests/warn/i22527.scala:60:10 ------------------------------------------------------------------------------
60 | y, // warn
| ^
| Line is indented too far to the left, or a ')' is missing
-- Warning: tests/warn/i22527.scala:61:10 ------------------------------------------------------------------------------
61 | z, // warn
| ^
| Line is indented too far to the left, or a ')' is missing
-- Warning: tests/warn/i22527.scala:72:2 -------------------------------------------------------------------------------
72 | 42 // warn
| ^
| Line is indented too far to the left, or a ')' is missing
-- Warning: tests/warn/i22527.scala:79:0 -------------------------------------------------------------------------------
79 |k: // warn
|^
|Line is indented too far to the left, or a ')' is missing
-- [E190] Potential Issue Warning: tests/warn/i22527.scala:40:6 --------------------------------------------------------
40 | true, "fail") // warn value discard
| ^^^^
| Discarded non-Unit value of type Boolean. Add `: Unit` to discard silently.
|
| longer explanation available when compiling with `-explain`
81 changes: 81 additions & 0 deletions tests/warn/i22527.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

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, // warn
z, // warn
)
)

object `arrow eol`:
def f(g: Int => Int, i: Int): Int = g(i)
def g(map: Int => Int): Int => Int = map
def k(i: => Int) = i
def test =
f(
g(_ + 1),
42 // warn
)
def test2 =
f(
g:
x =>
x + 1,
k: // warn
42
)
Loading