Skip to content
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

Check New tree for ill-formed module instantiations #17553

Merged
merged 1 commit into from
May 25, 2023
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: 12 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,18 @@ object TreeChecker {
assert(tree.qual.typeOpt.isInstanceOf[ThisType], i"expect prefix of Super to be This, actual = ${tree.qual}")
super.typedSuper(tree, pt)

override def typedNew(tree: untpd.New, pt: Type)(using Context): Tree =
val tree1 = super.typedNew(tree, pt).asInstanceOf[tpd.New]
val sym = tree1.tpe.typeSymbol
if postTyperPhase <= ctx.phase then // postTyper checks that `New` nodes can be instantiated
assert(!tree1.tpe.isInstanceOf[TermRef], s"New should not have a TermRef type: ${tree1.tpe}")
assert(
!sym.is(Module)
|| ctx.erasedTypes // TODO add check for module initialization after erasure (LazyVals transformation)
|| ctx.owner == sym.companionModule,
i"new of $sym module should only exist in ${sym.companionModule} but was in ${ctx.owner}")
tree1

override def typedApply(tree: untpd.Apply, pt: Type)(using Context): Tree = tree match
case Apply(Select(qual, nme.CONSTRUCTOR), _)
if !ctx.phase.erasedTypes
Expand Down
11 changes: 11 additions & 0 deletions tests/neg-macros/i17545a/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted.*

object InvokeConstructor {
inline def apply[A] = ${ constructorMacro[A] }

def constructorMacro[A: Type](using Quotes) = {
import quotes.reflect.*
val tpe = TypeRepr.of[A]
New(Inferred(tpe)).select(tpe.typeSymbol.primaryConstructor).appliedToArgs(Nil).asExprOf[A]
}
}
4 changes: 4 additions & 0 deletions tests/neg-macros/i17545a/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
case object WhateverA

def testA =
val whateverA: WhateverA.type = InvokeConstructor[WhateverA.type] // error
11 changes: 11 additions & 0 deletions tests/neg-macros/i17545b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted.*

object InvokeConstructor {
inline def apply[A] = ${ constructorMacro[A] }

def constructorMacro[A: Type](using Quotes) = {
import quotes.reflect.*
val tpe = TypeRepr.of[A]
New(Inferred(tpe)).select(tpe.typeSymbol.primaryConstructor).appliedToArgs(Nil).asExprOf[A]
}
}
3 changes: 3 additions & 0 deletions tests/neg-macros/i17545b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def testB =
case object WhateverB
val whateverB: WhateverB.type = InvokeConstructor[WhateverB.type] // error
11 changes: 11 additions & 0 deletions tests/neg-macros/i17545c/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted.*

object InvokeConstructor {
inline def apply[A] = ${ constructorMacro[A] }

def constructorMacro[A: Type](using Quotes) = {
import quotes.reflect.*
val tpe = TypeRepr.of[A].termSymbol.moduleClass.typeRef
New(Inferred(tpe)).select(tpe.typeSymbol.primaryConstructor).appliedToArgs(Nil).asExprOf[A]
}
}
4 changes: 4 additions & 0 deletions tests/neg-macros/i17545c/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
case object WhateverA

def testA =
val whateverA: WhateverA.type = InvokeConstructor[WhateverA.type] // error
11 changes: 11 additions & 0 deletions tests/neg-macros/i17545d/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted.*

object InvokeConstructor {
inline def apply[A] = ${ constructorMacro[A] }

def constructorMacro[A: Type](using Quotes) = {
import quotes.reflect.*
val tpe = TypeRepr.of[A].termSymbol.moduleClass.typeRef
New(Inferred(tpe)).select(tpe.typeSymbol.primaryConstructor).appliedToArgs(Nil).asExprOf[A]
}
}
3 changes: 3 additions & 0 deletions tests/neg-macros/i17545d/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def testB =
case object WhateverB
val whateverB: WhateverB.type = InvokeConstructor[WhateverB.type] // error