Re: Expected warning for @volatile def

Adriaan Moors <[email protected]> Tue, 26 Jul 2016 15:20:58 -0700 (PDT)
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
------=_Part_6480_487050654.1469571658386
Content-Type: multipart/alternative; 
	boundary="----=_Part_6481_111709188.1469571658386"

------=_Part_6481_111709188.1469571658386
Content-Type: text/plain; charset=UTF-8

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 [email protected].
For more options, visit https://groups.google.com/d/optout.

------=_Part_6481_111709188.1469571658386
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Yes, I agree the compiler should warn more consistently ab=
out nonsensical use of @volatile.<div>I filed this at=C2=A0https://github.c=
om/scala/scala-dev/issues/190<br><br>On Friday, July 15, 2016 at 7:22:53 AM=
 UTC-7, Benjamin Darfler wrote:<blockquote class=3D"gmail_quote" style=3D"m=
argin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"=
><div dir=3D"ltr">Hello all,<div><br></div><div>I recently came across the =
following code in our codebase:</div><div><br></div><div><div style=3D"bord=
er:1px solid rgb(187,187,187);word-wrap:break-word;background-color:rgb(250=
,250,250)"><code><div><font color=3D"#660066"><span style=3D"color:#066">@v=
olatile</span><span style=3D"color:#000"> </span><span style=3D"color:#008"=
>def</span><span style=3D"color:#000"> doSomething </span><span style=3D"co=
lor:#660">=3D</span><span style=3D"color:#000"> </span><span style=3D"color=
:#660">{</span><span style=3D"color:#000"> </span><span style=3D"color:#800=
">/** do the thing */</span><span style=3D"color:#000"> </span><span style=
=3D"color:#660">}</span></font><span style=3D"color:#000"><br></span></div>=
</code></div><br>I was immediately=C2=A0suspicious and created the followin=
g test</div><div><br></div><div><div style=3D"border:1px solid rgb(187,187,=
187);word-wrap:break-word;background-color:rgb(250,250,250)"><code><div><p>=
<span><span style=3D"color:#008">class</span><span style=3D"color:#000"> </=
span><span style=3D"color:#606">Test</span><span style=3D"color:#000"> </sp=
an><span style=3D"color:#660">{</span></span></p><span style=3D"color:#000"=
><br></span><p><span><span style=3D"color:#000">=C2=A0 </span><span style=
=3D"color:#066">@volatile</span><span style=3D"color:#000"> </span><span st=
yle=3D"color:#008">private</span><span style=3D"color:#660">[</span><span s=
tyle=3D"color:#008">this</span><span style=3D"color:#660">]</span><span sty=
le=3D"color:#000"> </span><span style=3D"color:#008">var</span><span style=
=3D"color:#000"> something </span><span style=3D"color:#660">=3D</span><spa=
n style=3D"color:#000"> </span><span style=3D"color:#080">&quot;something&q=
uot;</span></span></p><span style=3D"color:#000"><br></span><p><span><span =
style=3D"color:#000">=C2=A0 </span><span style=3D"color:#066">@volatile</sp=
an><span style=3D"color:#000"> </span><span style=3D"color:#008">def</span>=
<span style=3D"color:#000"> doSomething </span><span style=3D"color:#660">=
=3D</span><span style=3D"color:#000"> </span><span style=3D"color:#660">{</=
span><span style=3D"color:#000"> println</span><span style=3D"color:#660">(=
</span><span style=3D"color:#000">something</span><span style=3D"color:#660=
">)</span><span style=3D"color:#000"> </span><span style=3D"color:#660">}</=
span></span></p><p><span style=3D"color:#000"><br></span><span></span></p><=
p><span><span style=3D"color:#660">}</span></span></p></div></code></div><b=
r></div><br>$ scalac test.scala <br><br>$ javap -p Test.class <br><br>Compi=
led from &quot;test.scala&quot; <br><br>public class Test { <br><br>private=
 volatile java.lang.String something; <br><br>public void doSomething(); <b=
r><br>public Test(); <br><br>}<div><br></div>As you can see the @volatile o=
n the var translates into a volatile variable in Java but the @volatile on =
the def is silently dropped.=C2=A0<div><br></div><div>This makes sense sinc=
e a @volatile def is nonsensical but I was surprised that there was no comp=
iler warning for this.<div><br></div><div>Should this be considered a bug?<=
/div></div><div><br></div><div>Ben Darfler</div></div></blockquote></div></=
div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">scal=
[email protected]</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

------=_Part_6481_111709188.1469571658386--

------=_Part_6480_487050654.1469571658386--