From 3314d76cebc6e27ba4d4ca75cc64fe67fcb90fb6 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Tue, 18 Feb 2014 14:37:51 +0100 Subject: [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 609047ba372ceaf06916d3361954bc949a6906ee) --- src/reflect/scala/reflect/internal/Trees.scala | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/reflect') diff --git a/src/reflect/scala/reflect/internal/Trees.scala b/src/reflect/scala/reflect/internal/Trees.scala index 53b9b1d88e..0e5985face 100644 --- a/src/reflect/scala/reflect/internal/Trees.scala +++ b/src/reflect/scala/reflect/internal/Trees.scala @@ -1538,6 +1538,21 @@ trait Trees extends api.Trees { self: SymbolTable => def duplicateAndKeepPositions(tree: Tree) = new Duplicator(focusPositions = false) transform tree + def wrapIntoTerm(tree: Tree): Tree = { + if (!tree.isTerm) Block(List(tree), Literal(Constant(()))) else tree + } + + // this is necessary to avoid crashes like https://github.com/scalamacros/paradise/issues/1 + // when someone tries to c.typecheck a naked MemberDef + def wrappingIntoTerm(tree0: Tree)(op: Tree => Tree): Tree = { + val neededWrapping = !tree0.isTerm + val tree1 = wrapIntoTerm(tree0) + op(tree1) match { + case Block(tree2 :: Nil, Literal(Constant(()))) if neededWrapping => tree2 + case tree2 => tree2 + } + } + // ------ copiers ------------------------------------------- def copyDefDef(tree: Tree)( -- cgit v1.2.3