From 822a29c5098082fc225e6f64343244fe0290fd27 Mon Sep 17 00:00:00 2001 From: odersky Date: Sun, 19 Jan 2025 20:03:10 +0100 Subject: [PATCH] Fix mapping of annotations We previously did not do an (expensive) TreeTypeMap on an annotation if the mapped versions of all types of subtrees of the annotation tree were =:= to the original types. But it turns out this is too coarse. In the test we have capture set variables where we intend to map a TypeRef to a TypeParamRef but the two were considered as =:= because of the bounds they had. So no mapping took place. We now use `eql` instead of =:=, which is structural comparison with `eq` for references to corresponding binders. [Cherry-picked 6d6da118ac15b16dc58e35ce85127f468e7c4674] --- compiler/src/dotty/tools/dotc/core/Annotations.scala | 2 +- compiler/src/dotty/tools/dotc/core/Types.scala | 2 ++ tests/pos-custom-args/captures/setup/a_1.scala | 6 ++++++ tests/pos-custom-args/captures/setup/b_1.scala | 5 +++++ tests/pos-custom-args/captures/setup/b_2.scala | 5 +++++ 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/pos-custom-args/captures/setup/a_1.scala create mode 100644 tests/pos-custom-args/captures/setup/b_1.scala create mode 100644 tests/pos-custom-args/captures/setup/b_2.scala diff --git a/compiler/src/dotty/tools/dotc/core/Annotations.scala b/compiler/src/dotty/tools/dotc/core/Annotations.scala index f5825df84a8b..0706003f533e 100644 --- a/compiler/src/dotty/tools/dotc/core/Annotations.scala +++ b/compiler/src/dotty/tools/dotc/core/Annotations.scala @@ -65,7 +65,7 @@ object Annotations { if tm.isRange(x) then x else val tp1 = tm(tree.tpe) - foldOver(if !tp1.exists || (tp1 frozen_=:= tree.tpe) then x else tp1, tree) + foldOver(if !tp1.exists || tp1.eql(tree.tpe) then x else tp1, tree) val diff = findDiff(NoType, args) if tm.isRange(diff) then EmptyAnnotation else if diff.exists then derivedAnnotation(tm.mapOver(tree)) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 5fe6c3174c04..ac6089516571 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -5967,6 +5967,8 @@ object Types extends TypeUtils { override def stopAt = thisMap.stopAt def apply(tp: Type) = f(thisMap(tp)) } + + override def toString = s"${getClass.getSimpleName}@$hashCode" // otherwise would print as } /** A type map that maps also parents and self type of a ClassInfo */ diff --git a/tests/pos-custom-args/captures/setup/a_1.scala b/tests/pos-custom-args/captures/setup/a_1.scala new file mode 100644 index 000000000000..21afde8be3ea --- /dev/null +++ b/tests/pos-custom-args/captures/setup/a_1.scala @@ -0,0 +1,6 @@ +// a.scala +import language.experimental.captureChecking +import scala.caps.CapSet + +trait A: + def f[C^](x: AnyRef^{C^}): Unit diff --git a/tests/pos-custom-args/captures/setup/b_1.scala b/tests/pos-custom-args/captures/setup/b_1.scala new file mode 100644 index 000000000000..d5ba925970ba --- /dev/null +++ b/tests/pos-custom-args/captures/setup/b_1.scala @@ -0,0 +1,5 @@ +import language.experimental.captureChecking +import scala.caps.CapSet + +class B extends A: + def f[C^](x: AnyRef^{C^}): Unit = ??? diff --git a/tests/pos-custom-args/captures/setup/b_2.scala b/tests/pos-custom-args/captures/setup/b_2.scala new file mode 100644 index 000000000000..d5ba925970ba --- /dev/null +++ b/tests/pos-custom-args/captures/setup/b_2.scala @@ -0,0 +1,5 @@ +import language.experimental.captureChecking +import scala.caps.CapSet + +class B extends A: + def f[C^](x: AnyRef^{C^}): Unit = ???