Skip to content

Fix #9464: Fix computation of expected type in a return #9540

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 1 commit into from
Aug 13, 2020
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
20 changes: 12 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1473,17 +1473,21 @@ class Typer extends Namer
instantiateCFT(appType.instantiate(paramss.head.map(_.termRef)), paramss.tail)
else pt

def returnProto(owner: Symbol, locals: Scope): Type =
def returnProto(owner: Symbol): Type =
if (owner.isConstructor) defn.UnitType
else
val rt = owner.info match
// We need to get the return type of the enclosing function, with all parameters replaced
// by the local type and value parameters. It would be nice if we could look up that
// type simply in the tpt field of the enclosing function. But the tree argument in
// a context is an untyped tree, so we cannot extract its type.
def instantiateRT(info: Type, psymss: List[List[Symbol]]): Type = info match
case info: PolyType =>
val tparams = locals.toList.takeWhile(_ is TypeParam)
assert(info.paramNames.length == tparams.length,
i"return mismatch from $owner, tparams = $tparams, locals = ${locals.toList}%, %")
info.instantiate(tparams.map(_.typeRef)).finalResultType
instantiateRT(info.instantiate(psymss.head.map(_.typeRef)), psymss.tail)
case info: MethodType =>
instantiateRT(info.instantiate(psymss.head.map(_.termRef)), psymss.tail)
case info =>
info.finalResultType
info.widenExpr
val rt = instantiateRT(owner.info, owner.paramSymss)
def iftParamss = ctx.owner.ownersIterator
.filter(_.is(Method, butNot = Accessor))
.takeWhile(_.isAnonymousFunction)
Expand All @@ -1505,7 +1509,7 @@ class Typer extends Namer
(EmptyTree, errorType(MissingReturnTypeWithReturnStatement(owner), tree.sourcePos))
else {
val from = Ident(TermRef(NoPrefix, owner.asTerm))
val proto = returnProto(owner, cx.scope)
val proto = returnProto(owner)
(from, proto)
}
else enclMethInfo(cx.outer)
Expand Down
6 changes: 6 additions & 0 deletions tests/pos/i9464.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait T:
type X
def x: X

def test1(t: T): t.X = t.x
def test2(t: T): t.X = return t.x