Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[nomaster] typecheck(q"class C") no longer crashes
MemberDefs alone can't be typechecked as is, because namer only names contents of PackageDefs, Templates and Blocks. And, if not named, a tree can't be typed. This commit solves this problem by wrapping typecheckees in a trivial block and then unwrapping the result when it returns back from the typechecker. (cherry picked from commit 609047b)
- Loading branch information
Showing
6 changed files
with
100 additions
and
62 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
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
Empty file.
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,17 @@ | ||
import scala.reflect.macros.Context | ||
import scala.language.experimental.macros | ||
|
||
object Macros { | ||
def impl(c: Context) = { | ||
import c.universe._ | ||
val classDef = ClassDef( | ||
Modifiers(), newTypeName("C"), List(), | ||
Template( | ||
List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))), emptyValDef, | ||
List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(()))))))) | ||
c.typeCheck(classDef) | ||
c.Expr[Any](Literal(Constant(()))) | ||
} | ||
|
||
def foo: Any = macro impl | ||
} |
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,15 @@ | ||
import scala.reflect.runtime.universe._ | ||
import scala.reflect.runtime.{currentMirror => cm} | ||
import scala.tools.reflect.ToolBox | ||
|
||
object Test extends App { | ||
Macros.foo | ||
|
||
val tb = cm.mkToolBox() | ||
val classDef = ClassDef( | ||
Modifiers(), newTypeName("C"), List(), | ||
Template( | ||
List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))), emptyValDef, | ||
List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(()))))))) | ||
tb.typeCheck(classDef) | ||
} |