Skip to content

Commit 8434e57

Browse files
committed
Fix problems handling annotations in dependent function types
Fix problems handling function types that are dependent through their result annotations.
1 parent 00badaa commit 8434e57

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ object Annotations {
6767
else if diff.exists then derivedAnnotation(tm.mapOver(tree))
6868
else this
6969

70+
/** Does this annotation refer to a parameter of `tl`?
71+
* Overridden in ConcreteAnnotation
72+
*/
73+
def refersToParamOf(tl: TermLambda)(using Context): Boolean =
74+
val args = arguments
75+
if args.isEmpty then false
76+
else tree.existsSubTree {
77+
case id: Ident => id.tpe match
78+
case TermParamRef(tl1, _) => tl eq tl1
79+
case _ => false
80+
case _ => false
81+
}
82+
7083
/** A string representation of the annotation. Overridden in BodyAnnotation.
7184
*/
7285
def toText(printer: Printer): Text = printer.annotText(this)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3604,6 +3604,9 @@ object Types {
36043604
case tp: AppliedType => tp.fold(status, compute(_, _, theAcc))
36053605
case tp: TypeVar if !tp.isInstantiated => combine(status, Provisional)
36063606
case tp: TermParamRef if tp.binder eq thisLambdaType => TrueDeps
3607+
case AnnotatedType(parent, ann) =>
3608+
if ann.refersToParamOf(thisLambdaType) then TrueDeps
3609+
else compute(status, parent, theAcc)
36073610
case _: ThisType | _: BoundType | NoPrefix => status
36083611
case _ =>
36093612
(if theAcc != null then theAcc else DepAcc()).foldOver(status, tp)
@@ -3656,8 +3659,10 @@ object Types {
36563659
if (isResultDependent) {
36573660
val dropDependencies = new ApproximatingTypeMap {
36583661
def apply(tp: Type) = tp match {
3659-
case tp @ TermParamRef(thisLambdaType, _) =>
3662+
case tp @ TermParamRef(`thisLambdaType`, _) =>
36603663
range(defn.NothingType, atVariance(1)(apply(tp.underlying)))
3664+
case AnnotatedType(parent, ann) if ann.refersToParamOf(thisLambdaType) =>
3665+
mapOver(parent)
36613666
case _ => mapOver(tp)
36623667
}
36633668
}

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)