Skip to content

Improve Unit ascription escape hatch #23147

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,19 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
case _ => false
}

/** Expression was written `e: Unit` to quell warnings. Looks into adapted tree. */
def isAscribedToUnit(tree: Tree): Boolean =
import typer.Typer.AscribedToUnit
tree.hasAttachment(AscribedToUnit)
|| {
def loop(tree: Tree): Boolean = tree match
case Apply(fn, _) => fn.hasAttachment(AscribedToUnit) || loop(fn)
case TypeApply(fn, _) => fn.hasAttachment(AscribedToUnit) || loop(fn)
case Block(_, expr) => expr.hasAttachment(AscribedToUnit) || loop(expr)
case _ => false
loop(tree)
}

/** Does this CaseDef catch Throwable? */
def catchesThrowable(cdef: CaseDef)(using Context): Boolean =
catchesAllOf(cdef, defn.ThrowableType)
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
val checkedType = checkNotShadowed(ownType)
val tree1 = checkedType match
case checkedType: NamedType if !prefixIsElidable(checkedType) =>
ref(checkedType).withSpan(tree.span)
ref(checkedType).withSpan(tree.span).withAttachmentsFrom(tree)
case _ =>
def isScalaModuleRef = checkedType match
case moduleRef: TypeRef if moduleRef.symbol.is(ModuleClass, butNot = JavaDefined) => true
Expand Down Expand Up @@ -4695,7 +4695,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
&& !ctx.isAfterTyper
&& !tree.isInstanceOf[Inlined]
&& !isThisTypeResult(tree)
&& !tree.hasAttachment(AscribedToUnit) then
&& !isAscribedToUnit(tree)
then
report.warning(ValueDiscarding(tree.tpe), tree.srcPos)

return tpd.Block(tree1 :: Nil, unitLiteral)
Expand Down
8 changes: 4 additions & 4 deletions tests/warn/warn-value-discard.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
27 | mutable.Set.empty[String].remove("") // warn
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| discarded non-Unit value of type Boolean. Add `: Unit` to discard silently.
-- [E175] Potential Issue Warning: tests/warn/warn-value-discard.scala:39:41 -------------------------------------------
39 | mutable.Set.empty[String].subtractOne("") // warn
-- [E175] Potential Issue Warning: tests/warn/warn-value-discard.scala:37:41 -------------------------------------------
37 | mutable.Set.empty[String].subtractOne("") // warn
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| discarded non-Unit value of type scala.collection.mutable.Set[String]. Add `: Unit` to discard silently.
-- [E175] Potential Issue Warning: tests/warn/warn-value-discard.scala:59:4 --------------------------------------------
59 | mutable.Set.empty[String] += "" // warn
-- [E175] Potential Issue Warning: tests/warn/warn-value-discard.scala:57:4 --------------------------------------------
57 | mutable.Set.empty[String] += "" // warn
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| discarded non-Unit value of type scala.collection.mutable.Set[String]. Add `: Unit` to discard silently.
-- [E175] Potential Issue Warning: tests/warn/warn-value-discard.scala:15:35 -------------------------------------------
Expand Down
43 changes: 37 additions & 6 deletions tests/warn/warn-value-discard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ class ValueDiscardTest:
// --> Warning
mutable.Set.empty[String].remove("") // warn

// TODO IMHO we don't need to support this,
// as it's just as easy to add a @nowarn annotation as a Unit ascription
//def removeAscribed(): Unit = {
// mutable.Set.empty[String].remove(""): Unit // nowarn
//}
def removeAscribed(): Unit = {
mutable.Set.empty[String].remove(""): Unit // nowarn
}

def subtract(): Unit =
// - Set#subtractOne returns this.type
Expand Down Expand Up @@ -63,4 +61,37 @@ class ValueDiscardTest:
// - receiver is a local variable
// --> No warning
val s: mutable.Set[String] = mutable.Set.empty[String]
s += ""
s += ""

// see also tests/warn/21557.scala
class UnitAscription:
import scala.concurrent.*, ExecutionContext.Implicits.given

case class C(c: Int):
def f(i: Int, j: Int = c) = i + j

def f(i: Int, j: Int = 27) = i + j

def g[A]: List[A] = Nil

def i: Int = 42

def `default arg is inline`: Unit =
f(i = 42): Unit // nowarn

def `default arg requires block`: Unit =
C(27).f(i = 42): Unit // nowarn

def `application requires implicit arg`: Unit =
Future(42): Unit // nowarn

def `application requires inferred type arg`: Unit =
g: Unit // nowarn

def `implicit selection from this`: Unit =
i: Unit // nowarn

object UnitAscription:
def g[A]: List[A] = Nil
def `application requires inferred type arg`: Unit =
g: Unit // nowarn UnitAscription.g
Loading