Skip to content

Commit d59494e

Browse files
committed
Infer result type for vals, like we do for defs
The lack of result type inference caused pos/t6780 to fail in the new field encoding for traits, as there is no separate accessor, and method synthesis computes the type signature based on the ValDef tree. This caused a cyclic error in implicit search, because now the implicit val's result type was not inferred from the super member, and inferring it from the RHS would cause implicit search to consider the member in question, so that a cycle is detected and type checking fails...
1 parent 4336eb1 commit d59494e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/compiler/scala/tools/nsc/typechecker/Namers.scala

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,12 +1376,31 @@ trait Namers extends MethodSynthesis {
13761376
MissingParameterOrValTypeError(tpt)
13771377
ErrorType
13781378
}
1379-
else assignTypeToTree(vdef, typer, WildcardType)
1379+
else {
1380+
val valOwner = owner.owner
1381+
val pt =
1382+
// there's no overriding outside of classes
1383+
if (!valOwner.isClass) WildcardType
1384+
else {
1385+
// Pretend we're an erroneous symbol, for now, so that we match while finding the overridden symbol,
1386+
// but are not considered during implicit search.
1387+
// The symbol's info is currently being determined (up the call stack, you'll find a TypeCompleter's complete method),
1388+
// so the info will be set to whatever type we return here by the complete method.
1389+
val saved = vdef.symbol.rawInfo
1390+
try {
1391+
vdef.symbol setInfo ErrorType
1392+
val overridden = vdef.symbol.nextOverriddenSymbol
1393+
if (overridden == NoSymbol || overridden.isOverloaded) WildcardType
1394+
else valOwner.thisType.memberType(overridden).resultType
1395+
} finally vdef.symbol.setInfo(saved)
1396+
}
1397+
1398+
assignTypeToTree(vdef, typer, pt) // defines (based on `pt`) and returns `vdef.tpt.tpe`
1399+
}
13801400
} else {
13811401
typer.typedType(tpt).tpe
13821402
}
13831403
pluginsTypeSig(result, typer, vdef, if (tpt.isEmpty) WildcardType else result)
1384-
13851404
}
13861405

13871406
//@M! an abstract type definition (abstract type member/type parameter)

0 commit comments

Comments
 (0)