Skip to content

Commit 941b7f4

Browse files
committed
Some progress towards maintaining position on annotations.
There's still something missing, which might have to be for later. See comment in RefinedPrinter.scala.
1 parent 18fddc1 commit 941b7f4

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/dotty/tools/dotc/core/tasty/PositionPickler.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ class PositionPickler(pickler: TastyPickler, addrsInfo: AddrsInfo) {
6666
val posType = positionedOfIterator.next(x)
6767
//println(s"pickle type pos $posType")
6868
posType.foreachPart {
69-
case t @ AnnotatedType(_, PosAnnot(pos)) => picklePos(t, pos.toSynthetic)
69+
case t @ AnnotatedType(_, annot) =>
70+
annot match {
71+
case PosAnnot(pos) => picklePos(t, pos.toSynthetic)
72+
case _ => traverse(annot.tree)
73+
}
7074
case _ =>
7175
}
7276
}

src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
446446
case _ => String.valueOf(const.value)
447447
}
448448

449-
def toText(annot: Annotation): Text = s"@${annot.symbol.name}" // for now
449+
def toText(annot: Annotation): Text = s"@${annot.symbol.name}" // should be overridden in RefinedPrinter
450450

451451
protected def escapedString(str: String): String = str flatMap escapedChar
452452

src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
4242
case _ => false
4343
}
4444

45+
private var printPos = ctx.settings.Yprintpos.value
46+
4547
override protected def recursionLimitExceeded() = {}
4648

4749
protected val PrintableFlags = (SourceModifierFlags | Label | Module | Local).toCommonFlags
@@ -156,6 +158,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
156158
def blockText[T >: Untyped](trees: List[Tree[T]]): Text =
157159
("{" ~ toText(trees, "\n") ~ "}").close
158160

161+
def constrText(tree: untpd.Tree): Text = toTextLocal(tree).stripPrefix("new ") // DD
162+
163+
// Would like to override this:
164+
// But this leads to tasty position tests failing because we read some
165+
// annotations wihtout positions (curiously, only annootated types in patterns
166+
// seem to be a problem). A failing test is:
167+
//
168+
// test/new/annot.scala
169+
//
170+
// override def toText(annot: Annotation): Text =
171+
// if (annot.tree.isEmpty) annot.toString else constrText(annot.tree)
172+
159173
override def toText[T >: Untyped](tree: Tree[T]): Text = controlled {
160174

161175
import untpd.{modsDeco => _, _}
@@ -206,8 +220,6 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
206220
case untpd.Function(_, tpt) => " <% " ~ toText(tpt)
207221
}
208222

209-
def constrText(tree: untpd.Tree): Text = toTextLocal(tree).stripPrefix("new ") // DD
210-
211223
def annotText(tree: untpd.Tree): Text = "@" ~ constrText(tree) // DD
212224

213225
def useSymbol =
@@ -524,11 +536,20 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
524536
case tp => tp
525537
}
526538
if (tree.isType) txt = toText(tp)
527-
else if (!tree.isDef) txt = ("<" ~ txt ~ ":" ~ toText(tp) ~ ">").close
539+
else if (!tree.isDef) {
540+
val saved = printPos
541+
printPos = false // disable printPos for types that are not explicitly written
542+
try txt = ("<" ~ txt ~ ":" ~ toText(tp) ~ ">").close
543+
finally printPos = saved
544+
}
528545
}
529-
else if (homogenizedView && tree.isType)
546+
else if (homogenizedView && tree.isType) {
530547
txt = toText(tree.typeOpt)
531-
if (ctx.settings.Yprintpos.value && !tree.isInstanceOf[WithoutTypeOrPos[_]]) {
548+
if (printPos && false)
549+
txt = txt ~ "@@" ~
550+
Text(TypePositions.refPositions(tree.asInstanceOf[tpd.Tree]).map(x => Str(x.toString)))
551+
}
552+
if (printPos && !tree.isInstanceOf[WithoutTypeOrPos[_]]) {
532553
val pos =
533554
if (homogenizedView)
534555
tree match {
@@ -537,7 +558,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
537558
case _ => tree.pos.toSynthetic // deserialized position is synthetic
538559
}
539560
else tree.pos
540-
val clsStr = "" // DEBUG: if (tree.isType) tree.getClass.toString else ""
561+
val clsStr = ""//#" + tree.uniqueId // DEBUG: if (tree.isType) tree.getClass.toString else ""
541562
txt = (txt ~ "@" ~ pos.toString ~ clsStr).close
542563
}
543564
tree match {

tests/new/annot.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import annotation.unchecked.uncheckedVariance
2+
import annotation.unchecked.uncheckedVariance
3+
4+
class Test[T] {
5+
val x: T @uncheckedVariance = ???
6+
7+
def f(x: Any) = (??? : T @unchecked) match {
8+
case x: T @ unchecked => ???
9+
}
10+
}

0 commit comments

Comments
 (0)