Skip to content

Commit 09c989a

Browse files
committed
Simplify typedDependent
1 parent 9109003 commit 09c989a

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -926,27 +926,21 @@ class Typer extends Namer
926926
isContextual = funFlags.is(Given), isErased = funFlags.is(Erased))
927927

928928
/** Typechecks dependent function type with given parameters `params` */
929-
def typedDependent(params: List[ValDef])(implicit ctx: Context): Tree = {
930-
completeParams(params)
931-
val params1 = params.map(typedExpr(_).asInstanceOf[ValDef])
932-
if (!funFlags.isEmpty)
933-
params1.foreach(_.symbol.setFlag(funFlags))
934-
val resultTpt = typed(body)
935-
val companion = MethodType.companion(
936-
isContextual = funFlags.is(Given), isErased = funFlags.is(Erased))
937-
val mt = companion.fromSymbols(params1.map(_.symbol), resultTpt.tpe)
929+
def typedDependent(params: List[ValDef])(implicit ctx: Context): Tree =
930+
val params1 =
931+
if funFlags.is(Given) then params.map(_.withAddedFlags(Given))
932+
else params
933+
val appDef0 = untpd.DefDef(nme.apply, Nil, List(params1), body, EmptyTree).withSpan(tree.span)
934+
index(appDef0 :: Nil)
935+
val appDef = typed(appDef0).asInstanceOf[DefDef]
936+
val mt = appDef.symbol.info.asInstanceOf[MethodType]
938937
if (mt.isParamDependent)
939938
ctx.error(i"$mt is an illegal function type because it has inter-parameter dependencies", tree.sourcePos)
940939
val resTpt = TypeTree(mt.nonDependentResultApprox).withSpan(body.span)
941-
val typeArgs = params1.map(_.tpt) :+ resTpt
940+
val typeArgs = appDef.vparamss.head.map(_.tpt) :+ resTpt
942941
val tycon = TypeTree(funCls.typeRef)
943-
val core = assignType(cpy.AppliedTypeTree(tree)(tycon, typeArgs), tycon, typeArgs)
944-
val appMeth = ctx.newSymbol(ctx.owner, nme.apply, Synthetic | Method | Deferred, mt, coord = body.span)
945-
val appDef = assignType(
946-
untpd.DefDef(appMeth.name, Nil, List(params1), resultTpt, EmptyTree),
947-
appMeth).withSpan(body.span)
942+
val core = AppliedTypeTree(tycon, typeArgs)
948943
RefinedTypeTree(core, List(appDef), ctx.owner.asClass)
949-
}
950944

951945
args match {
952946
case ValDef(_, _, _) :: _ =>

tests/pos/i8128.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
object Test {
22
def id: (x: 1 | 0) => x.type = x => x
3-
id(0): 0
3+
id(0): 0 // fails
4+
5+
def id2: Function1[1 | 0, 1 | 0] {
6+
def apply(x: 1 | 0): x.type
7+
} = ???
8+
id2(0): 0 // fails
9+
10+
def id3: Function1[1 | 0, Int] {
11+
def apply(x: 1 | 0): x.type
12+
} = ???
13+
id3(0): 0 // ok
414
}

0 commit comments

Comments
 (0)