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

Support macro annotation expansions in -Wmacros:MODE #8799

Merged
merged 1 commit into from Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -651,11 +651,13 @@ trait TypeDiagnostics {
}
object skipMacroExpansion extends UnusedPrivates {
override def traverse(t: Tree): Unit =
if (!hasMacroExpansionAttachment(t)) super.traverse(t)
if (!hasMacroExpansionAttachment(t) && !(t.hasSymbol && isExpanded(t.symbol)))
super.traverse(t)
}
object checkMacroExpandee extends UnusedPrivates {
override def traverse(t: Tree): Unit =
super.traverse(if (hasMacroExpansionAttachment(t)) macroExpandee(t) else t)
if (!(t.hasSymbol && isExpanded(t.symbol)))
super.traverse(if (hasMacroExpansionAttachment(t)) macroExpandee(t) else t)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the condition is flipped? This is the "yes, lint the expandee".

Moving the test to neg and including a Test_3 with -Wmacro:after to show the error would verify.

Copy link
Contributor

Choose a reason for hiding this comment

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

I only just glanced at the code comment in StdAttachments about "should be a better way".

Copy link
Contributor Author

@cb372 cb372 Mar 13, 2020

Choose a reason for hiding this comment

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

I agree having a "pos" test for -Wmacro:before and a "neg" test for -Wmacro:after is a good idea, but I'm a bit confused by your suggestion. Is the semantics of neg tests that all the files except the last one should compile, and the last one should not compile? So we can write both the pos and the neg parts in the same test?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it would compile A_1, then B_2, then error on C_3, and each round can use different flags. If you think that is too confusing or subtle, pos and neg tests is OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification. I added a separate neg test as I think that's slightly clearer.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not obvious from my first comment, but for "expandee" I always conflate "tree to be expanded" and "result of expansion". I would be less confused by "pre" and "post" or "original" and "expanded", etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, the naming in this area is mighty confusing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Some followup to my confusion in #10693 (in case I have to research the nomenclature again in future). The other naming that confuses me still is each instance of UnusedPrivates as it relates to -Wmacros settings. Maybe I only have trouble with names I didn't come up with myself. For an improved default behavior for -Wmacros, I could only come up with default.

}

private def warningsEnabled: Boolean = {
Expand Down
6 changes: 6 additions & 0 deletions test/files/neg/macro-annot-unused-param.check
@@ -0,0 +1,6 @@
Test_2.scala:2: warning: parameter value x in anonymous function is never used
@mymacro
^
error: No warnings can be incurred under -Werror.
1 warning
1 error
22 changes: 22 additions & 0 deletions test/files/neg/macro-annot-unused-param/Macros_1.scala
@@ -0,0 +1,22 @@
// scalac: -Ymacro-annotations
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.annotation.StaticAnnotation

object Macros {
def annotImpl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val classTree = annottees.head.tree
val objectTree = q"""
object X {
def f: Int => String = { x => "hello" }
}
"""

c.Expr[Any](Block(List(classTree, objectTree), Literal(Constant(()))))
}
}

class mymacro extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro Macros.annotImpl
}
7 changes: 7 additions & 0 deletions test/files/neg/macro-annot-unused-param/Test_2.scala
@@ -0,0 +1,7 @@
// scalac: -Ymacro-annotations -Wunused:params -Wmacros:after -Werror
@mymacro
class X

object Test {
println(X.f(123))
}
22 changes: 22 additions & 0 deletions test/files/pos/macro-annot-unused-param/Macros_1.scala
@@ -0,0 +1,22 @@
// scalac: -Ymacro-annotations
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.annotation.StaticAnnotation

object Macros {
def annotImpl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val classTree = annottees.head.tree
val objectTree = q"""
object X {
def f: Int => String = { x => "hello" }
}
"""

c.Expr[Any](Block(List(classTree, objectTree), Literal(Constant(()))))
}
}

class mymacro extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro Macros.annotImpl
}
7 changes: 7 additions & 0 deletions test/files/pos/macro-annot-unused-param/Test_2.scala
@@ -0,0 +1,7 @@
// scalac: -Ymacro-annotations -Wunused:params -Werror
@mymacro
class X

object Test {
println(X.f(123))
}