Skip to content

Commit

Permalink
Merge pull request #14738 from dotty-staging/fix-14721
Browse files Browse the repository at this point in the history
Warn on misleading indentation in single-case catch
  • Loading branch information
odersky authored Mar 23, 2022
2 parents a2cd2c5 + a7ffc8e commit e64fc49
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/BytecodeWriters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ trait BytecodeWriters {
catch case ex: ClosedByInterruptException =>
try
outfile.delete() // don't leave an empty or half-written classfile around after an interrupt
catch case _: Throwable =>
catch
case _: Throwable =>
throw ex
finally outstream.close()
report.informProgress("wrote '" + label + "' to " + outfile)
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/GenBCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ class GenBCodePipeline(val int: DottyBackendInterface, val primitives: DottyPrim
catch case ex: ClosedByInterruptException =>
try
outTastyFile.delete() // don't leave an empty or half-written tastyfile around after an interrupt
catch case _: Throwable =>
catch
case _: Throwable =>
throw ex
finally outstream.close()

Expand Down
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2617,7 +2617,14 @@ object Parsers {
(pattern(), guard())
}
CaseDef(pat, grd, atSpan(accept(ARROW)) {
if exprOnly then expr() else block()
if exprOnly then
if in.indentSyntax && in.isAfterLineEnd && in.token != INDENT then
warning(i"""Misleading indentation: this expression forms part of the preceding catch case.
|If this is intended, it should be indented for clarity.
|Otherwise, if the handler is intended to be empty, use a multi-line catch with
|an indented case.""")
expr()
else block()
})
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/repl/ShadowingTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object ShadowingTests:
val subdir = dir.resolve(name)
try Files.createDirectory(subdir)
catch case _: java.nio.file.FileAlreadyExistsException =>
assert(Files.isDirectory(subdir), s"failed to create shadowed subdirectory $subdir")
assert(Files.isDirectory(subdir), s"failed to create shadowed subdirectory $subdir")
subdir

// The directory on the classpath containing artifacts to be shadowed
Expand Down
8 changes: 8 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i14721.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class C:
def op: Unit = println("op")
def handler: Unit = println("handler")
def test: Unit =
try op
catch case _: NullPointerException =>
handler // error

0 comments on commit e64fc49

Please sign in to comment.