Skip to content

Commit

Permalink
SI-5703: normalize refined types more
Browse files Browse the repository at this point in the history
to improve Array[T] java-interop with T[],
normalize Object with Object{} to Object

fix #SI-5688 by flattening refined types in parents

updated check files to reflect flattening of refined types and updated position for refined types
  • Loading branch information
adriaanm committed May 3, 2012
1 parent 6300c30 commit 7a5aaa9
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 34 deletions.
31 changes: 22 additions & 9 deletions src/compiler/scala/reflect/internal/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1610,12 +1610,26 @@ trait Types extends api.Types { self: SymbolTable =>
override def typeConstructor =
copyRefinedType(this, parents map (_.typeConstructor), decls)

/* MO to AM: This is probably not correct
* If they are several higher-kinded parents with different bounds we need
* to take the intersection of their bounds
*/
override def normalize = {
if (isHigherKinded) {
final override def normalize: Type =
if (phase.erasedTypes) normalizeImpl
else {
if (normalized eq null) normalized = normalizeImpl
normalized
}

private var normalized: Type = _
private def normalizeImpl = {
// TODO see comments around def intersectionType and def merge
def flatten(tps: List[Type]): List[Type] = tps flatMap { case RefinedType(parents, ds) if ds.isEmpty => flatten(parents) case tp => List(tp) }
val flattened = flatten(parents).distinct
if (decls.isEmpty && flattened.tail.isEmpty) {
flattened.head
} else if (flattened != parents) {
refinedType(flattened, if (typeSymbol eq NoSymbol) NoSymbol else typeSymbol.owner, decls, NoPosition)
} else if (isHigherKinded) {
// MO to AM: This is probably not correct
// If they are several higher-kinded parents with different bounds we need
// to take the intersection of their bounds
typeFun(
typeParams,
RefinedType(
Expand All @@ -1625,8 +1639,7 @@ trait Types extends api.Types { self: SymbolTable =>
},
decls,
typeSymbol))
}
else super.normalize
} else super.normalize
}

/** A refined type P1 with ... with Pn { decls } is volatile if
Expand Down Expand Up @@ -3322,7 +3335,7 @@ trait Types extends api.Types { self: SymbolTable =>
if (phase.erasedTypes)
if (parents.isEmpty) ObjectClass.tpe else parents.head
else {
val clazz = owner.newRefinementClass(NoPosition)
val clazz = owner.newRefinementClass(pos) // TODO: why were we passing in NoPosition instead of pos?
val result = RefinedType(parents, decls, clazz)
clazz.setInfo(result)
result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,12 @@ abstract class ClassfileParser {
// make unbounded Array[T] where T is a type variable into Array[T with Object]
// (this is necessary because such arrays have a representation which is incompatible
// with arrays of primitive types.
if (elemtp.typeSymbol.isAbstractType && !(elemtp <:< definitions.ObjectClass.tpe))
// NOTE that the comparison to Object only works for abstract types bounded by classes that are strict subclasses of Object
// if the bound is exactly Object, it will have been converted to Any, and the comparison will fail
// see also RestrictJavaArraysMap (when compiling java sources directly)
if (elemtp.typeSymbol.isAbstractType && !(elemtp <:< definitions.ObjectClass.tpe)) {
elemtp = intersectionType(List(elemtp, definitions.ObjectClass.tpe))
}

definitions.arrayType(elemtp)
case '(' =>
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/scala/tools/nsc/typechecker/Namers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,11 @@ trait Namers extends MethodSynthesis {
/** Convert Java generic array type T[] to (T with Object)[]
* (this is necessary because such arrays have a representation which is incompatible
* with arrays of primitive types.)
*
* @note the comparison to Object only works for abstract types bounded by classes that are strict subclasses of Object
* if the bound is exactly Object, it will have been converted to Any, and the comparison will fail
*
* see also sigToType
*/
private object RestrictJavaArraysMap extends TypeMap {
def apply(tp: Type): Type = tp match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL
// implements the run-time aspects of (§8.2) (typedPattern has already done the necessary type transformations)
// TODO: normalize construction, which yields a combination of a EqualityTestTreeMaker (when necessary) and a TypeTestTreeMaker
case class TypeAndEqualityTestTreeMaker(prevBinder: Symbol, patBinder: Symbol, pt: Type, pos: Position) extends CondTreeMaker {
val nextBinderTp = glb(List(patBinder.info.widen, pt))
val nextBinderTp = glb(List(patBinder.info.widen, pt)).normalize

/** Type patterns consist of types, type variables, and wildcards. A type pattern T is of one of the following forms:
- A reference to a class C, p.C, or T#C.
Expand Down
2 changes: 1 addition & 1 deletion test/files/neg/override.check
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
override.scala:9: error: overriding type T in trait A with bounds >: Int <: Int;
type T in trait B with bounds >: String <: String has incompatible type
lazy val x : A with B = x
^
^
one error found
3 changes: 3 additions & 0 deletions test/files/pos/t5703/Base.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public abstract class Base<Params> {
public abstract void func(Params[] params);
}
3 changes: 3 additions & 0 deletions test/files/pos/t5703/Impl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Implementation extends Base[Object] {
def func(params: Array[Object]): Unit = {}
}
44 changes: 22 additions & 22 deletions test/files/run/existentials3-old.check
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
_ <: scala.runtime.AbstractFunction0[_ <: Object with Test$ToS with scala.Product with scala.Serializable] with scala.Serializable with java.lang.Object
_ <: Object with Test$ToS with scala.Product with scala.Serializable
Object with Test$ToS
Object with Test$ToS
Object with Test$ToS
scala.Function0[Object with Test$ToS]
scala.Function0[Object with Test$ToS]
_ <: Object with _ <: Object with Object with Test$ToS
_ <: Object with _ <: Object with _ <: Object with Test$ToS
scala.collection.immutable.List[Object with scala.collection.Seq[Int]]
scala.collection.immutable.List[Object with scala.collection.Seq[_ <: Int]]
_ <: scala.runtime.AbstractFunction0[_ <: Object with Test$ToS with scala.Product with scala.Serializable] with scala.Serializable with java.lang.Object
_ <: Object with Test$ToS with scala.Product with scala.Serializable
Object with Test$ToS
Object with Test$ToS
Object with Test$ToS
scala.Function0[Object with Test$ToS]
scala.Function0[Object with Test$ToS]
_ <: Object with _ <: Object with Object with Test$ToS
_ <: Object with _ <: Object with _ <: Object with Test$ToS
scala.collection.immutable.List[Object with scala.collection.Seq[Int]]
scala.collection.immutable.List[Object with scala.collection.Seq[_ <: Int]]
_ <: scala.runtime.AbstractFunction0[_ <: Object with Test$ToS with scala.Product with scala.Serializable] with scala.Serializable with java.lang.Object
_ <: Object with Test$ToS with scala.Product with scala.Serializable
Object with Test$ToS
Object with Test$ToS
Object with Test$ToS
scala.Function0[Object with Test$ToS]
scala.Function0[Object with Test$ToS]
_ <: Object with _ <: Object with Test$ToS
_ <: Object with _ <: Object with _ <: Object with Test$ToS
scala.collection.immutable.List[Object with scala.collection.Seq[Int]]
scala.collection.immutable.List[Object with scala.collection.Seq[_ <: Int]]
_ <: scala.runtime.AbstractFunction0[_ <: Object with Test$ToS with scala.Product with scala.Serializable] with scala.Serializable with java.lang.Object
_ <: Object with Test$ToS with scala.Product with scala.Serializable
Object with Test$ToS
Object with Test$ToS
Object with Test$ToS
scala.Function0[Object with Test$ToS]
scala.Function0[Object with Test$ToS]
_ <: Object with _ <: Object with Test$ToS
_ <: Object with _ <: Object with _ <: Object with Test$ToS
scala.collection.immutable.List[Object with scala.collection.Seq[Int]]
scala.collection.immutable.List[Object with scala.collection.Seq[_ <: Int]]
1 change: 1 addition & 0 deletions test/files/run/t5688.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Vector(ta, tb, tab)
23 changes: 23 additions & 0 deletions test/files/run/t5688.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
object Test extends App {
trait T

trait TA
trait TB

class A extends T with TA
class B extends T with TB
class AB extends T with TA with TB
// Matching on _: TA with TB

val li: Vector[T] = Vector(new A, new B, new AB)

val matched = (for (l <- li) yield {
l match {
case _: TA with TB => "tab"
case _: TA => "ta"
case _: TB => "tb"
}
})

println(matched)
}

0 comments on commit 7a5aaa9

Please sign in to comment.