Skip to content

Commit a2e7b73

Browse files
Merge pull request #9540 from dotty-staging/fix-#9464
Fix #9464: Fix computation of expected type in a return
2 parents 28069b6 + a24a370 commit a2e7b73

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,17 +1473,21 @@ class Typer extends Namer
14731473
instantiateCFT(appType.instantiate(paramss.head.map(_.termRef)), paramss.tail)
14741474
else pt
14751475

1476-
def returnProto(owner: Symbol, locals: Scope): Type =
1476+
def returnProto(owner: Symbol): Type =
14771477
if (owner.isConstructor) defn.UnitType
14781478
else
1479-
val rt = owner.info match
1479+
// We need to get the return type of the enclosing function, with all parameters replaced
1480+
// by the local type and value parameters. It would be nice if we could look up that
1481+
// type simply in the tpt field of the enclosing function. But the tree argument in
1482+
// a context is an untyped tree, so we cannot extract its type.
1483+
def instantiateRT(info: Type, psymss: List[List[Symbol]]): Type = info match
14801484
case info: PolyType =>
1481-
val tparams = locals.toList.takeWhile(_ is TypeParam)
1482-
assert(info.paramNames.length == tparams.length,
1483-
i"return mismatch from $owner, tparams = $tparams, locals = ${locals.toList}%, %")
1484-
info.instantiate(tparams.map(_.typeRef)).finalResultType
1485+
instantiateRT(info.instantiate(psymss.head.map(_.typeRef)), psymss.tail)
1486+
case info: MethodType =>
1487+
instantiateRT(info.instantiate(psymss.head.map(_.termRef)), psymss.tail)
14851488
case info =>
1486-
info.finalResultType
1489+
info.widenExpr
1490+
val rt = instantiateRT(owner.info, owner.paramSymss)
14871491
def iftParamss = ctx.owner.ownersIterator
14881492
.filter(_.is(Method, butNot = Accessor))
14891493
.takeWhile(_.isAnonymousFunction)
@@ -1505,7 +1509,7 @@ class Typer extends Namer
15051509
(EmptyTree, errorType(MissingReturnTypeWithReturnStatement(owner), tree.sourcePos))
15061510
else {
15071511
val from = Ident(TermRef(NoPrefix, owner.asTerm))
1508-
val proto = returnProto(owner, cx.scope)
1512+
val proto = returnProto(owner)
15091513
(from, proto)
15101514
}
15111515
else enclMethInfo(cx.outer)

tests/pos/i9464.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait T:
2+
type X
3+
def x: X
4+
5+
def test1(t: T): t.X = t.x
6+
def test2(t: T): t.X = return t.x

0 commit comments

Comments
 (0)