Skip to content

Commit

Permalink
Cleanup retains annotations in all inferred type trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyxus committed Apr 30, 2024
1 parent 48aac2c commit 60f98a5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
16 changes: 7 additions & 9 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,9 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
// during the typer, it is infeasible to correctly infer the capture sets in most
// cases, resulting ill-formed capture sets that could crash the pickler later on.
// See #20035.
private def cleanupRetainsAnnot(symbol: Symbol, tpt: Tree)(using Context): Tree =
private def cleanupRetainsAnnot(tpt: Tree)(using Context): Tree =
tpt match
case tpt: InferredTypeTree
if !symbol.allOverriddenSymbols.hasNext =>
// if there are overridden symbols, the annotation comes from an explicit type of the overridden symbol
// and should be retained.
case tpt: InferredTypeTree =>
val tm = new CleanupRetains
val tpe1 = tm(tpt.tpe)
tpt.withType(tpe1)
Expand Down Expand Up @@ -421,7 +418,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
registerIfHasMacroAnnotations(tree)
checkErasedDef(tree)
Checking.checkPolyFunctionType(tree.tpt)
val tree1 = cpy.ValDef(tree)(tpt = cleanupRetainsAnnot(tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
val tree1 = cpy.ValDef(tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
if tree1.removeAttachment(desugar.UntupledParam).isDefined then
checkStableSelection(tree.rhs)
processValOrDefDef(super.transform(tree1))
Expand All @@ -431,7 +428,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
checkErasedDef(tree)
Checking.checkPolyFunctionType(tree.tpt)
annotateContextResults(tree)
val tree1 = cpy.DefDef(tree)(tpt = cleanupRetainsAnnot(tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
val tree1 = cpy.DefDef(tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
processValOrDefDef(superAcc.wrapDefDef(tree1)(super.transform(tree1).asInstanceOf[DefDef]))
case tree: TypeDef =>
registerIfHasMacroAnnotations(tree)
Expand Down Expand Up @@ -504,8 +501,9 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
report.error(em"type ${alias.tpe} outside bounds $bounds", tree.srcPos)
super.transform(tree)
case tree: TypeTree =>
tree.withType(
tree.tpe match {
val tree1 = cleanupRetainsAnnot(tree)
tree1.withType(
tree1.tpe match {
case AnnotatedType(tpe, annot) => AnnotatedType(tpe, transformAnnot(annot))
case tpe => tpe
}
Expand Down
20 changes: 20 additions & 0 deletions tests/pos-custom-args/captures/i20272a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import language.experimental.captureChecking

trait Iterable[T] { self: Iterable[T]^ =>
def map[U](f: T => U): Iterable[U]^{this, f}
}

object Test {
def assertEquals[A, B](a: A, b: B): Boolean = ???

def foo[T](level: Int, lines: Iterable[T]) =
lines.map(x => x)

def bar(messages: Iterable[String]) =
foo(1, messages)

val it: Iterable[String] = ???
val msgs = bar(it)

assertEquals(msgs, msgs)
}
16 changes: 16 additions & 0 deletions tests/pos-custom-args/captures/i20272b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import language.experimental.captureChecking

trait Iterable[T] { self: Iterable[T]^ =>
def map[U](f: T => U): Iterable[U]^{this, f}
}

object Test {
def foo[T](level: Int, lines: Iterable[T]) =
lines.map(x => x)

class Bar:
def bar(messages: Iterable[String]) =
foo(1, messages)
class Baz extends Bar:
override def bar(messages: Iterable[String]) = ???
}

0 comments on commit 60f98a5

Please sign in to comment.