@@ -21,7 +21,7 @@ import annotation.constructorOnly
2121import ast .tpd
2222import printing .{Printer , Showable }
2323import printing .Texts .Text
24- import reporting .Message
24+ import reporting .{ Message , trace }
2525import NameOps .isImpureFunction
2626import annotation .internal .sharable
2727
@@ -693,7 +693,7 @@ object Capabilities:
693693 thisMap =>
694694
695695 override def apply (t : Type ) =
696- if variance <= 0 then t
696+ if variance < 0 then t
697697 else t match
698698 case t @ CapturingType (_, _) =>
699699 mapOver(t)
@@ -703,6 +703,8 @@ object Capabilities:
703703 this (CapturingType (parent1, ann.tree.toCaptureSet))
704704 else
705705 t.derivedAnnotatedType(parent1, ann)
706+ case defn.RefinedFunctionOf (_) =>
707+ t // stop at dependent function types
706708 case _ =>
707709 mapFollowingAliases(t)
708710
@@ -784,7 +786,7 @@ object Capabilities:
784786 abstract class CapMap extends BiTypeMap :
785787 override def mapOver (t : Type ): Type = t match
786788 case t @ FunctionOrMethod (args, res) if variance > 0 && ! t.isAliasFun =>
787- t // `t` should be mapped in this case by a different call to `mapCap` .
789+ t // `t` should be mapped in this case by a different call to `toResult`. See [[toResultInResults]] .
788790 case t : (LazyRef | TypeVar ) =>
789791 mapConserveSuper(t)
790792 case _ =>
@@ -849,7 +851,8 @@ object Capabilities:
849851 end toResult
850852
851853 /** Map global roots in function results to result roots. Also,
852- * map roots in the types of parameterless def methods.
854+ * map roots in the types of def methods that are parameterless
855+ * or have only type parameters.
853856 */
854857 def toResultInResults (sym : Symbol , fail : Message => Unit , keepAliases : Boolean = false )(tp : Type )(using Context ): Type =
855858 val m = new TypeMap with FollowAliasesMap :
@@ -878,8 +881,19 @@ object Capabilities:
878881 throw ex
879882 m(tp) match
880883 case tp1 : ExprType if sym.is(Method , butNot = Accessor ) =>
884+ // Map the result of parameterless `def` methods.
881885 tp1.derivedExprType(toResult(tp1.resType, tp1, fail))
886+ case tp1 : PolyType if ! tp1.resType.isInstanceOf [MethodicType ] =>
887+ // Map also the result type of method with only type parameters.
888+ // This way, the `^` in the following method will be mapped to a `ResultCap`:
889+ // ```
890+ // object Buffer:
891+ // def empty[T]: Buffer[T]^
892+ // ```
893+ // This is more desirable than interpreting `^` as a `Fresh` at the level of `Buffer.empty`
894+ // in most cases.
895+ tp1.derivedLambdaType(resType = toResult(tp1.resType, tp1, fail))
882896 case tp1 => tp1
883897 end toResultInResults
884898
885- end Capabilities
899+ end Capabilities
0 commit comments