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
Allow using classOf with object type #9279
Conversation
(iirc there is a passage in the spec that will need to be updated) |
This comment has been minimized.
This comment has been minimized.
@odersky you aren't opposed to this on principle, by any chance? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left I few comments to motivate the change.
@@ -4752,7 +4752,7 @@ trait Types | |||
/** def isNonValueType(tp: Type) = !isValueElseNonValue(tp) */ | |||
|
|||
def isNonRefinementClassType(tpe: Type) = tpe match { | |||
case SingleType(_, sym) => sym.isModuleClass | |||
case SingleType(_, sym) => sym.isModuleOrModuleClass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the end, this seems to be suspiciously simple fix. Not sure if that has other implications though.
trait Foo { | ||
@AnnotationWithClassType(cls = classOf[Bar.type]) | ||
def function: Any = ??? | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this compile test because the issue shows up when using a Java annotation that requires a class. But of course, the impl allows many other usages.
When I faced that issue for the first time, I tried to used it as follows:
trait Foo {
@AnnotationWithClassType(cls = Bar.getClass)
def function: Any = ???
}
which gives the following error:
Foo.scala:5: error: annotation argument needs to be a constant; found: Bar.getClass()
@AnnotationWithClassType(cls = Bar.getClass)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
How about I just worked around this:
Edit: I see the macro version in the linked ticket. |
Yeah, that's user-land |
This won't work with Java annotation for the same reason that object Bar
object Companion {
def classOf[A: reflect.ClassTag] = implicitly[reflect.ClassTag[A]].runtimeClass
}
trait Foo {
@AnnotationWithClassType(cls = Companion.classOf[Bar.type])
def function: Any = ???
}
gives: Foo.scala:9: error: annotation argument needs to be a constant; found: Companion.classOf[Bar.type]((ClassTag.apply[Bar.type](classOf[Bar$]): scala.reflect.ClassTag[Bar.type]))
@AnnotationWithClassType(cls = Companion.classOf[Bar.type]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get why it needs a rebase on #9046. Did you pasted here the right issue number? Btw, the tests are failing for an unrelated compilation issue. [error] /home/travis/build/scala/scala/src/compiler/scala/tools/nsc/transform/Constructors.scala:477:25: Unused import
[error] import scala.util.Try
[error] ^
[error] one error found
[error] (compiler / Compile / compileIncremental) Compilation failed Is 2.13.x branch broken? Or maybe your intention was to link here the PR that fix 2.13 branch? |
I guess this was the PR you wanted to link: #9280 |
562dbe1
to
d184843
Compare
TravisCI failed with some other unrelated reason. I will try again in a few days (after rebasing on 2.13). Also, before adding more time on this, I would like to know if the solution is sound. |
d184843
to
cb4b36e
Compare
It looks good to me, so it has a reasonable chance to be merged. |
cb4b36e
to
0f9911c
Compare
codewise LGTM @smarter any objection to making the corresponding change to Scala 3? |
It already works in dotty :). |
thank you Renato! you get a special prize for closing such a low bug number! (*) (* there is no actual prize) |
This is a fix for scala/bug#2453.
It makes it possible to write the following: