From 5439c4c9aa9ef03c061fa9c2c4689a722729b8f9 Mon Sep 17 00:00:00 2001 From: Den Shabalin Date: Thu, 8 Aug 2013 18:53:42 +0200 Subject: SI-7731 make CannotHaveAttrs more consistent Previously setPos, pos_=, setType, tpe_= all behaved inconsistently between each other even though they all represent similar attributes that cannot be changed on CannotHaveAttrs trees. In order to simplify handling of such trees in compiler code each of these fields now supports assignment to its current default value: NoType for tpe and NoPosition for pos. Such assignments don't mutate underlying trees. --- .../tools/nsc/symtab/CannotHaveAttrsTest.scala | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala (limited to 'test') diff --git a/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala b/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala new file mode 100644 index 0000000000..5867a9030b --- /dev/null +++ b/test/junit/scala/tools/nsc/symtab/CannotHaveAttrsTest.scala @@ -0,0 +1,70 @@ +package scala.tools.nsc +package symtab + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.tools.testing.AssertUtil.assertThrows +import scala.reflect.internal.util.OffsetPosition + +@RunWith(classOf[JUnit4]) +class CannotHaveAttrsTest { + class CustomSymbolTable extends SymbolTableForUnitTesting { + object CHA extends CannotHaveAttrs { + def canEqual(that: Any): Boolean = ??? + def productArity: Int = ??? + def productElement(n: Int): Any = ??? + } + val attrlessTrees = List(CHA, EmptyTree, emptyValDef, pendingSuperCall) + } + def withCtx(body: CustomSymbolTable => Unit) = body(new CustomSymbolTable) + + @Test + def canHaveAttrsIsFalse = withCtx { st => import st._ + attrlessTrees.foreach { t => + assertFalse(t.canHaveAttrs) + } + } + + @Test + def defaultPosAssignment = withCtx { st => import st._ + attrlessTrees.foreach { t => + assertEquals(t.pos, NoPosition) + t.pos = NoPosition + assertEquals(t.pos, NoPosition) + t.setPos(NoPosition) + assertEquals(t.pos, NoPosition) + } + } + + @Test + def defaultTpeAssignment = withCtx { st => import st._ + attrlessTrees.foreach { t => + assertEquals(t.tpe, NoType) + t.tpe = NoType + assertEquals(t.tpe, NoType) + t.setType(NoType) + assertEquals(t.tpe, NoType) + } + } + + @Test + def nonDefaultPosAssignmentFails = withCtx { st => import st._ + val pos = new OffsetPosition(null, 0) + attrlessTrees.foreach { t => + assertThrows[IllegalArgumentException] { t.pos = pos } + assertThrows[IllegalArgumentException] { t.setPos(pos) } + } + } + + @Test + def nonDefaultTpeAssignmentFails = withCtx { st => import st._ + val tpe = typeOf[Int] + attrlessTrees.foreach { t => + assertThrows[IllegalArgumentException] { t.tpe = tpe } + assertThrows[IllegalArgumentException] { t.setType(tpe) } + } + } +} -- cgit v1.2.3