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

Implicit conversions are called three times undesirably #5080

Closed
scabug opened this issue Oct 16, 2011 · 4 comments
Closed

Implicit conversions are called three times undesirably #5080

scabug opened this issue Oct 16, 2011 · 4 comments
Assignees
Labels

Comments

@scabug
Copy link

scabug commented Oct 16, 2011

For example execution of the following code will give you "hey" printed out three times instead of one.

// BugImplicit.scala
object BugImplicit extends App {

  abstract class Value {
  }

  case class Num(value: Int) extends Value {
    override def toString = value.toString;
  }

  implicit def conversions(x: Value) = new {
    def toInt =
      x match {
        case Num(n) => n
        case _ => throw new RuntimeException
      }
  }

  def eval(v: Value): Value = {
    print("hey")
    Num(1)
  }

  eval(Num(1)).toInt
}
@scabug
Copy link
Author

scabug commented Oct 16, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5080?orig=1
Reporter: Anna Yudina (annyu)
Affected Versions: 2.9.1

@scabug
Copy link
Author

scabug commented Oct 16, 2011

Nate Nystrom (nystrom) said (edited on Oct 16, 2011 3:25:24 PM UTC):
It looks like the bug is with compilation of calls to methods named toInt. The body of the App is duplicated to perform two isInstanceOf checks vs. java.lang.Number and java.lang.Character. Changing the name of the toInt method in the code above makes the problem go away.

@scabug
Copy link
Author

scabug commented Oct 16, 2011

@paulp said:
You're right, the selection is duplicated during cleanup for boxing logic incorrectly not accounting for the fact that it may be a side-effecting expression.

@scabug
Copy link
Author

scabug commented Oct 16, 2011

Commit Message Bot (anonymous) said:
(extempore in r25838) Fix for multiple evaluation in structural calls.

An interesting bug during cleanup: runtime checks on the target of a
structural invocation duplicated the selection without regard for the
fact that it might be an expression. So if the name of the method being
invoked allowed the possibility that the target was a primitive type
(such as "toInt") the expression would be evaluated three times.

Closes #5080, no review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants