From 7e0eee211fee8958e93fad646846f1df0405d499 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 17 Dec 2013 10:27:42 +0100 Subject: More robust hyperlink tests for the presentation compiler Report null symbols in an civilised manner, rather than with a NPE. --- .../scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/interactive/scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala b/src/interactive/scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala index bc490d8d45..343986a45d 100644 --- a/src/interactive/scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala +++ b/src/interactive/scala/tools/nsc/interactive/tests/core/CoreTestDefs.scala @@ -94,7 +94,7 @@ private[tests] trait CoreTestDefs askAllSources(HyperlinkMarker) { pos => askTypeAt(pos)(NullReporter) } { (pos, tree) => - if(tree.symbol == compiler.NoSymbol) { + if(tree.symbol == compiler.NoSymbol || tree.symbol == null) { reporter.println("\nNo symbol is associated with tree: "+tree) } else { -- cgit v1.2.3 From d744921f855a6c5d4f4df62895bc3b17b8e0e532 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Wed, 18 Dec 2013 16:55:15 +0100 Subject: SI-8064 Automatic position repair for macro expansion - Replace NoPosition with the focus of the macro application - Focus all range positions, for example, those of spliced arguments --- .../scala/tools/nsc/typechecker/Macros.scala | 26 +++++++++++++++++++++- test/files/pos/t8064.flags | 1 + test/files/pos/t8064/Client_2.scala | 8 +++++++ test/files/pos/t8064/Macro_1.scala | 10 +++++++++ test/files/pos/t8064b.flags | 1 + test/files/pos/t8064b/Client_2.scala | 6 +++++ test/files/pos/t8064b/Macro_1.scala | 11 +++++++++ 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/files/pos/t8064.flags create mode 100644 test/files/pos/t8064/Client_2.scala create mode 100644 test/files/pos/t8064/Macro_1.scala create mode 100644 test/files/pos/t8064b.flags create mode 100644 test/files/pos/t8064b/Client_2.scala create mode 100644 test/files/pos/t8064b/Macro_1.scala (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala index 006ab792fc..c32b986d9c 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala @@ -760,7 +760,31 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { macroLogLite("" + expanded + "\n" + showRaw(expanded)) val freeSyms = expanded.freeTerms ++ expanded.freeTypes freeSyms foreach (sym => MacroFreeSymbolError(expandee, sym)) - Success(atPos(enclosingMacroPosition.focus)(expanded)) + // Macros might have spliced arguments with range positions into non-compliant + // locations, notably, under a tree without a range position. Or, they might + // splice a tree that `resetAttrs` has assigned NoPosition. + // + // Here, we just convert all positions in the tree to offset positions, and + // convert NoPositions to something sensible. + // + // Given that the IDE now sees the expandee (by using -Ymacro-expand:discard), + // this loss of position fidelity shouldn't cause any real problems. + // + // Alternatively, we could pursue a way to exclude macro expansions from position + // invariant checking, or find a way not to touch expansions that happen to validate. + // + // This would be useful for cases like: + // + // macro1 { macro2 { "foo" } } + // + // to allow `macro1` to see the range position of the "foo". + val expandedPos = enclosingMacroPosition.focus + def fixPosition(pos: Position) = + if (pos == NoPosition) expandedPos else pos.focus + expanded.foreach(t => t.pos = fixPosition(t.pos)) + + val result = atPos(enclosingMacroPosition.focus)(expanded) + Success(result) } expanded match { case expanded: Expr[_] if expandee.symbol.isTermMacro => validateResultingTree(expanded.tree) diff --git a/test/files/pos/t8064.flags b/test/files/pos/t8064.flags new file mode 100644 index 0000000000..281f0a10cd --- /dev/null +++ b/test/files/pos/t8064.flags @@ -0,0 +1 @@ +-Yrangepos diff --git a/test/files/pos/t8064/Client_2.scala b/test/files/pos/t8064/Client_2.scala new file mode 100644 index 0000000000..44106782c7 --- /dev/null +++ b/test/files/pos/t8064/Client_2.scala @@ -0,0 +1,8 @@ +object Test { + Macro { + def s = "" + Macro(s): @unchecked + ??? + } +} +// Was: a range position validation error (unpositioned tree) \ No newline at end of file diff --git a/test/files/pos/t8064/Macro_1.scala b/test/files/pos/t8064/Macro_1.scala new file mode 100644 index 0000000000..4fabd54a89 --- /dev/null +++ b/test/files/pos/t8064/Macro_1.scala @@ -0,0 +1,10 @@ +import language.experimental.macros +import scala.reflect.macros.Context + +object Macro { + def apply(a: Any): Any = macro impl + + def impl(c: Context)(a: c.Tree): c.Tree = { + c.resetLocalAttrs(a) + } +} diff --git a/test/files/pos/t8064b.flags b/test/files/pos/t8064b.flags new file mode 100644 index 0000000000..281f0a10cd --- /dev/null +++ b/test/files/pos/t8064b.flags @@ -0,0 +1 @@ +-Yrangepos diff --git a/test/files/pos/t8064b/Client_2.scala b/test/files/pos/t8064b/Client_2.scala new file mode 100644 index 0000000000..a7bf2b9fb4 --- /dev/null +++ b/test/files/pos/t8064b/Client_2.scala @@ -0,0 +1,6 @@ +object Test { + Macro { + "".reverse + } +} +// Was: a range position validation error (tree with offset position enclosing tree with range position) \ No newline at end of file diff --git a/test/files/pos/t8064b/Macro_1.scala b/test/files/pos/t8064b/Macro_1.scala new file mode 100644 index 0000000000..82582356c0 --- /dev/null +++ b/test/files/pos/t8064b/Macro_1.scala @@ -0,0 +1,11 @@ +import language.experimental.macros +import scala.reflect.macros.Context + +object Macro { + def apply(a: Any): Any = macro impl + def impl(c: Context)(a: c.Tree): c.Tree = { + import c.universe._ + + q"{$a; true}" + } +} -- cgit v1.2.3 From 6e4c926b4a4c5e8dd350ae3a150490a794b139ca Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 17 Dec 2013 10:36:26 +0100 Subject: Use macro expandee, rather than expansion, in pres. compiler The presentation compiler is primarily interested in trees that represent the code that one sees in the IDE, not the expansion of macros. This commit continues to expand macros, but adds a hook in which the presentation compiler discards the expansion, retaining instead the expandee. The expandee is attributed with the type of the expansion, which allows white box macros to work. In addition, any domain specific errors and warnings issued by the macro will still be reported, as a side-effect of the expansion. The failing test from the last commit now correctly resolves hyperlinks in macro arguments. Related IDE ticket: https://www.assembla.com/spaces/scala-ide/tickets/1001449# This facility is configured as follows: // expand macros as per normal -Ymacro-expand:normal // don't expand the macro, takes the place of -Ymacro-no-expand -Ymacro-expand:none // expand macros to compute type and emit warnings, // but retain expandee. Set automatically be the presentation // compiler -Ymacro-expand:discard This leaves to door ajar for a new option: // Don't expand blackbox macros; expand whitebox // but retain expandee -Ymacro-expand:discard-whitebox-only The existing test for SI-6812 has been duplicated. One copy exercises the now-deprecated -Ymacro-no-expand, and the other uses the new option. --- .../scala/tools/nsc/settings/ScalaSettings.scala | 8 ++++++- .../scala/tools/nsc/typechecker/Macros.scala | 27 ++++++++++++---------- .../tools/nsc/typechecker/StdAttachments.scala | 2 +- .../scala/tools/nsc/interactive/Global.scala | 4 +++- test/files/presentation/hyperlinks-macro.check | 4 ++-- .../hyperlinks-macro/src/MacroCall.scala | 1 - test/scaladoc/run/SI-6812.check | 1 + test/scaladoc/run/SI-6812.scala | 2 +- test/scaladoc/run/SI-6812b.check | 1 + test/scaladoc/run/SI-6812b.scala | 24 +++++++++++++++++++ 10 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 test/scaladoc/run/SI-6812b.check create mode 100644 test/scaladoc/run/SI-6812b.scala (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala index 7568c789fb..1f9987c83b 100644 --- a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala +++ b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala @@ -172,7 +172,8 @@ trait ScalaSettings extends AbsScalaSettings val Yrangepos = BooleanSetting ("-Yrangepos", "Use range positions for syntax trees.") val Ymemberpos = StringSetting ("-Yshow-member-pos", "output style", "Show start and end positions of members", "") withPostSetHook (_ => Yrangepos.value = true) val Yreifycopypaste = BooleanSetting ("-Yreify-copypaste", "Dump the reified trees in copypasteable representation.") - val Ymacronoexpand = BooleanSetting ("-Ymacro-no-expand", "Don't expand macros. Might be useful for scaladoc and presentation compiler, but will crash anything which uses macros and gets past typer.") + val Ymacroexpand = ChoiceSetting ("-Ymacro-expand", "policy", "Control expansion of macros, useful for scaladoc and presentation compiler", List(MacroExpand.Normal, MacroExpand.None, MacroExpand.Discard), MacroExpand.Normal) + val Ymacronoexpand = BooleanSetting ("-Ymacro-no-expand", "Don't expand macros. Might be useful for scaladoc and presentation compiler, but will crash anything which uses macros and gets past typer.") withDeprecationMessage(s"Use ${Ymacroexpand.name}:${MacroExpand.None}") withPostSetHook(_ => Ymacroexpand.value = MacroExpand.None) val Yreplsync = BooleanSetting ("-Yrepl-sync", "Do not use asynchronous code for repl startup") val Yreplclassbased = BooleanSetting ("-Yrepl-class-based", "Use classes to wrap REPL snippets instead of objects") val Yreploutdir = StringSetting ("-Yrepl-outdir", "path", "Write repl-generated classfiles to given output directory (use \"\" to generate a temporary dir)" , "") @@ -249,4 +250,9 @@ trait ScalaSettings extends AbsScalaSettings def isBCodeAskedFor = (Ybackend.value != "GenASM") def isICodeAskedFor = ((Ybackend.value == "GenASM") || optimiseSettings.exists(_.value) || writeICode.isSetByUser) + object MacroExpand { + val None = "none" + val Normal = "normal" + val Discard = "discard" + } } diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala index c32b986d9c..c1a6ac32c9 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala @@ -535,25 +535,25 @@ 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[Result: ClassTag](val role: MacroRole, val typer: Typer, val expandee: Tree) { + 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: Result): Boolean = true + def allowResult(result: Tree): Boolean = true - def onSuccess(expanded: Tree): Result - def onFallback(expanded: Tree): Result - def onSuppressed(expandee: Tree): Result = expandee match { case expandee: Result => expandee } - def onDelayed(expanded: Tree): Result = expanded match { case expanded: Result => expanded } - def onSkipped(expanded: Tree): Result = expanded match { case expanded: Result => expanded } - def onFailure(expanded: Tree): Result = { typer.infer.setError(expandee); expandee match { case expandee: Result => expandee } } + def onSuccess(expanded: Tree): Tree + def onFallback(expanded: Tree): Tree + def onSuppressed(expandee: Tree): Tree = expandee + def onDelayed(expanded: Tree): Tree = expanded + def onSkipped(expanded: Tree): Tree = expanded + def onFailure(expanded: Tree): Tree = { typer.infer.setError(expandee); expandee } - def apply(desugared: Tree): Result = { + def apply(desugared: Tree): Tree = { if (isMacroExpansionSuppressed(desugared)) onSuppressed(expandee) else expand(desugared) } - protected def expand(desugared: Tree): Result = { + protected def expand(desugared: Tree): Tree = { 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()}") @@ -580,7 +580,10 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers { // 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)) expanded1 else onFailure(expanded) + 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) @@ -605,7 +608,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[Tree](APPLY_ROLE, typer, expandee) { + extends MacroExpander(APPLY_ROLE, typer, expandee) { lazy val innerPt = { val tp = if (isNullaryInvocation(expandee)) expandee.tpe.finalResultType else expandee.tpe if (isBlackbox(expandee)) tp diff --git a/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala b/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala index 14f47a00fd..70a3acbb6c 100644 --- a/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala +++ b/src/compiler/scala/tools/nsc/typechecker/StdAttachments.scala @@ -95,7 +95,7 @@ trait StdAttachments { /** Determines whether a tree should not be expanded, because someone has put SuppressMacroExpansionAttachment on it or one of its children. */ def isMacroExpansionSuppressed(tree: Tree): Boolean = - ( settings.Ymacronoexpand.value // SI-6812 + ( settings.Ymacroexpand.value == settings.MacroExpand.None // SI-6812 || tree.attachments.get[SuppressMacroExpansionAttachment.type].isDefined || (tree match { // we have to account for the fact that during typechecking an expandee might become wrapped, diff --git a/src/interactive/scala/tools/nsc/interactive/Global.scala b/src/interactive/scala/tools/nsc/interactive/Global.scala index 27f10ff00a..b55a573029 100644 --- a/src/interactive/scala/tools/nsc/interactive/Global.scala +++ b/src/interactive/scala/tools/nsc/interactive/Global.scala @@ -88,7 +88,6 @@ trait InteractiveAnalyzer extends Analyzer { } } - /** The main class of the presentation compiler in an interactive environment such as an IDE */ class Global(settings: Settings, _reporter: Reporter, projectName: String = "") extends { @@ -105,6 +104,9 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "") import definitions._ + if (!settings.Ymacroexpand.isSetByUser) + settings.Ymacroexpand.value = settings.MacroExpand.Discard + val debugIDE: Boolean = settings.YpresentationDebug.value val verboseIDE: Boolean = settings.YpresentationVerbose.value diff --git a/test/files/presentation/hyperlinks-macro.check b/test/files/presentation/hyperlinks-macro.check index 39e66d1eca..80d2268fa1 100644 --- a/test/files/presentation/hyperlinks-macro.check +++ b/test/files/presentation/hyperlinks-macro.check @@ -1,8 +1,8 @@ reload: MacroCall.scala -askHyperlinkPos for `` at (5,7) MacroCall.scala +askHyperlinkPos for `foo` at (5,7) MacroCall.scala ================================================================================ -[response] found askHyperlinkPos for `` at (1,13) MacroCall.scala +[response] found askHyperlinkPos for `foo` at (2,7) MacroCall.scala ================================================================================ askHyperlinkPos for `foo` at (9,7) MacroCall.scala diff --git a/test/files/presentation/hyperlinks-macro/src/MacroCall.scala b/test/files/presentation/hyperlinks-macro/src/MacroCall.scala index 42685735b2..d9676b3d2a 100644 --- a/test/files/presentation/hyperlinks-macro/src/MacroCall.scala +++ b/test/files/presentation/hyperlinks-macro/src/MacroCall.scala @@ -9,4 +9,3 @@ object Test { foo/*#*/ } } -// Currently, the hyperlink within the argument to the macro `reify` does not resolve correctly. diff --git a/test/scaladoc/run/SI-6812.check b/test/scaladoc/run/SI-6812.check index 619c56180b..3be8a300e7 100644 --- a/test/scaladoc/run/SI-6812.check +++ b/test/scaladoc/run/SI-6812.check @@ -1 +1,2 @@ +warning: -Ymacro-no-expand is deprecated: Use -Ymacro-expand:none Done. diff --git a/test/scaladoc/run/SI-6812.scala b/test/scaladoc/run/SI-6812.scala index 059c327e7e..6893e816d0 100644 --- a/test/scaladoc/run/SI-6812.scala +++ b/test/scaladoc/run/SI-6812.scala @@ -19,6 +19,6 @@ object Test extends ScaladocModelTest { """ def scaladocSettings = "" - override def extraSettings = super.extraSettings + " -Ymacro-no-expand" + override def extraSettings = super.extraSettings + " -Ymacro-no-expand -deprecation" def testModel(root: Package) = () } diff --git a/test/scaladoc/run/SI-6812b.check b/test/scaladoc/run/SI-6812b.check new file mode 100644 index 0000000000..619c56180b --- /dev/null +++ b/test/scaladoc/run/SI-6812b.check @@ -0,0 +1 @@ +Done. diff --git a/test/scaladoc/run/SI-6812b.scala b/test/scaladoc/run/SI-6812b.scala new file mode 100644 index 0000000000..b8a8140357 --- /dev/null +++ b/test/scaladoc/run/SI-6812b.scala @@ -0,0 +1,24 @@ +import scala.tools.nsc.doc.model._ +import scala.tools.partest.ScaladocModelTest +import language._ + +object Test extends ScaladocModelTest { + + override def code = """ + import scala.reflect.macros.BlackboxContext + import language.experimental.macros + + object Macros { + def impl(c: BlackboxContext) = c.literalUnit + def foo: Unit = macro impl + } + + class C { + def bar = Macros.foo + } + """ + + def scaladocSettings = "" + override def extraSettings = super.extraSettings + " -Ymacro-expand:none" + def testModel(root: Package) = () +} -- cgit v1.2.3 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. --- .../scala/reflect/macros/contexts/Enclosures.scala | 4 -- .../tools/nsc/typechecker/ContextErrors.scala | 4 -- .../scala/tools/nsc/typechecker/Macros.scala | 45 ++++------------------ .../tools/nsc/typechecker/StdAttachments.scala | 8 ++-- src/reflect/scala/reflect/macros/Enclosures.scala | 13 ------- 5 files changed, 12 insertions(+), 62 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/reflect/macros/contexts/Enclosures.scala b/src/compiler/scala/reflect/macros/contexts/Enclosures.scala index bb88c8d5e1..5e931817b5 100644 --- a/src/compiler/scala/reflect/macros/contexts/Enclosures.scala +++ b/src/compiler/scala/reflect/macros/contexts/Enclosures.scala @@ -8,10 +8,6 @@ trait Enclosures { import universe._ - type MacroRole = analyzer.MacroRole - def APPLY_ROLE = analyzer.APPLY_ROLE - def macroRole: MacroRole - private lazy val site = callsiteTyper.context private lazy val enclTrees = site.enclosingContextChain map (_.tree) private lazy val enclPoses = enclosingMacros map (_.macroApplication.pos) filterNot (_ eq NoPosition) 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 } diff --git a/src/reflect/scala/reflect/macros/Enclosures.scala b/src/reflect/scala/reflect/macros/Enclosures.scala index 31905c4739..f3e934d12b 100644 --- a/src/reflect/scala/reflect/macros/Enclosures.scala +++ b/src/reflect/scala/reflect/macros/Enclosures.scala @@ -20,19 +20,6 @@ trait Enclosures { */ def macroApplication: Tree - /** The semantic role that `macroApplication` plays in the code. - */ - type MacroRole - - /** The role that represents an application of a term macro, - * e.g. `M(2)(3)` in `val x = M(2)(3)` or `M(a, b)` in `x match { case x @ M(a, b) => }`. - */ - def APPLY_ROLE: MacroRole - - /** The semantic role that `macroApplication` plays in the code. - */ - def macroRole: MacroRole - /** Contexts that represent macros in-flight, including the current one. Very much like a stack trace, but for macros only. * Can be useful for interoperating with other macros and for imposing compiler-friendly limits on macro expansion. * -- cgit v1.2.3