Skip to content

Commit 790779a

Browse files
committed
Fixes to positions
- harden addChild to also work if existing children don't have positions (this can happen if they came from other compilation units) - tweak position of Deriver, so that it always points to the deriving class template.
1 parent af94114 commit 790779a

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Namer { typer: Typer =>
196196
val TypedAhead: Property.Key[tpd.Tree] = new Property.Key
197197
val ExpandedTree: Property.Key[untpd.Tree] = new Property.Key
198198
val SymOfTree: Property.Key[Symbol] = new Property.Key
199-
val DerivingCompanion: Property.Key[Unit] = new Property.Key
199+
val DerivingCompanion: Property.Key[Position] = new Property.Key
200200
val Deriver: Property.Key[typer.Deriver] = new Property.Key
201201

202202
/** A partial map from unexpanded member and pattern defs and to their expansions.
@@ -508,7 +508,7 @@ class Namer { typer: Typer =>
508508
val childStart = child.pos.start
509509
def insertInto(annots: List[Annotation]): List[Annotation] =
510510
annots.find(_.symbol == defn.ChildAnnot) match {
511-
case Some(Annotation.Child(other)) if childStart <= other.pos.start =>
511+
case Some(Annotation.Child(other)) if other.pos.exists && childStart <= other.pos.start =>
512512
if (child == other)
513513
annots // can happen if a class has several inaccessible children
514514
else {
@@ -574,7 +574,7 @@ class Namer { typer: Typer =>
574574
if (fromTempl.derived.nonEmpty) {
575575
if (modTempl.derived.nonEmpty)
576576
ctx.error(em"a class and its companion cannot both have `derives' clauses", mdef.pos)
577-
res.putAttachment(DerivingCompanion, ())
577+
res.putAttachment(DerivingCompanion, fromTempl.pos.startPos)
578578
}
579579
res
580580
}
@@ -1012,11 +1012,11 @@ class Namer { typer: Typer =>
10121012
typr.println(i"completing $denot, parents = $parents%, %, parentTypes = $parentTypes%, %")
10131013

10141014
if (impl.derived.nonEmpty) {
1015-
val derivingClass =
1016-
if (original.removeAttachment(DerivingCompanion).isDefined ||
1017-
original.mods.is(Synthetic)) cls.companionClass.asClass
1018-
else cls
1019-
val deriver = new Deriver(derivingClass, impl.pos.startPos)(localCtx)
1015+
val (derivingClass, derivePos) = original.removeAttachment(DerivingCompanion) match {
1016+
case Some(pos) => (cls.companionClass.asClass, pos)
1017+
case None => (cls, impl.pos.startPos)
1018+
}
1019+
val deriver = new Deriver(derivingClass, derivePos)(localCtx)
10201020
deriver.enterDerived(impl.derived)
10211021
original.putAttachment(Deriver, deriver)
10221022
}

compiler/test/dotc/run-from-tasty.blacklist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ typeclass-derivation1.scala
1010
typeclass-derivation2.scala
1111
typeclass-derivation2a.scala
1212
typeclass-derivation3.scala
13+
derive-generic.scala
1314
deriving-interesting-prefixes.scala
1415

compiler/test/dotc/run-test-pickling.blacklist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ typeclass-derivation1.scala
1818
typeclass-derivation2.scala
1919
typeclass-derivation2a.scala
2020
typeclass-derivation3.scala
21+
derive-generic.scala
2122
deriving-interesting-prefixes.scala

tests/neg/deriving.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import reflect.Generic
22

3-
sealed trait A derives Generic // error: cannot take shape, it has anonymous or inaccessible subclasses
3+
sealed trait A derives Generic // error: cannot take shape, it has anonymous or inaccessible subclasses
44

55
object A {
66
def f() = {

0 commit comments

Comments
 (0)