Skip to content

Fix #5013: Check statement purity for adapted expressions of Unit #5707

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

Merged
merged 4 commits into from
Jan 15, 2019
Merged
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
16 changes: 11 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2064,9 +2064,7 @@ class Typer extends Namer
traverse(stats ++ rest)
case stat :: rest =>
val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
if (!ctx.isAfterTyper && isPureExpr(stat1) &&
!stat1.tpe.isRef(defn.UnitClass) && !isSelfOrSuperConstrCall(stat1))
ctx.warning(PureExpressionInStatementPosition(stat, exprOwner), stat.pos)
checkStatementPurity(stat1)(stat, exprOwner)
buf += stat1
traverse(rest)
case nil =>
Expand Down Expand Up @@ -2607,10 +2605,13 @@ class Typer extends Namer
val folded = ConstFold(tree, pt)
if (folded ne tree) return adaptConstant(folded, folded.tpe.asInstanceOf[ConstantType])
// drop type if prototype is Unit
if (pt isRef defn.UnitClass)
if (pt isRef defn.UnitClass) {
// local adaptation makes sure every adapted tree conforms to its pt
// so will take the code path that decides on inlining
return tpd.Block(adapt(tree, WildcardType, locked) :: Nil, Literal(Constant(())))
val tree1 = adapt(tree, WildcardType, locked)
checkStatementPurity(tree1)(tree, ctx.owner)
return tpd.Block(tree1 :: Nil, Literal(Constant(())))
}
// convert function literal to SAM closure
tree match {
case closure(Nil, id @ Ident(nme.ANON_FUN), _)
Expand Down Expand Up @@ -2770,4 +2771,9 @@ class Typer extends Namer
case _ =>
}
}

private def checkStatementPurity(tree: tpd.Tree)(original: untpd.Tree, exprOwner: Symbol)(implicit ctx: Context): Unit = {
if (!ctx.isAfterTyper && isPureExpr(tree) && !tree.tpe.isRef(defn.UnitClass) && !isSelfOrSuperConstrCall(tree))
ctx.warning(PureExpressionInStatementPosition(original, exprOwner), original.pos)
}
}
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/io/ZipArchive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ final class FileZipArchive(jpath: JPath) extends ZipArchive(jpath) {
while (entries.hasMoreElements) {
val zipEntry = entries.nextElement
val dir = getDir(dirs, zipEntry)
if (zipEntry.isDirectory) dir
else {
if (!zipEntry.isDirectory) {
val f =
if (ZipArchive.closeZipFile)
new LazyEntry(
Expand Down
9 changes: 9 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i5013.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo {

def foo1: Unit = 2 // error: A pure expression does nothing in statement position

def foo2: Unit = {
3 // error: A pure expression does nothing in statement position
4 // error: A pure expression does nothing in statement position
}
}
16 changes: 16 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i5013b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Foo {

val a: Int = 3

def b: 1 = {
println("1")
1
}

val c: Unit = ()

def foo1: Unit = a // error: A pure expression does nothing in statement position
def foo2: Unit = b
def foo3: Unit = c // Not addapted to { c; () } and hence c is not a statement

}
2 changes: 1 addition & 1 deletion tests/patmat/i4226.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Empty {
object Test {
val a: Maybe[Int] = Just(2)
def main(args: Array[String]): Unit = a match {
case Just(2) => true
case Just(2) =>
case Empty() =>
}
}
2 changes: 1 addition & 1 deletion tests/patmat/i4226b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Empty {
object Test {
val a: Maybe[Int] = Just(2)
def main(args: Array[String]): Unit = a match {
case Just(2) => true
case Just(2) =>
case Empty() =>
}
}