-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support zinc invalidation for type arguments in macro calls #23900
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
Open
jchyb
wants to merge
4
commits into
scala:main
Choose a base branch
from
dotty-staging:fix-23852-invalidate-macro-type-arg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
object Components extends App { | ||
try { | ||
wire[Dep] | ||
} catch { | ||
case e: Throwable => | ||
e.printStackTrace() | ||
sys.exit(-1) | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
class Dep() |
This file contains hidden or 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 @@ | ||
class Dep(int: Int) |
This file contains hidden or 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,16 @@ | ||
import scala.quoted.* | ||
|
||
inline def wire[T]: T = ${ wireImpl[T] } | ||
def wireImpl[T: Type](using q: Quotes): Expr[T] = { | ||
import q.reflect.* | ||
|
||
lazy val targetType = TypeRepr.of[T] | ||
val constructorValue = targetType.typeSymbol.primaryConstructor | ||
val constructionMethodTree: Term = { | ||
val ctor = Select(New(TypeIdent(targetType.typeSymbol)), constructorValue) | ||
if (targetType.typeArgs.isEmpty) ctor else ctor.appliedToTypes(targetType.typeArgs) | ||
} | ||
val constructorArgsValue = List(Nil) | ||
val code: Tree = constructorArgsValue.foldLeft(constructionMethodTree)((acc: Term, args: List[Term]) => Apply(acc, args)) | ||
code.asExprOf[T] | ||
} |
This file contains hidden or 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,11 @@ | ||
import sbt._ | ||
import Keys._ | ||
|
||
object DottyInjectedPlugin extends AutoPlugin { | ||
override def requires = plugins.JvmPlugin | ||
override def trigger = allRequirements | ||
|
||
override val projectSettings = Seq( | ||
scalaVersion := sys.props("plugin.scalaVersion") | ||
) | ||
} |
This file contains hidden or 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,3 @@ | ||
> run | ||
$ copy-file Dep.scala-added Dep.scala | ||
-> compile |
This file contains hidden or 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,2 @@ | ||
package A | ||
class A |
This file contains hidden or 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,11 @@ | ||
package app | ||
|
||
import Macros.* | ||
import A.A | ||
|
||
object App { | ||
@main def hasFields(expected: Boolean): Unit = { | ||
val actual = Macros.hasAnyField[A] | ||
assert(expected == actual, s"Expected $expected, obtained $actual") | ||
} | ||
} |
This file contains hidden or 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,27 @@ | ||
// { | ||
// "projects": [ | ||
// { | ||
// "name": "app", | ||
// "dependsOn": [ | ||
// "macros", | ||
// "A" | ||
// ], | ||
// "scalaVersion": "2.13.12" | ||
// }, | ||
// { | ||
// "name": "macros", | ||
// "scalaVersion": "2.13.12" | ||
// }, | ||
// { | ||
// "name": "A", | ||
// "scalaVersion": "2.13.12" | ||
// } | ||
// ] | ||
// } | ||
|
||
lazy val app = project.in(file("app")) | ||
.dependsOn(macros, A) | ||
|
||
lazy val macros = project.in(file("macros")) | ||
|
||
lazy val A = project.in(file("A")) |
This file contains hidden or 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,4 @@ | ||
package A | ||
class A { | ||
val hello: String = "" | ||
} |
This file contains hidden or 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,15 @@ | ||
package Macros | ||
|
||
import scala.quoted.* | ||
|
||
object Macros { | ||
inline def hasAnyField[T]: Boolean = ${ hasAnyFieldImpl[T] } | ||
|
||
def hasAnyFieldImpl[T: Type](using Quotes): Expr[Boolean] = { | ||
import quotes.reflect.* | ||
|
||
val hasField = TypeRepr.of[T].typeSymbol.fieldMembers.nonEmpty | ||
|
||
Expr(hasField) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
sbt-test/macros/macro-type-change-2/project/DottyInjectedPlugin.scala
This file contains hidden or 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,11 @@ | ||
import sbt._ | ||
import Keys._ | ||
|
||
object DottyInjectedPlugin extends AutoPlugin { | ||
override def requires = plugins.JvmPlugin | ||
override def trigger = allRequirements | ||
|
||
override val projectSettings = Seq( | ||
scalaVersion := sys.props("plugin.scalaVersion") | ||
) | ||
} |
This file contains hidden or 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,4 @@ | ||
# adapted from https://github.com/sbt/zinc/blob/1e422e5525c698aa71cc35b30c275c8c1c3135b2/zinc/src/sbt-test/macros/macro-type-change-2/test | ||
> app/run false | ||
$ copy-file changes/A1.scala A/A.scala | ||
> app/run true |
This file contains hidden or 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,2 @@ | ||
package A | ||
class A |
This file contains hidden or 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,2 @@ | ||
package A | ||
class B extends A |
This file contains hidden or 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,11 @@ | ||
package app | ||
|
||
import Macros.* | ||
import A.B | ||
|
||
object App { | ||
@main def hasFields(expected: Boolean): Unit = { | ||
val actual = Macros.hasAnyField[B] | ||
assert(expected == actual, s"Expected $expected, obtained $actual") | ||
} | ||
} |
This file contains hidden or 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,27 @@ | ||
// { | ||
// "projects": [ | ||
// { | ||
// "name": "app", | ||
// "dependsOn": [ | ||
// "macros", | ||
// "A" | ||
// ], | ||
// "scalaVersion": "2.13.12" | ||
// }, | ||
// { | ||
// "name": "macros", | ||
// "scalaVersion": "2.13.12" | ||
// }, | ||
// { | ||
// "name": "A", | ||
// "scalaVersion": "2.13.12" | ||
// } | ||
// ] | ||
// } | ||
|
||
lazy val app = project.in(file("app")) | ||
.dependsOn(macros, A) | ||
|
||
lazy val macros = project.in(file("macros")) | ||
|
||
lazy val A = project.in(file("A")) |
This file contains hidden or 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,4 @@ | ||
package A | ||
class A { | ||
val hello: String = "" | ||
} |
This file contains hidden or 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,15 @@ | ||
package Macros | ||
|
||
import scala.quoted.* | ||
|
||
object Macros { | ||
inline def hasAnyField[T]: Boolean = ${ hasAnyFieldImpl[T] } | ||
|
||
def hasAnyFieldImpl[T: Type](using Quotes): Expr[Boolean] = { | ||
import quotes.reflect.* | ||
|
||
val hasField = TypeRepr.of[T].typeSymbol.fieldMembers.nonEmpty | ||
|
||
Expr(hasField) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
sbt-test/macros/macro-type-change-3/project/DottyInjectedPlugin.scala
This file contains hidden or 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,11 @@ | ||
import sbt._ | ||
import Keys._ | ||
|
||
object DottyInjectedPlugin extends AutoPlugin { | ||
override def requires = plugins.JvmPlugin | ||
override def trigger = allRequirements | ||
|
||
override val projectSettings = Seq( | ||
scalaVersion := sys.props("plugin.scalaVersion") | ||
) | ||
} |
This file contains hidden or 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,4 @@ | ||
# adapted from https://github.com/sbt/zinc/blob/1e422e5525c698aa71cc35b30c275c8c1c3135b2/zinc/src/sbt-test/macros/macro-type-change-3/test | ||
> app/run false | ||
$ copy-file changes/A1.scala A/A.scala | ||
> app/run true |
This file contains hidden or 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,2 @@ | ||
package A | ||
class A |
This file contains hidden or 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,11 @@ | ||
package app | ||
|
||
import Macros.* | ||
import A.A | ||
|
||
object App { | ||
@main def hasFields(expected: Boolean): Unit = { | ||
val actual = Macros.hasAnyField[A](true) | ||
assert(expected == actual, s"Expected $expected, obtained $actual") | ||
} | ||
} |
This file contains hidden or 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,27 @@ | ||
// { | ||
// "projects": [ | ||
// { | ||
// "name": "app", | ||
// "dependsOn": [ | ||
// "macros", | ||
// "A" | ||
// ], | ||
// "scalaVersion": "2.13.12" | ||
// }, | ||
// { | ||
// "name": "macros", | ||
// "scalaVersion": "2.13.12" | ||
// }, | ||
// { | ||
// "name": "A", | ||
// "scalaVersion": "2.13.12" | ||
// } | ||
// ] | ||
// } | ||
|
||
lazy val app = project.in(file("app")) | ||
.dependsOn(macros, A) | ||
|
||
lazy val macros = project.in(file("macros")) | ||
|
||
lazy val A = project.in(file("A")) |
This file contains hidden or 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,4 @@ | ||
package A | ||
class A { | ||
val hello: String = "" | ||
} |
This file contains hidden or 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,15 @@ | ||
package Macros | ||
|
||
import scala.quoted.* | ||
|
||
object Macros { | ||
inline def hasAnyField[T](placeholder: Boolean): Boolean = ${ hasAnyFieldImpl[T]('placeholder) } | ||
|
||
def hasAnyFieldImpl[T: Type](placeholder: Expr[Boolean])(using Quotes): Expr[Boolean] = { | ||
import quotes.reflect.* | ||
|
||
val hasField = TypeRepr.of[T].typeSymbol.fieldMembers.nonEmpty | ||
|
||
Expr(hasField) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
sbt-test/macros/macro-type-change-4/project/DottyInjectedPlugin.scala
This file contains hidden or 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,11 @@ | ||
import sbt._ | ||
import Keys._ | ||
|
||
object DottyInjectedPlugin extends AutoPlugin { | ||
override def requires = plugins.JvmPlugin | ||
override def trigger = allRequirements | ||
|
||
override val projectSettings = Seq( | ||
scalaVersion := sys.props("plugin.scalaVersion") | ||
) | ||
} |
This file contains hidden or 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,4 @@ | ||
# adapted from https://github.com/sbt/zinc/blob/1e422e5525c698aa71cc35b30c275c8c1c3135b2/zinc/src/sbt-test/macros/macro-type-change-4/test | ||
> app/run false | ||
$ copy-file changes/A1.scala A/A.scala | ||
> app/run true |
This file contains hidden or 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,2 @@ | ||
package app | ||
class A |
This file contains hidden or 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,10 @@ | ||
package app | ||
|
||
import Macros.* | ||
|
||
object App { | ||
@main def hasFields(expected: Boolean): Unit = { | ||
val actual = Macros.hasAnyField[A] | ||
assert(expected == actual, s"Expected $expected, obtained $actual") | ||
} | ||
} |
This file contains hidden or 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,20 @@ | ||
// { | ||
// "projects": [ | ||
// { | ||
// "name": "app", | ||
// "dependsOn": [ | ||
// "macros" | ||
// ], | ||
// "scalaVersion": "2.13.12" | ||
// }, | ||
// { | ||
// "name": "macros", | ||
// "scalaVersion": "2.13.12" | ||
// } | ||
// ] | ||
// } | ||
|
||
lazy val app = project.in(file("app")) | ||
.dependsOn(macros) | ||
|
||
lazy val macros = project.in(file("macros")) |
This file contains hidden or 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,4 @@ | ||
package app | ||
class A { | ||
val hello: String = "" | ||
} |
This file contains hidden or 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,15 @@ | ||
package Macros | ||
|
||
import scala.quoted.* | ||
|
||
object Macros { | ||
inline def hasAnyField[T]: Boolean = ${ hasAnyFieldImpl[T] } | ||
|
||
def hasAnyFieldImpl[T: Type](using Quotes): Expr[Boolean] = { | ||
import quotes.reflect.* | ||
|
||
val hasField = TypeRepr.of[T].typeSymbol.fieldMembers.nonEmpty | ||
|
||
Expr(hasField) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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 this is a mistake, this is using the static type (meaning field names are statically available), so this will always pass. (e.g. you made Zinc 1.10.x a library dependency)
There should be a way to dynamically test actual linked zinc version
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 getFields() still uses reflection (https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getFields--), at the very least the check still works when I publish locally and use older sbt versions relying on older zinc (as opposed to what would happen without that check, where it would crash).
I did find just now that zinc ships with
incrementalcompiler.version.properties
resource file (but no utils for that, unfortunately), so we could probably read that if you prefer (also thank you for the timely reviews! and apologies for taking this long to update the PR).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've tested just now and something like this:
also works. Let me know if I should change it to that