Skip to content

Fix regression: do not approximate prefixes when using memberType in reflect API #22448

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 3 commits into from
Mar 4, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end SimpleSelectorTypeTest

object SimpleSelector extends SimpleSelectorModule:
def apply(name: String): SimpleSelector =
def apply(name: String): SimpleSelector =
withDefaultPos(untpd.ImportSelector(untpd.Ident(name.toTermName)))
def unapply(x: SimpleSelector): Some[String] = Some(x.name.toString)
end SimpleSelector
Expand Down Expand Up @@ -1837,7 +1837,15 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def termSymbol: Symbol = self.termSymbol
def isSingleton: Boolean = self.isSingleton
def memberType(member: Symbol): TypeRepr =
member.info.asSeenFrom(self, member.owner)
// we replace thisTypes here to avoid resolving otherwise unstable prefixes into Nothing
val memberInfo =
if self.typeSymbol.isClassDef then
member.info.substThis(self.classSymbol.asClass, self)
else
member.info
memberInfo
.asSeenFrom(self, member.owner)

def baseClasses: List[Symbol] = self.baseClasses
def baseType(cls: Symbol): TypeRepr = self.baseType(cls)
def derivesFrom(cls: Symbol): Boolean = self.derivesFrom(cls)
Expand Down
25 changes: 25 additions & 0 deletions tests/pos-macros/i22424/Macro_1.scala
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]
}
}
4 changes: 4 additions & 0 deletions tests/pos-macros/i22424/Test_2.scala
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]
Loading