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

Compiler bug for updateDynamic invocations within expressions #5733

Closed
scabug opened this issue May 2, 2012 · 6 comments
Closed

Compiler bug for updateDynamic invocations within expressions #5733

scabug opened this issue May 2, 2012 · 6 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

@scabug scabug commented May 2, 2012

Problem with updateDynamic:
\
If a dynamic field assignment is invoked within an expression (the type of the assignment invocation depends on the result type of method updateDynamic), then the compiler reports the error reassignment to val.

scala> val foo = new A // A implements selectDynamic and updateDynamic
foo: A = A@71933d6c

scala> foo.xxx = 3
foo.xxx: Any = 3

scala> foo.xxx
res0: Any = 3

scala> println(foo.xxx = 3)
<console>:12: error: reassignment to val
              println(foo.xxx = 3)
                              ^

scala> val x = (foo.xxx = 3) == ()
<console>:11: error: reassignment to val
       val x = (foo.xxx = 3) == ()
                        ^
@scabug
Copy link
Author

@scabug scabug commented May 2, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5733?orig=1
Reporter: @dgruntz
Affected Versions: 2.10.0-M3

Loading

@scabug
Copy link
Author

@scabug scabug commented May 2, 2012

@dgruntz said:
Actually, the latter assignments are not translated by the compiler to invocations of updateDynamic. If we look at the tree after typers, it looks as follows:

    private[this] val foo: A = new A();
    Test.this.foo.updateDynamic("xxx")(3);
    scala.this.Predef.println(foo.xxx = 3);
    private[this] val x: <error> = foo.xxx = 3.==()

Loading

@scabug
Copy link
Author

@scabug scabug commented Sep 10, 2012

@harrah said:
Can you provide an implementation of A that demonstrates this issue?

Loading

@scabug
Copy link
Author

@scabug scabug commented Oct 13, 2012

Alexis Agahi (alag) said (edited on Oct 14, 2012 1:04:21 PM UTC):
Can't figure out why updateDynamic works in App and wont compile the test class...

// src/AB.scala
import language.dynamics

object A extends Dynamic {
  var a = "a"

  def selectDynamic(method:String): String = a

  def updateDynamic(method:String)(v:String) { a = v }
}

class B extends Dynamic {
  var b = "b"

  def selectDynamic(method:String): String = b

  def updateDynamic(method:String)(v:String) { b = v }
}

object TestApp extends App {
  assert( A.foo == "a" )
  assert( A.bar == "a" )
  A.aaa = "aaa"
  assert( A.bar == "aaa" )


  val b = new B
  assert( b.foo == "b" )
  assert( b.bar == "b" )
  b.bbb = "bbb"
  assert( b.bar == "bbb" )

  println("Done")
}
// test/ABTest.scala
import org.junit.Test
import language.dynamics

class ABTest {

  @Test
  def test() {
    assert( A.foo == "a" )
    assert( A.bar == "a" )
    A.aaa = "aaa"
    assert( A.bar == "aaa" )


    val b = new B
    assert( b.foo == "b" )
    assert( b.bar == "b" )
    b.bbb = "bbb"
    assert( b.bar == "bbb" )

  }

}
// build.sbt
scalaVersion := "2.10.0-RC1"

scalaSource in Compile <<= baseDirectory(_ / "src")

scalaSource in Test <<= baseDirectory(_ / "test")

libraryDependencies += "com.novocode" % "junit-interface" % "0.10-M1" % "test"

libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.10.0-RC1"

libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.10.0-RC1"
> scala-version
[info] 2.10.0-RC1
> sbt-version
[info] 0.12.1
> test
[info] Compiling 1 Scala source to /2.10/target/scala-2.10/test-classes...
[error] /2.10/test/ABTest.scala:24: reassignment to val
[error]     A.aaa = "aaa"
[error]           ^
[error] /2.10/test/DynamicTest.scala:31: reassignment to val
[error]     b.bbb = "bbb"
[error]           ^
[error] two errors found
[error] (test:compile) Compilation failed
> clean
[success] Total time: 0 s, completed 14 oct. 2012 00:18:51
> run
[info] Updating {file:/2.10/}default-5d6b7a...
[info] Resolving org.scala-tools.testing#test-interface;0.5 ...
[info] Done updating.
[info] Compiling 1 Scala sources to /2.10/target/scala-2.10/classes...
[info] Running TestApp 
Done
[success] Total time: 17 s, completed 14 oct. 2012 00:19:09

Loading

@scabug
Copy link
Author

@scabug scabug commented Nov 28, 2012

Loading

@scabug
Copy link
Author

@scabug scabug commented Dec 5, 2012

Loading

@scabug scabug closed this Dec 5, 2012
@scabug scabug added the dynamic label Apr 7, 2017
@scabug scabug added this to the 2.10.0 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants