From 94eb751d0063b80e7bca542b9c243900527f9028 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 8 Jan 2014 21:09:32 +0100 Subject: Removes unnecessary generality in the macro engine In Jan 2013, I submitted a number of pull requests that built up a foundation for the upcoming type macros pull request. Unfortunately, type macros ended up being rejected, but the extra generality introduced in advance still persisted in the compiler until now. This commit takes care of unused generality in the macro engine, keeping the internal implementation as well as the public API clean. --- .../tools/nsc/typechecker/ContextErrors.scala | 4 -- .../scala/tools/nsc/typechecker/Macros.scala | 45 ++++------------------ .../tools/nsc/typechecker/StdAttachments.scala | 8 ++-- 3 files changed, 12 insertions(+), 45 deletions(-) (limited to 'src/compiler/scala/tools/nsc/typechecker') diff --git a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala index 352ae6c8b0..cd6b77404d 100644 --- a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala +++ b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala @@ -726,10 +726,6 @@ trait ContextErrors { NormalTypeError(expandee, "too many argument lists for " + fun) } - def MacroInvalidExpansionError(expandee: Tree, role: String, allowedExpansions: String) = { - issueNormalTypeError(expandee, s"macro in $role role can only expand into $allowedExpansions") - } - def MacroIncompatibleEngineError(macroEngine: String) = { val message = s"macro cannot be expanded, because it was compiled by an incompatible macro engine $macroEngine" issueNormalTypeError(lastTreeToTyper, message) diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala index c1a6ac32c9..a1e77515a8 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala @@ -354,7 +354,6 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { val universe: self.global.type = self.global val callsiteTyper: universe.analyzer.Typer = typer.asInstanceOf[global.analyzer.Typer] val expandee = universe.analyzer.macroExpanderAttachment(expandeeTree).original orElse duplicateAndKeepPositions(expandeeTree) - val macroRole = universe.analyzer.macroExpanderAttachment(expandeeTree).role } with UnaffiliatedMacroContext { val prefix = Expr[Nothing](prefixTree)(TypeTag.Nothing) override def toString = "MacroContext(%s@%s +%d)".format(expandee.symbol.name, expandee.pos, enclosingMacros.length - 1 /* exclude myself */) @@ -487,12 +486,6 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { def popMacroContext() = _openMacros = _openMacros.tail def enclosingMacroPosition = openMacros map (_.macroApplication.pos) find (_ ne NoPosition) getOrElse NoPosition - /** Describes the role that the macro expandee is performing. - */ - type MacroRole = scala.tools.nsc.typechecker.MacroRole - final def APPLY_ROLE = MacroRole.Apply - final def UNAPPLY_ROLE = MacroRole.Unapply - /** Performs macro expansion: * * ========= Expandable trees ========= @@ -535,12 +528,7 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { * the expandee with an error marker set if the expansion has been cancelled due malformed arguments or implementation * the expandee with an error marker set if there has been an error */ - abstract class MacroExpander(val role: MacroRole, val typer: Typer, val expandee: Tree) { - def allowExpandee(expandee: Tree): Boolean = true - def allowExpanded(expanded: Tree): Boolean = true - def allowedExpansions: String = "anything" - def allowResult(result: Tree): Boolean = true - + abstract class MacroExpander(val typer: Typer, val expandee: Tree) { def onSuccess(expanded: Tree): Tree def onFallback(expanded: Tree): Tree def onSuppressed(expandee: Tree): Tree = expandee @@ -557,8 +545,7 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { def showDetailed(tree: Tree) = showRaw(tree, printIds = true, printTypes = true) def summary() = s"expander = $this, expandee = ${showDetailed(expandee)}, desugared = ${if (expandee == desugared) () else showDetailed(desugared)}" if (macroDebugVerbose) println(s"macroExpand: ${summary()}") - assert(allowExpandee(expandee), summary()) - linkExpandeeAndDesugared(expandee, desugared, role) + linkExpandeeAndDesugared(expandee, desugared) val start = if (Statistics.canEnable) Statistics.startTimer(macroExpandNanos) else null if (Statistics.canEnable) Statistics.incCounter(macroExpandCount) @@ -576,18 +563,11 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { } expanded match { case Success(expanded) => - if (allowExpanded(expanded)) { - // also see http://groups.google.com/group/scala-internals/browse_thread/thread/492560d941b315cc - val expanded1 = try onSuccess(duplicateAndKeepPositions(expanded)) finally popMacroContext() - if (!hasMacroExpansionAttachment(expanded1)) linkExpandeeAndExpanded(expandee, expanded1) - if (allowResult(expanded1)) { - if (settings.Ymacroexpand.value == settings.MacroExpand.Discard) expandee.setType(expanded1.tpe) - else expanded1 - } else onFailure(expanded) - } else { - typer.TyperErrorGen.MacroInvalidExpansionError(expandee, role.name, allowedExpansions) - onFailure(expanded) - } + // also see http://groups.google.com/group/scala-internals/browse_thread/thread/492560d941b315cc + val expanded1 = try onSuccess(duplicateAndKeepPositions(expanded)) finally popMacroContext() + if (!hasMacroExpansionAttachment(expanded1)) linkExpandeeAndExpanded(expandee, expanded1) + if (settings.Ymacroexpand.value == settings.MacroExpand.Discard) expandee.setType(expanded1.tpe) + else expanded1 case Fallback(fallback) => onFallback(fallback) case Delayed(delayed) => onDelayed(delayed) case Skipped(skipped) => onSkipped(skipped) @@ -608,7 +588,7 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { * @param innerPt Expected type that comes from the signature of a macro def, possibly wildcarded to help type inference. */ class DefMacroExpander(typer: Typer, expandee: Tree, mode: Mode, outerPt: Type) - extends MacroExpander(APPLY_ROLE, typer, expandee) { + extends MacroExpander(typer, expandee) { lazy val innerPt = { val tp = if (isNullaryInvocation(expandee)) expandee.tpe.finalResultType else expandee.tpe if (isBlackbox(expandee)) tp @@ -922,12 +902,3 @@ object Fingerprint { val LiftedTyped = new Fingerprint(-2) val LiftedUntyped = new Fingerprint(-3) } - -class MacroRole private[MacroRole](val name: String) extends AnyVal { - override def toString = name -} - -object MacroRole { - val Apply = new MacroRole("apply") - val Unapply = new MacroRole("unapply") -} diff --git a/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala b/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala index 70a3acbb6c..995f98cc2c 100644 --- a/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala +++ b/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala @@ -16,7 +16,7 @@ trait StdAttachments { /** Scratchpad for the macro expander, which is used to store all intermediate data except the details about the runtime. */ - case class MacroExpanderAttachment(original: Tree, desugared: Tree, role: MacroRole) + case class MacroExpanderAttachment(original: Tree, desugared: Tree) /** Loads underlying MacroExpanderAttachment from a macro expandee or returns a default value for that attachment. */ @@ -24,15 +24,15 @@ trait StdAttachments { tree.attachments.get[MacroExpanderAttachment] getOrElse { tree match { case Apply(fn, _) if tree.isInstanceOf[ApplyToImplicitArgs] => macroExpanderAttachment(fn) - case _ => MacroExpanderAttachment(tree, EmptyTree, APPLY_ROLE) + case _ => MacroExpanderAttachment(tree, EmptyTree) } } /** After macro expansion is completed, links the expandee and the expansion result * by annotating them both with a `MacroExpansionAttachment`. */ - def linkExpandeeAndDesugared(expandee: Tree, desugared: Tree, role: MacroRole): Unit = { - val metadata = MacroExpanderAttachment(expandee, desugared, role) + def linkExpandeeAndDesugared(expandee: Tree, desugared: Tree): Unit = { + val metadata = MacroExpanderAttachment(expandee, desugared) expandee updateAttachment metadata desugared updateAttachment metadata } -- cgit v1.2.3