Skip to content

Minor architectural clean-up #6941

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

Merged

Conversation

anatoliykmetyuk
Copy link
Contributor

No description provided.

Copy link
Contributor

@allanrenucci allanrenucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM

@@ -115,15 +115,11 @@ object Annotations {
def apply(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation =
apply(New(atp, args))

private def resolveConstructor(atp: Type, args:List[Tree])(implicit ctx: Context): Tree = {
def resolveConstructor(atp: Type, args:List[Tree])(implicit ctx: Context): Tree = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that it's public, I think compiler/src/dotty/tools/dotc/ast/tpd.scala is a better place for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this one is quite annotation-specific. I think tpd has lots of things already in it - why do you think it would look better there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it work with any types? I realize it is currently only used for annotations, but I would expect there are other places where this could be used. Here is a possible candidate:

https://github.com/lampepfl/dotty/blob/d8ac6cdf97ce14018e862c84b85d44f88d83a540/compiler/src/dotty/tools/backend/sjs/JUnitBootstrappers.scala#L253-L257

If this is only used for annotations, then I agree Annotations.scala is a better place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, makes sense, it can well be used for other types. I'm not sure of your example though, the lack of type parameters handling and the absence of applyOverloaded make me wonder about the semantic differences.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no semantic difference in this particular use case. The author is performing overloading resolution manually. He is looking for an overload for the constructor of NoSuchMethodException. There is no type parameter and using the following predicate is enough to select the right constructor:

c.info.paramInfoss.head.size == 1 && c.info.paramInfoss.head.head.isRef(defn.StringClass)

So we could rewrite the code as follow:

--- a/compiler/src/dotty/tools/backend/sjs/JUnitBootstrappers.scala
+++ b/compiler/src/dotty/tools/backend/sjs/JUnitBootstrappers.scala
@@ -250,11 +250,7 @@ class JUnitBootstrappers extends MiniPhase {
         ValDef(castInstanceSym, instanceParamRef.cast(testClass.typeRef)) :: Nil,
         tests.foldRight[Tree] {
           val tp = junitdefn.NoSuchMethodExceptionType
-          val constr = tp.member(nme.CONSTRUCTOR).suchThat { c =>
-            c.info.paramInfoss.head.size == 1 &&
-            c.info.paramInfoss.head.head.isRef(defn.StringClass)
-          }.symbol.asTerm
-          Throw(New(tp, constr, nameParamRef :: Nil))
+          Throw(resolveConstructor(tp, nameParamRef :: Nil))
         } { (test, next) =>
           If(Literal(Constant(test.name.toString)).select(defn.Any_equals).appliedTo(nameParamRef),
             genTestInvocation(testClass, test, ref(castInstanceSym)),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is another candidate:

--- a/compiler/src/dotty/tools/backend/sjs/JUnitBootstrappers.scala
+++ b/compiler/src/dotty/tools/backend/sjs/JUnitBootstrappers.scala
@@ -229,8 +229,7 @@ class JUnitBootstrappers extends MiniPhase {
         val testAnnot = test.getAnnotation(junitdefn.TestAnnotClass).get
         if (testAnnot.arguments.nonEmpty)
           ctx.error("@Test annotations with arguments are not yet supported in Scala.js for dotty", testAnnot.tree.sourcePos)
-        val noArgConstr = junitdefn.TestAnnotType.member(nme.CONSTRUCTOR).suchThat(_.info.paramInfoss.head.isEmpty).symbol.asTerm
-        val reifiedAnnot = New(junitdefn.TestAnnotType, noArgConstr, Nil)
+        val reifiedAnnot = resolveConstructor(junitdefn.TestAnnotType, Nil)
         New(junitdefn.TestMetadataType, List(name, ignored, reifiedAnnot))
       }
       JavaSeqLiteral(metadata, TypeTree(junitdefn.TestMetadataType))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's try...

@anatoliykmetyuk anatoliykmetyuk force-pushed the architectural-improvements branch from 93efc19 to 54b6234 Compare July 26, 2019 18:36
@anatoliykmetyuk anatoliykmetyuk force-pushed the architectural-improvements branch from 54b6234 to b7a433c Compare July 26, 2019 18:41
@anatoliykmetyuk anatoliykmetyuk requested a review from sjrd July 26, 2019 19:56
@anatoliykmetyuk
Copy link
Contributor Author

/cc @sjrd since it touches ScalaJS codebase

@anatoliykmetyuk anatoliykmetyuk merged commit 0b52037 into scala:master Jul 29, 2019
@anatoliykmetyuk anatoliykmetyuk deleted the architectural-improvements branch July 29, 2019 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants