Skip to content

Commit

Permalink
Fix: Use cap instead of Fresh.Cap for parameter types of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Dec 21, 2024
1 parent 4b37a9a commit daeae71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 13 additions & 1 deletion compiler/src/dotty/tools/dotc/cc/Fresh.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ object Fresh:

lazy val inverse = new BiTypeMap:
def apply(t: Type): Type = t match
case t @ Cap(_) => defn.captureRoot.termRef
case t @ Cap(_) =>
change = true
defn.captureRoot.termRef
case _ => mapOver(t)
def inverse = thisMap
override def toString = thisMap.toString + ".inverse"
Expand All @@ -104,6 +106,16 @@ object Fresh:
if mapper.change then mapped else tp
else
tp

/** Maps fresh to cap */
def toCap(tp: Type)(using Context): Type =
if ccConfig.useFresh then
val fromCap = FromCap(NoSymbol)
val mapper = fromCap.inverse
val mapped = mapper(tp)
if fromCap.change then mapped else tp
else
tp
end Fresh


Expand Down
18 changes: 13 additions & 5 deletions compiler/src/dotty/tools/dotc/cc/Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,20 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
def setupTraverser(checker: CheckerAPI) = new TreeTraverserWithPreciseImportContexts:
import checker.*

private val paramSigChange = util.EqHashSet[Tree]()

/** Transform type of tree, and remember the transformed type as the type the tree */
private def transformTT(tree: TypeTree, sym: Symbol, boxed: Boolean)(using Context): Unit =
if !tree.hasNuType then
val transformed =
var transformed =
if tree.isInferred
then transformInferredType(tree.tpe)
else transformExplicitType(tree.tpe, tptToCheck = tree)
if boxed then transformed = box(transformed)
if sym.is(Param) && (transformed ne tree.tpe) then
paramSigChange += tree
tree.setNuType(
if boxed then box(transformed) else Fresh.fromCap(transformed, sym))
if boxed then transformed else Fresh.fromCap(transformed, sym))

/** Transform the type of a val or var or the result type of a def */
def transformResultType(tpt: TypeTree, sym: Symbol)(using Context): Unit =
Expand Down Expand Up @@ -504,6 +509,9 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
inContext(ctx.withOwner(sym))
traverseChildren(tree)

case tree @ TypeDef(_, rhs: TypeTree) =>
transformTT(rhs, tree.symbol, boxed = false)

case tree @ SeqLiteral(elems, tpt: TypeTree) =>
traverse(elems)
tpt.setNuType(box(transformInferredType(tpt.tpe)))
Expand Down Expand Up @@ -547,8 +555,8 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
def paramSignatureChanges = tree.match
case tree: DefDef =>
tree.paramss.nestedExists:
case param: ValDef => param.tpt.hasNuType
case param: TypeDef => param.rhs.hasNuType
case param: ValDef => paramSigChange.contains(param.tpt)
case param: TypeDef => paramSigChange.contains(param.rhs)
case _ => false

// A symbol's signature changes if some of its parameter types or its result type
Expand Down Expand Up @@ -583,7 +591,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
mt.paramInfos
else
val subst = SubstParams(psyms :: prevPsymss, mt1 :: prevLambdas)
psyms.map(psym => adaptedInfo(psym, subst(psym.nextInfo).asInstanceOf[mt.PInfo])),
psyms.map(psym => adaptedInfo(psym, subst(Fresh.toCap(psym.nextInfo)).asInstanceOf[mt.PInfo])),
mt1 =>
integrateRT(mt.resType, psymss.tail, resType, psyms :: prevPsymss, mt1 :: prevLambdas)
)
Expand Down

0 comments on commit daeae71

Please sign in to comment.