@@ -167,28 +167,6 @@ class TreeChecker extends Phase with SymTransformer {
167
167
}
168
168
169
169
object TreeChecker {
170
- /** - Check that TypeParamRefs and MethodParams refer to an enclosing type.
171
- * - Check that all type variables are instantiated.
172
- */
173
- def checkNoOrphans (tp0 : Type , tree : untpd.Tree = untpd.EmptyTree )(using Context ): Type = new TypeMap () {
174
- val definedBinders = new java.util.IdentityHashMap [Type , Any ]
175
- def apply (tp : Type ): Type = {
176
- tp match {
177
- case tp : BindingType =>
178
- definedBinders.put(tp, tp)
179
- mapOver(tp)
180
- definedBinders.remove(tp)
181
- case tp : ParamRef =>
182
- assert(definedBinders.get(tp.binder) != null , s " orphan param: ${tp.show}, hash of binder = ${System .identityHashCode(tp.binder)}, tree = ${tree.show}, type = $tp0" )
183
- case tp : TypeVar =>
184
- assert(tp.isInstantiated, s " Uninstantiated type variable: ${tp.show}, tree = ${tree.show}" )
185
- apply(tp.underlying)
186
- case _ =>
187
- mapOver(tp)
188
- }
189
- tp
190
- }
191
- }.apply(tp0)
192
170
193
171
/** Run some additional checks on the nodes of the trees. Specifically:
194
172
*
@@ -219,7 +197,7 @@ object TreeChecker {
219
197
class Checker (phasesToCheck : Seq [Phase ]) extends ReTyper with Checking {
220
198
import ast .tpd ._
221
199
222
- private val nowDefinedSyms = util.HashSet [Symbol ]()
200
+ protected val nowDefinedSyms = util.HashSet [Symbol ]()
223
201
private val patBoundSyms = util.HashSet [Symbol ]()
224
202
private val everDefinedSyms = MutableSymbolMap [untpd.Tree ]()
225
203
@@ -723,5 +701,72 @@ object TreeChecker {
723
701
}
724
702
725
703
override def simplify (tree : Tree , pt : Type , locked : TypeVars )(using Context ): tree.type = tree
704
+
705
+ /** - Check that TypeParamRefs and MethodParams refer to an enclosing type.
706
+ * - Check that all type variables are instantiated.
707
+ */
708
+ def checkNoOrphans (tp0 : Type , tree : untpd.Tree = untpd.EmptyTree )(using Context ): Type = new TypeMap () {
709
+ val definedBinders = new java.util.IdentityHashMap [Type , Any ]
710
+ def apply (tp : Type ): Type = {
711
+ tp match {
712
+ case tp : BindingType =>
713
+ definedBinders.put(tp, tp)
714
+ mapOver(tp)
715
+ definedBinders.remove(tp)
716
+ case tp : ParamRef =>
717
+ assert(definedBinders.get(tp.binder) != null , s " orphan param: ${tp.show}, hash of binder = ${System .identityHashCode(tp.binder)}, tree = ${tree.show}, type = $tp0" )
718
+ case tp : TypeVar =>
719
+ assert(tp.isInstantiated, s " Uninstantiated type variable: ${tp.show}, tree = ${tree.show}" )
720
+ apply(tp.underlying)
721
+ case _ =>
722
+ mapOver(tp)
723
+ }
724
+ tp
725
+ }
726
+ }.apply(tp0)
726
727
}
728
+
729
+ /** Tree checker that can be applied to a local tree. */
730
+ class LocalChecker (phasesToCheck : Seq [Phase ]) extends Checker (phasesToCheck : Seq [Phase ]) {
731
+ override def assertDefined (tree : untpd.Tree )(using Context ): Unit =
732
+ // Only check definitions nested in the local tree
733
+ if nowDefinedSyms.contains(tree.symbol.maybeOwner) then
734
+ super .assertDefined(tree)
735
+
736
+ override def checkNoOrphans (tp0 : Type , tree : untpd.Tree = untpd.EmptyTree )(using Context ): Type =
737
+ // TODO: Is this fails on tests/run-macros/quoted-ToExpr-derivation-macro.
738
+ // Is this a bug?
739
+ tp0 // Do not check for orphans
740
+ }
741
+
742
+ def checkMacroGeneratedTree (original : tpd.Tree , expansion : tpd.Tree )(using Context ): Unit =
743
+ if ctx.settings.XcheckMacros .value then
744
+ val checkingCtx = ctx
745
+ .fresh
746
+ .addMode(Mode .ImplicitsEnabled )
747
+ .setReporter(new ThrowingReporter (ctx.reporter))
748
+
749
+ val treeChecker =
750
+ new LocalChecker (Nil ) // TODO enable previous phase post-conditions
751
+
752
+ try treeChecker.typed(expansion)(using checkingCtx)
753
+ catch
754
+ case err : java.lang.AssertionError =>
755
+ report.error(
756
+ em """ Malformed tree was found while expanding macro with -Xcheck-macros.
757
+ |The tree does not conform to the compiler's tree invariants.
758
+ |
759
+ |Macro was:
760
+ | ${original}
761
+ |
762
+ |The macro returned:
763
+ | ${expansion}
764
+ |
765
+ |Error:
766
+ | ${err.getMessage}
767
+ |
768
+ | """ ,
769
+ original
770
+ )
771
+
727
772
}
0 commit comments