Skip to content

Commit d208198

Browse files
committed
Handle Ranges in dependent annotations
# Conflicts: # compiler/src/dotty/tools/dotc/core/Types.scala
1 parent b3d7433 commit d208198

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5392,6 +5392,17 @@ object Types {
53925392
variance = saved
53935393
derivedLambdaType(tp)(ptypes1, this(restpe))
53945394

5395+
protected def isRange(tp: Type): Boolean = tp.isInstanceOf[Range]
5396+
5397+
private def diffType(ann: Tree): Type =
5398+
val acc = new TreeAccumulator[Type]:
5399+
def apply(x: Type, tree: Tree)(using Context): Type =
5400+
if isRange(x) then x
5401+
else
5402+
val tp1 = thisMap(tree.tpe)
5403+
foldOver(if tp1 =:= tree.tpe then x else tp1, tree)
5404+
acc(NoType, ann)
5405+
53955406
/** Map this function over given type */
53965407
def mapOver(tp: Type): Type = {
53975408
record(s"TypeMap mapOver ${getClass}")
@@ -5435,17 +5446,16 @@ object Types {
54355446

54365447
case tp @ AnnotatedType(underlying, annot) =>
54375448
val underlying1 = this(underlying)
5438-
val annot1 =
5439-
if underlying1 ne underlying then mapOver(annot)
5440-
else annot match
5441-
case ConcreteAnnotation(ann)
5442-
if ann.existsSubTree { t =>
5443-
val tpe = t.typeOpt
5444-
tpe.exists && !(this(tpe) =:= tpe)
5445-
} => mapOver(annot)
5446-
case _ => annot
5447-
if (underlying1 eq underlying) && (annot eq annot1) then tp
5448-
else derivedAnnotatedType(tp, underlying1, annot1)
5449+
if (underlying1 ne underlying) || annot.isInstanceOf[ConcreteAnnotation] then
5450+
val diff = diffType(annot.tree)
5451+
if isRange(diff) then
5452+
// Can't map a tree type to a Range, drop annotation instead
5453+
underlying1
5454+
else
5455+
val annot1 = if diff.exists then mapOver(annot) else annot
5456+
derivedAnnotatedType(tp, underlying1, annot1)
5457+
else
5458+
derivedAnnotatedType(tp, underlying1, annot)
54495459

54505460
case _: ThisType
54515461
| _: BoundType
@@ -5566,8 +5576,6 @@ object Types {
55665576

55675577
protected def emptyRange = range(defn.NothingType, defn.AnyType)
55685578

5569-
protected def isRange(tp: Type): Boolean = tp.isInstanceOf[Range]
5570-
55715579
protected def lower(tp: Type): Type = tp match {
55725580
case tp: Range => tp.lo
55735581
case _ => tp

tests/pos/dependent-annot.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class C
2+
class ann(x: Any*) extends annotation.Annotation
3+
4+
def f(y: C, z: C) =
5+
def g(): C @ann(y, z) = ???
6+
val ac: ((x: C) => Array[String @ann(x)]) = ???
7+
val dc = ac(g())

0 commit comments

Comments
 (0)