Skip to content

Commit 148f69f

Browse files
committed
Assert that memberType selects an existing memberType
Fixes #15159 Updates buggy tests of #13230
1 parent 819c625 commit 148f69f

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
17791779
def termSymbol: Symbol = self.termSymbol
17801780
def isSingleton: Boolean = self.isSingleton
17811781
def memberType(member: Symbol): TypeRepr =
1782+
assert(self.derivesFrom(member.owner), s"$member is not a member of ${self.show}")
17821783
member.info.asSeenFrom(self, member.owner)
17831784
def baseClasses: List[Symbol] = self.baseClasses
17841785
def baseType(cls: Symbol): TypeRepr = self.baseType(cls)

tests/neg-macros/i15159/Macro_1.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted.*
2+
object TestMacro:
3+
inline def test[T]: Unit = ${ testImpl[T] }
4+
def testImpl[T: Type](using Quotes): Expr[Unit] =
5+
import quotes.reflect.*
6+
val tpe = TypeRepr.of[T]
7+
tpe.typeSymbol.children.map { childSymbol =>
8+
tpe.memberType(childSymbol) // not a member of tpe
9+
}
10+
'{ () }

tests/neg-macros/i15159/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sealed trait A
2+
case class X(i: Int) extends A
3+
4+
object Test extends App {
5+
TestMacro.test[A] // error
6+
}

tests/run-macros/i13230/Macros_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ inline def showEnumChildren = ${ showEnumChildrenExpr }
77

88
def showEnumChildrenExpr(using Quotes) =
99
import quotes.reflect.*
10-
val repr = TypeRepr.of[E]
10+
val repr = TypeRepr.of[E.type] // we know that all the implementations of the class E are in the object E
1111
Expr(TypeRepr.of[E].classSymbol.get.children.map(sym => (sym.name, repr.memberType(sym).show)))
1212

0 commit comments

Comments
 (0)