-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try not to approximate prefixes when using memberType in reflect API (#…
…22448) To achieve this, we substitute This types in `member.info` referencing classSymbol with TypeReprs actual self (which may or may not be a This type) Fixes #22424
- Loading branch information
Showing
3 changed files
with
39 additions
and
2 deletions.
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,25 @@ | ||
|
||
import scala.quoted.* | ||
|
||
object MockMaker: | ||
inline def inlineMock[T]: Unit = ${instance[T]} | ||
transparent inline def transparentInlineMock[T]: Unit = ${instance[T]} | ||
|
||
def instance[T: Type](using quotes: Quotes): Expr[Unit] = | ||
import quotes.reflect._ | ||
val tpe = TypeRepr.of[T] | ||
val symbol = tpe.typeSymbol.methodMember("innerTraitInOptions").head | ||
tpe.memberType(symbol) match | ||
case mt @ MethodType(_, args, _) => | ||
assert(args.head.typeSymbol != TypeRepr.of[Nothing].typeSymbol, "argument is incorrectly approximated") | ||
val shownType = mt.show | ||
val expectedType = "(x: m.Embedded#ATrait[scala.Predef.String, scala.Int])m.Embedded#ATrait[scala.Predef.String, scala.Int]" | ||
assert(shownType == expectedType, s"Incorrect type shown. Obtained: $shownType, Expected: $expectedType") | ||
'{()} | ||
|
||
trait PolymorphicTrait { | ||
trait Embedded { | ||
trait ATrait[A, B] | ||
def innerTraitInOptions(x: ATrait[String, Int]): ATrait[String, Int] | ||
} | ||
} |
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,4 @@ | ||
@main def Test = | ||
val m = new PolymorphicTrait {} | ||
MockMaker.inlineMock[m.Embedded] | ||
MockMaker.transparentInlineMock[m.Embedded] |