Skip to content

Commit e8228d7

Browse files
committed
Fix scala#1665: Check that != has an operand on the left.
1 parent 5cef7a9 commit e8228d7

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/dotty/tools/dotc/transform/InterceptedMethods.scala

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,31 @@ class InterceptedMethods extends MiniPhaseTransform {
9696
s"that means the intercepted methods set doesn't match the code")
9797
tree
9898
}
99-
lazy val Select(qual, _) = tree.fun
99+
lazy val qual = tree.fun match {
100+
case Select(qual, _) => qual
101+
case ident @ Ident(_) =>
102+
ident.tpe match {
103+
case TermRef(prefix: TermRef, name) =>
104+
tpd.ref(prefix)
105+
case TermRef(prefix: ThisType, name) =>
106+
tpd.This(prefix.cls)
107+
}
108+
109+
}
100110
val Any_## = this.Any_##
101111
val Any_!= = defn.Any_!=
102112
val rewrite: Tree = tree.fun.symbol match {
103113
case Any_## =>
104114
poundPoundValue(qual)
105115
case Any_!= =>
106-
qual.select(defn.Any_==).appliedToArgs(tree.args).select(defn.Boolean_!)
116+
assert(tree.args.size == 1)
117+
val lhs = qual.tpe
118+
val rhs = tree.args.head.tpe
119+
if (!(lhs <:< rhs || rhs <:< lhs) && rhs != defn.NullType && lhs != defn.NullType) {
120+
ctx.warning(ex"comparing values of types ${lhs.widenDealias} and ${rhs.widenDealias} using `!=' will always yield true",
121+
tree.pos)
122+
}
123+
qual.select(defn.Any_==).appliedToArgs(tree.args).select(defn.Boolean_!)
107124
/*
108125
/* else if (isPrimitiveValueClass(qual.tpe.typeSymbol)) {
109126
// todo: this is needed to support value classes

tests/pos/i1665-nonfuzzy.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
object Test {
3+
!=(1) // warning: comparing values of types Test and Int using `!=' will always yield true
4+
!=("abc") // warning: comparing values of types Test and String using `!=' will always yield true
5+
1 != this // warning: comparing values of types Int and Test using `!=' will always yield true
6+
!=(this)
7+
}

tests/pos/i1665.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
import scala.reflect.runtime.universe._
3+
import scala.reflect.runtime.{currentMirror => cm}
4+
5+
object Test extends App {
6+
val mutant = new { val x = 2 }
7+
val c = cm.classSymbol(mutant.getClass)
8+
!=(c) // warning
9+
println(c.fullName)
10+
c.info.toString.lines
11+
.filter(_ != " private var bitmap$init$0: Boolean") foreach println
12+
}

0 commit comments

Comments
 (0)