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

Allow suppressing value-discard warning via type ascription to Unit #7563

Merged
merged 2 commits into from
Jan 9, 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
3 changes: 3 additions & 0 deletions src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ trait StdAttachments {
*/
case class OriginalTreeAttachment(original: Tree)

/** Marks a Typed tree with Unit tpt. */
case object TypedExpectingUnitAttachment

case class StabilizingDefinitions(vdefs: List[ValDef])
private[this] val StabilizingDefinitionsTag: reflect.ClassTag[StabilizingDefinitions] = reflect.classTag[StabilizingDefinitions]

Expand Down
11 changes: 8 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
// requiring both the ACCESSOR and the SYNTHETIC bits to trigger the exemption
private def isSyntheticAccessor(sym: Symbol) = sym.isAccessor && (!sym.isLazy || isPastTyper)

private def explicitlyUnit(tree: Tree): Boolean = tree.hasAttachment[TypedExpectingUnitAttachment.type]

// when type checking during erasure, generate erased types in spots that aren't transformed by erasure
// (it erases in TypeTrees, but not in, e.g., the type a Function node)
def phasedAppliedType(sym: Symbol, args: List[Type]) = {
Expand Down Expand Up @@ -1057,7 +1059,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
case (Apply(Select(receiver, _), _), SingleType(_, sym)) => sym == receiver.symbol
case _ => false
}
if (!isThisTypeResult) context.warning(tree.pos, "discarded non-Unit value")
if (!isThisTypeResult && !explicitlyUnit(tree)) context.warning(tree.pos, "discarded non-Unit value")
}
@inline def warnNumericWiden(): Unit =
if (!isPastTyper && settings.warnNumericWiden) context.warning(tree.pos, "implicit numeric widening")
Expand Down Expand Up @@ -2446,7 +2448,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
case _ => (1, EmptyTree, false)
}
def checkPure(t: Tree, supple: Boolean): Unit =
if (treeInfo.isPureExprForWarningPurposes(t)) {
if (!explicitlyUnit(t) && treeInfo.isPureExprForWarningPurposes(t)) {
val msg = "a pure expression does nothing in statement position"
val parens = if (statsTyped.length + count > 1) "multiline expressions might require enclosing parentheses" else ""
val discard = if (adapted) "; a value can be silently discarded when Unit is expected" else ""
Expand Down Expand Up @@ -5442,7 +5444,10 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
}
case Typed(expr, tpt) =>
val tpt1 = typedType(tpt, mode) // type the ascribed type first
val expr1 = typed(expr, mode.onlySticky, tpt1.tpe.deconst) // then type the expression with tpt1 as the expected type
val exprWithAttachment =
if (definitions.isUnitType(tpt1.tpe)) expr.updateAttachment(TypedExpectingUnitAttachment)
else expr
val expr1 = typed(exprWithAttachment, mode.onlySticky, tpt1.tpe.deconst) // then type the expression with tpt1 as the expected type
treeCopy.Typed(tree, expr1, tpt1) setType tpt1.tpe
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/reflect/scala/reflect/api/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ trait Types {
def sym: Symbol
}
/** The `SuperType` type is not directly written, but arises when `C.super` is used
* as a prefix in a `TypeRef` or `SingleType`. It's internal presentation is
* as a prefix in a `TypeRef` or `SingleType`. Its internal presentation is
* {{{
* SuperType(thistpe, supertpe)
* }}}
Expand All @@ -514,7 +514,7 @@ trait Types {
*/
val SuperType: SuperTypeExtractor

/** An extractor class to create and pattern match with syntax `SingleType(thistpe, supertpe)`
/** An extractor class to create and pattern match with syntax `SuperType(thistpe, supertpe)`
* @group Extractors
*/
abstract class SuperTypeExtractor {
Expand Down
8 changes: 1 addition & 7 deletions test/files/neg/t9847.check
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ t9847.scala:6: warning: discarded non-Unit value
t9847.scala:6: warning: a pure expression does nothing in statement position
def f(): Unit = 42
^
t9847.scala:7: warning: discarded non-Unit value
def g = (42: Unit)
^
t9847.scala:7: warning: a pure expression does nothing in statement position
def g = (42: Unit)
^
t9847.scala:9: warning: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses
1
^
Expand Down Expand Up @@ -41,5 +35,5 @@ t9847.scala:24: warning: a pure expression does nothing in statement position; m
class D { 42 ; 17 }
^
error: No warnings can be incurred under -Xfatal-warnings.
14 warnings found
12 warnings found
one error found
12 changes: 12 additions & 0 deletions test/files/neg/value-discard.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
value-discard.scala:6: warning: discarded non-Unit value
mutable.Set[String]().remove("") // expected to warn
^
value-discard.scala:13: warning: discarded non-Unit value
def subtract(): Unit = mutable.Set.empty[String].subtractOne("") // warn
^
value-discard.scala:17: warning: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses
"" // warn
^
error: No warnings can be incurred under -Xfatal-warnings.
three warnings found
one error found
21 changes: 21 additions & 0 deletions test/files/neg/value-discard.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// scalac: -Ywarn-value-discard -Xfatal-warnings
final class UnusedTest {
import scala.collection.mutable

def remove(): Unit = {
mutable.Set[String]().remove("") // expected to warn
}

def removeAscribed(): Unit = {
mutable.Set[String]().remove(""): Unit // no warn
}

def subtract(): Unit = mutable.Set.empty[String].subtractOne("") // warn

def warnings(): Unit = {
val s: mutable.Set[String] = mutable.Set.empty[String]
"" // warn
"": Unit // no warn
s.subtractOne("") // no warn
}
}