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

Incorrect warning when using an overloaded 'eq' method #6328

Closed
scabug opened this issue Sep 6, 2012 · 3 comments
Closed

Incorrect warning when using an overloaded 'eq' method #6328

scabug opened this issue Sep 6, 2012 · 3 comments

Comments

@scabug
Copy link

scabug commented Sep 6, 2012

The following test case produces a warning, but I do not believe it should:

scala> object Foo { def eq(x:Int) = x }
defined module Foo

scala> Foo.eq(1)
<console>:9: warning: comparing values of types object Foo and Int using `eq' will always yield false
              Foo.eq(1)
                    ^
res0: Int = 1
@scabug
Copy link
Author

scabug commented Sep 6, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6328?orig=1
Reporter: Jared Luxenberg (jluxenberg)
Duplicates #6091

@scabug
Copy link
Author

scabug commented Sep 6, 2012

Jared Luxenberg (jluxenberg) said:
The bug appears to be in RefChecks.scala:

...
    def checkSensible(pos: Position, fn: Tree, args: List[Tree]) = fn match {
      case Select(qual, name @ (nme.EQ | nme.NE | nme.eq | nme.ne)) if args.length == 1 =>
        def isReferenceOp = name == nme.eq || name == nme.ne

...

        // Whether def equals(other: Any) is overridden                                                                                                                                                                                     
        def isUsingDefaultEquals      = {
          val m = receiver.info.member(nme.equals_)
          (m == Object_equals) || (m == Any_equals)
        }
        // Whether this == or != is one of those defined in Any/AnyRef or an overload from elsewhere.                                                                                                                                       
        def isUsingDefaultScalaOp = {
          val s = fn.symbol
          (s == Object_==) || (s == Object_!=) || (s == Any_==) || (s == Any_!=)
        }
        // Whether the operands+operator represent a warnable combo (assuming anyrefs)                                                                                                                                                      
        def isWarnable           = isReferenceOp || (isUsingDefaultEquals && isUsingDefaultScalaOp)

note that isReferenceOp is "true" whenever the method name is "eq" or "ne". This code does not take into account whether the method has been overloaded. A check similar to the one used by "isUsingDefaultEquals" and "isUsingDefaultScalaOp" should be included for "eq" and "ne".

@scabug
Copy link
Author

scabug commented Sep 7, 2012

@retronym said:
More or less a duplicate of #6091.

Dealing with name, rather than symbols, is perilous when creating or pattern matching against trees.

@scabug scabug closed this as completed Sep 7, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant