diff options
author | Stefan Zeiger <szeiger@novocode.com> | 2016-02-05 18:22:49 +0100 |
---|---|---|
committer | Stefan Zeiger <szeiger@novocode.com> | 2016-02-08 15:20:05 +0100 |
commit | fbba81991e69faf3e7416b4caabed1e99c528917 (patch) | |
tree | 3e0a59a9f82bab410bb4b2320a49376fff5a1bec /src/compiler | |
parent | 353c305b13a8b0fbb622e157eabcdd7c69e8ddca (diff) | |
download | scala-fbba81991e69faf3e7416b4caabed1e99c528917.tar.gz scala-fbba81991e69faf3e7416b4caabed1e99c528917.tar.bz2 scala-fbba81991e69faf3e7416b4caabed1e99c528917.zip |
SI-9574 Prevent illegal overrides of members with module types
Commit f32a32b1b33c9d7ccd62467e3e10cb69930023c8 introduced the ability
to override objects with other objects. The exception that allows
these overrides (where the usual subtyping check fails) was applied to
all members whose type is a module class. This is too broad, however,
because it not only applies to members of the form `object foo` but
also `def foo: bar.type` (where `bar` is an `object`).
The fix is to restrict the exception to those cases where both
definitions actually are objects.
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/RefChecks.scala | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 3860cc252d..cafea55b4d 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -88,17 +88,19 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans if (sym.hasAccessBoundary) "" + sym.privateWithin.name else "" ) - def overridesTypeInPrefix(tp1: Type, tp2: Type, prefix: Type): Boolean = (tp1.dealiasWiden, tp2.dealiasWiden) match { + def overridesTypeInPrefix(tp1: Type, tp2: Type, prefix: Type, isModuleOverride: Boolean): Boolean = (tp1.dealiasWiden, tp2.dealiasWiden) match { case (MethodType(List(), rtp1), NullaryMethodType(rtp2)) => rtp1 <:< rtp2 case (NullaryMethodType(rtp1), MethodType(List(), rtp2)) => rtp1 <:< rtp2 case (TypeRef(_, sym, _), _) if sym.isModuleClass => - overridesTypeInPrefix(NullaryMethodType(tp1), tp2, prefix) + overridesTypeInPrefix(NullaryMethodType(tp1), tp2, prefix, isModuleOverride) case _ => def classBoundAsSeen(tp: Type) = tp.typeSymbol.classBound.asSeenFrom(prefix, tp.typeSymbol.owner) - - (tp1 <:< tp2) || ( // object override check + (tp1 <:< tp2) || isModuleOverride && ( + // Object override check. This requires that both the overridden and the overriding member are object + // definitions. The overriding module type is allowed to replace the original one with the same name + // as long as it conform to the original non-singleton type. tp1.typeSymbol.isModuleClass && tp2.typeSymbol.isModuleClass && { val cb1 = classBoundAsSeen(tp1) val cb2 = classBoundAsSeen(tp2) @@ -520,7 +522,7 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans } def checkOverrideTerm() { other.cookJavaRawInfo() // #2454 - if (!overridesTypeInPrefix(lowType, highType, rootType)) { // 8 + if (!overridesTypeInPrefix(lowType, highType, rootType, low.isModuleOrModuleClass && high.isModuleOrModuleClass)) { // 8 overrideTypeError() explainTypes(lowType, highType) } |