Expected warning for @volatile def

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Expected warning for @volatile def

Benjamin Darfler-2
Hello all,

I recently came across the following code in our codebase:

@volatile def doSomething = { /** do the thing */ }

I was immediately suspicious and created the following test

class Test {


  @volatile private[this] var something = "something"


  @volatile def doSomething = { println(something) }


}



$ scalac test.scala

$ javap -p Test.class

Compiled from "test.scala"

public class Test {

private volatile java.lang.String something;

public void doSomething();

public Test();

}

As you can see the @volatile on the var translates into a volatile variable in Java but the @volatile on the def is silently dropped. 

This makes sense since a @volatile def is nonsensical but I was surprised that there was no compiler warning for this.

Should this be considered a bug?

Ben Darfler

--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Expected warning for @volatile def

Adriaan Moors-3
Yes, I agree the compiler should warn more consistently about nonsensical use of @volatile.
I filed this at https://github.com/scala/scala-dev/issues/190

On Friday, July 15, 2016 at 7:22:53 AM UTC-7, Benjamin Darfler wrote:
Hello all,

I recently came across the following code in our codebase:

@volatile def doSomething = { /** do the thing */ }

I was immediately suspicious and created the following test

class Test {


  @volatile private[this] var something = "something"


  @volatile def doSomething = { println(something) }


}



$ scalac test.scala

$ javap -p Test.class

Compiled from "test.scala"

public class Test {

private volatile java.lang.String something;

public void doSomething();

public Test();

}

As you can see the @volatile on the var translates into a volatile variable in Java but the @volatile on the def is silently dropped. 

This makes sense since a @volatile def is nonsensical but I was surprised that there was no compiler warning for this.

Should this be considered a bug?

Ben Darfler

--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.