Skip to content

Fix #5526: Better type inference for dependent function types #5631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,9 @@ class Namer { typer: Typer =>
case TypeTree() =>
checkMembersOK(inferredType, mdef.pos)
case DependentTypeTree(tpFun) =>
tpFun(paramss.head)
val tpe = tpFun(paramss.head)
if (isFullyDefined(tpe, ForceDegree.none)) tpe
else typedAheadExpr(mdef.rhs, tpe).tpe
case TypedSplice(tpt: TypeTree) if !isFullyDefined(tpt.tpe, ForceDegree.none) =>
val rhsType = typedAheadExpr(mdef.rhs, tpt.tpe).tpe
mdef match {
Expand Down
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,10 @@ class Typer extends Namer
val typeArgs = params1.map(_.tpt) :+ resTpt
val tycon = TypeTree(funCls.typeRef)
val core = assignType(cpy.AppliedTypeTree(tree)(tycon, typeArgs), tycon, typeArgs)
val appMeth = ctx.newSymbol(ctx.owner, nme.apply, Synthetic | Deferred, mt)
val appMeth = ctx.newSymbol(ctx.owner, nme.apply, Synthetic | Method | Deferred, mt, coord = body.pos)
val appDef = assignType(
untpd.DefDef(appMeth.name, Nil, List(params1), resultTpt, EmptyTree),
appMeth)
appMeth).withPos(body.pos)
RefinedTypeTree(core, List(appDef), ctx.owner.asClass)
}

Expand Down Expand Up @@ -1233,7 +1233,9 @@ class Typer extends Namer
}
case _ =>
tree.withType(
if (isFullyDefined(pt, ForceDegree.none)) pt else UnspecifiedErrorType)
if (isFullyDefined(pt, ForceDegree.noBottom)) pt
else if (ctx.reporter.errorsReported) UnspecifiedErrorType
else errorType(i"cannot infer type; expected type $pt is not fully defined", tree.pos))
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i5526a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trait A
object test1 {
def foo[E](f: (a: A) => (a.type, E)): E = {
val a = new A {}
f(a)._2
}
val res = foo { a => (a, 42) }
val x: Int = res
}
object test2 {
trait F[A, -E]
def empty[A](value: A): F[A, Any] = ???
def hof[R](f: (p: AnyRef) => F[R, p.type]): F[R, Any] = ???
hof { p =>
empty(42)
}
}
10 changes: 10 additions & 0 deletions tests/pos/i5526b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
trait F[A, -E]
object Test {
def empty[A](value: A): F[A, Any] = ???

def hof[R](f: (p: AnyRef) => F[R, p.type]): F[R, Any] = ???

hof { p =>
empty(42)
}
}