Skip to content

Commit 0ee4c20

Browse files
committed
Refactoring: pass an optional argument tree to safeSubstParam
1 parent 7b9f0cb commit 0ee4c20

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,10 @@ object SpaceEngine {
570570
// Case unapplySeq:
571571
// 1. return the type `List[T]` where `T` is the element type of the unapplySeq return type `Seq[T]`
572572

573-
val resTp = wildApprox(ctx.typeAssigner.safeSubstMethodParams(mt, scrutineeTp :: Nil).finalResultType)
573+
var resTp0 = mt.resultType
574+
if mt.isResultDependent then
575+
resTp0 = ctx.typeAssigner.safeSubstParam(resTp0, mt.paramRefs.head, scrutineeTp)
576+
val resTp = wildApprox(resTp0).finalResultType
574577

575578
val sig =
576579
if (resTp.isRef(defn.BooleanClass))

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import collection.mutable
1212
import reporting.*
1313
import Checking.{checkNoPrivateLeaks, checkNoWildcard}
1414
import cc.CaptureSet
15+
import util.Property
1516
import transform.Splicer
1617

1718
trait TypeAssigner {
@@ -273,33 +274,32 @@ trait TypeAssigner {
273274
/** Substitute argument type `argType` for parameter `pref` in type `tp`,
274275
* skolemizing the argument type if it is not stable and `pref` occurs in `tp`.
275276
*/
276-
def safeSubstParam(tp: Type, pref: ParamRef, argType: Type)(using Context): Type = {
277+
def safeSubstParam(tp: Type, pref: ParamRef, argType: Type, arg: Tree | Null = null)(using Context): Type = {
277278
val tp1 = tp.substParam(pref, argType)
278-
if ((tp1 eq tp) || argType.isStable) tp1
279+
if (tp1 eq tp) || argType.isStable then tp1
279280
else tp.substParam(pref, SkolemType(argType.widen))
280281
}
281282

282283
/** Substitute types of all arguments `args` for corresponding `params` in `tp`.
283284
* The number of parameters `params` may exceed the number of arguments.
284285
* In this case, only the common prefix is substituted.
285286
*/
286-
def safeSubstParams(tp: Type, params: List[ParamRef], argTypes: List[Type])(using Context): Type = argTypes match {
287-
case argType :: argTypes1 =>
288-
val tp1 = safeSubstParam(tp, params.head, argType)
289-
safeSubstParams(tp1, params.tail, argTypes1)
287+
def safeSubstParams(tp: Type, params: List[ParamRef], args: List[Tree])(using Context): Type = args match
288+
case arg :: args1 =>
289+
val tp1 = safeSubstParam(tp, params.head, arg.tpe, arg)
290+
safeSubstParams(tp1, params.tail, args1)
290291
case Nil =>
291292
tp
292-
}
293293

294-
def safeSubstMethodParams(mt: MethodType, argTypes: List[Type])(using Context): Type =
295-
if mt.isResultDependent then safeSubstParams(mt.resultType, mt.paramRefs, argTypes)
294+
def safeSubstMethodParams(mt: MethodType, args: List[Tree])(using Context): Type =
295+
if mt.isResultDependent then safeSubstParams(mt.resultType, mt.paramRefs, args)
296296
else mt.resultType
297297

298298
def assignType(tree: untpd.Apply, fn: Tree, args: List[Tree])(using Context): Apply = {
299299
val ownType = fn.tpe.widen match {
300300
case fntpe: MethodType =>
301301
if fntpe.paramInfos.hasSameLengthAs(args) || ctx.phase.prev.relaxedTyping then
302-
if fntpe.isResultDependent then safeSubstMethodParams(fntpe, args.tpes)
302+
if fntpe.isResultDependent then safeSubstMethodParams(fntpe, args)
303303
else fntpe.resultType // fast path optimization
304304
else
305305
val erroringPhase =
@@ -570,6 +570,12 @@ trait TypeAssigner {
570570
}
571571

572572
object TypeAssigner extends TypeAssigner:
573+
574+
/** An attachment on an argument in an application indicating that the argument's
575+
* type was converted to the given skolem type.
576+
*/
577+
private[typer] val Skolemized = new Property.StickyKey[SkolemType]
578+
573579
def seqLitType(tree: untpd.SeqLiteral, elemType: Type)(using Context) = tree match
574580
case tree: untpd.JavaSeqLiteral => defn.ArrayOf(elemType)
575581
case _ => if ctx.erasedTypes then defn.SeqType else defn.SeqType.appliedTo(elemType)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4355,7 +4355,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
43554355
def inferArgsAfter(leading: Tree) =
43564356
val formals2 =
43574357
if wtp.isParamDependent && leading.tpe.exists then
4358-
formals1.mapconserve(f1 => safeSubstParam(f1, wtp.paramRefs(argIndex), leading.tpe))
4358+
formals1.mapconserve(f1 => safeSubstParam(f1, wtp.paramRefs(argIndex), leading.tpe, leading))
43594359
else formals1
43604360
implicitArgs(formals2, argIndex + 1, pt)
43614361

0 commit comments

Comments
 (0)