-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #6048: set missing position in value class rewiring #6074
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
Merged
Changes from all commits
Commits
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
47 changes: 47 additions & 0 deletions
47
tests/run-with-compiler/reflect-select-value-class/assert_1.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,47 @@ | ||
import scala.quoted._ | ||
import scala.tasty._ | ||
|
||
object scalatest { | ||
|
||
inline def assert(condition: => Boolean): Unit = ${ assertImpl('condition, '{""}) } | ||
|
||
def assertImpl(cond: Expr[Boolean], clue: Expr[Any])(implicit refl: Reflection): Expr[Unit] = { | ||
import refl._ | ||
import util._ | ||
import quoted.Toolbox.Default._ | ||
|
||
def isImplicitMethodType(tp: Type): Boolean = | ||
Type.IsMethodType.unapply(tp).flatMap(tp => if tp.isImplicit then Some(true) else None).nonEmpty | ||
|
||
cond.unseal.underlyingArgument match { | ||
case t @ Term.Apply(Term.Select(lhs, op), rhs :: Nil) => | ||
let(lhs) { left => | ||
let(rhs) { right => | ||
val app = Term.Select.overloaded(left, op, Nil, right :: Nil) | ||
let(app) { result => | ||
val l = left.seal[Any] | ||
val r = right.seal[Any] | ||
val b = result.seal[Boolean] | ||
val code = '{ scala.Predef.assert($b) } | ||
code.unseal | ||
} | ||
} | ||
}.seal[Unit] | ||
case Term.Apply(f @ Term.Apply(Term.Select(Term.Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits) | ||
if isImplicitMethodType(f.tpe) => | ||
let(lhs) { left => | ||
let(rhs) { right => | ||
val app = Term.Select.overloaded(Term.Apply(qual, left :: Nil), op, Nil, right :: Nil) | ||
let(Term.Apply(app, implicits)) { result => | ||
val l = left.seal[Any] | ||
val r = right.seal[Any] | ||
val b = result.seal[Boolean] | ||
val code = '{ scala.Predef.assert($b) } | ||
code.unseal | ||
} | ||
} | ||
}.seal[Unit] | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
tests/run-with-compiler/reflect-select-value-class/test_2.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 @@ | ||
object Test { | ||
import scalatest._ | ||
|
||
implicit class AnyOps(x: String) extends AnyVal { | ||
def *(y: String): String = x + ", " + y | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
assert(("hello" * "world") == "hello, world") | ||
} | ||
} |
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 would put the withSpan in the definition of rewire
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 @smarter . That was what @nicolasstucki and I initially did -- but soon we find it's not the right place due to the recursive nature of
rewire
.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.
There is one branch of
rewire
which is not recursive (the last one), that branch should get the withSpan.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.
The problem is that we want to set the initial
tree.span
, instead of thetree.span
from recursive calls.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.
The issue there is that the
tree
is the tree of the qualifier which does not have the position of the full call (it does not cover the arguments). We could pass the position as a parameter torewire
instead.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.
ah I see, one way to do that would be to change rewire to contain an inner method which is recursive, but I guess it's not very important since it's only used once anyway.
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.
Or we could pass the
span
as an extra parameter.