Skip to content

Commit fd694c1

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... Regardless of the new encoding, we should consistently infer result types for `def`s and `val`s.
1 parent 68342b4 commit fd694c1

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
@@ -1372,12 +1372,31 @@ trait Namers extends MethodSynthesis {
13721372
MissingParameterOrValTypeError(tpt)
13731373
ErrorType
13741374
}
1375-
else assignTypeToTree(vdef, typer, WildcardType)
1375+
else {
1376+
val valOwner = owner.owner
1377+
val pt =
1378+
// there's no overriding outside of classes
1379+
if (!valOwner.isClass) WildcardType
1380+
else {
1381+
// Pretend we're an erroneous symbol, for now, so that we match while finding the overridden symbol,
1382+
// but are not considered during implicit search.
1383+
// The symbol's info is currently being determined (up the call stack, you'll find a TypeCompleter's complete method),
1384+
// so the info will be set to whatever type we return here by the complete method.
1385+
val saved = vdef.symbol.rawInfo
1386+
try {
1387+
vdef.symbol setInfo ErrorType
1388+
val overridden = vdef.symbol.nextOverriddenSymbol
1389+
if (overridden == NoSymbol || overridden.isOverloaded) WildcardType
1390+
else valOwner.thisType.memberType(overridden).resultType
1391+
} finally vdef.symbol.setInfo(saved)
1392+
}
1393+
1394+
assignTypeToTree(vdef, typer, pt) // defines (based on `pt`) and returns `vdef.tpt.tpe`
1395+
}
13761396
} else {
13771397
typer.typedType(tpt).tpe
13781398
}
13791399
pluginsTypeSig(result, typer, vdef, if (tpt.isEmpty) WildcardType else result)
1380-
13811400
}
13821401

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

0 commit comments

Comments
 (0)