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
som-snytt
merged 1 commit into
scala:2.13.x
from
cb372:macro-annotations-unused-symbols
Mar 18, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// scalac: -Ymacro-annotations -Wunused:params -Wmacros:after -Werror | ||
@mymacro | ||
class X | ||
|
||
object Test { | ||
println(X.f(123)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// scalac: -Ymacro-annotations -Wunused:params -Werror | ||
@mymacro | ||
class X | ||
|
||
object Test { | ||
println(X.f(123)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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.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 only just glanced at the code comment in
StdAttachments
about "should be a better way".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 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?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.
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.
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.
Thanks for the clarification. I added a separate neg test as I think that's slightly clearer.
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.
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.
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 agree, the naming in this area is mighty confusing.
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.
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 withdefault
.