Scala Programming Language
  1. Scala Programming Language
  2. SI-5080

Implicit conversions are called three times undesirably

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.1
    • Fix Version/s: None
    • Component/s: Misc Compiler
    • Labels:

      Description

      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
      }
      

        Activity

        Hide
        Nate Nystrom added a comment - - edited

        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.

        Show
        Nate Nystrom added a comment - - edited 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.
        Hide
        Paul Phillips added a comment -

        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.

        Show
        Paul Phillips added a comment - 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.
        Hide
        Commit Message Bot added a comment -

        (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 SI-5080, no review.

        Show
        Commit Message Bot added a comment - (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 SI-5080 , no review.

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Anna Yudina
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development