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 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

This comment has been minimized.

Copy link
Author

@scabug 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

This comment has been minimized.

Copy link
Author

@scabug 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

This comment has been minimized.

Copy link
Author

@scabug 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

This comment has been minimized.

Copy link
Author

@scabug 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.

@scabug scabug closed this Oct 16, 2011
@scabug scabug added the implicit label Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.