Skip to content

Conversation

@liufengyun
Copy link
Contributor

@liufengyun liufengyun commented Apr 5, 2018

Fix #4225

  • always do reachability check
  • handle null in patterns

Note that we only handle null in reachability check, while ignore null in exhaustivity check. Otherwise, the following pattern match will report a warning about null, which is annoying:

    (Some(3): Option[Int]) match {
       case Some(x) =>
       case None    =>
    }

@liufengyun liufengyun requested a review from smarter April 5, 2018 14:17
Text()

nodeName ~ "(" ~ elems ~ tpSuffix ~ ")" ~ (Str(node.pos.toString) provided ctx.settings.YprintPos.value)
case _ =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tree extends Positioned so we don't even need a pattern match at all here.

""".stripMargin
ctx.error(msg, pos)
None
case ex: Throwable => throw ex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me, a try/catch can always catch a Throwable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we could keep it, and no warning will be reported. I removed it because the compiler will generate a default case anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Typ(ConstantType(c), false)
case _: BackquotedIdent => Typ(pat.tpe, false)
case Ident(nme.WILDCARD) =>
Or(Typ(pat.tpe.stripAnnots, false) :: Typ(ConstantType(Constant(null))) :: Nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be cleaner to have val nullType = ConstantType(Constant(null)) somewhere.

def test(x: String) =
x match {
case Bar(a) => a
case _ => x // this case is reachable, i.e. test(null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion; maybe if the only way to reach a case is through null, we should suggest the user writes case null instead of case _ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea, it's implemented in the latest commit.

/** Is `tp1` a subtype of `tp2`? */
def isSubType(tp1: Type, tp2: Type): Boolean = {
val res = tp1 <:< tp2 && (tp1 != ConstantType(Constant(null)) || tp2 == ConstantType(Constant(null)))
val res = tp1 <:< tp2 && (tp1 != nullType || tp2 == nullType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the nullType checks could be moved before the subtype checks since they're cheaper.

// if last case is `_` and only matches `null`, produce a warning
if (i == cases.length - 1) {
simplify(minus(covered, prevs)) match {
case Typ(ConstantType(Constant(null)), _) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use `nullType` here

@liufengyun
Copy link
Contributor Author

test performance please

@dottybot
Copy link
Member

dottybot commented Apr 5, 2018

performance test scheduled: 2 job(s) in queue, 1 running.

case class MatchCaseUnreachable()(implicit ctx: Context)
extends Message(MatchCaseUnreachableID) {
val kind = s"""Match ${hl"case"} Unreachable"""
val kind = s"""Match case Unreachable"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interpolator not needed


case class MatchCaseOnlyNullWarning()(implicit ctx: Context)
extends Message(MatchCaseOnlyNullWarningID) {
val kind = s"""Only null matched"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interpolator not needed

case class MatchCaseOnlyNullWarning()(implicit ctx: Context)
extends Message(MatchCaseOnlyNullWarningID) {
val kind = s"""Only null matched"""
val msg = s"Only ${hl"null"} is matched. Consider use `case null =>` instead."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/use/using

@dottybot
Copy link
Member

dottybot commented Apr 5, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/4253/ to see the changes.

Benchmarks is based on merging with master (b1d9162)

Note that we only handle null in reachability check,
while ignore null in exhaustivity check. Otherwise,
the following pattern match will report a warning
about `null`, which is annoying:

    (Some(3): Option[Int]) match {
       case Some(x) =>
       case None    =>
    }
@liufengyun
Copy link
Contributor Author

liufengyun commented Apr 6, 2018

The performance regression on exhaustivity S and exhaustivity T is incurred by reporting a friendly warning about replacing case _ with case null, which requires subtraction of spaces (minus more expensive than isSubSpace).

exhaustivity V doesn't suffer from the problem, as the subtraction finishes with Empty quickly, as there are no overlapping between the cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants