diff options
author | James Iry <jamesiry@gmail.com> | 2013-02-07 21:39:12 -0800 |
---|---|---|
committer | James Iry <jamesiry@gmail.com> | 2013-02-07 21:39:12 -0800 |
commit | 4c08eb604f6a23077fd1c65d0bb073ea94389961 (patch) | |
tree | 1168b9588c803586d459059b91bdc7d87130ec35 /src/reflect | |
parent | 6bcb830c080c846a7ad4bf5e1949f6b2d02d45fe (diff) | |
parent | 55c9b9c280ac9bc36bbac09397c7646f8dcf4583 (diff) | |
download | scala-4c08eb604f6a23077fd1c65d0bb073ea94389961.tar.gz scala-4c08eb604f6a23077fd1c65d0bb073ea94389961.tar.bz2 scala-4c08eb604f6a23077fd1c65d0bb073ea94389961.zip |
Merge pull request #2088 from retronym/ticket/6146
SI-6146 More accurate prefixes for sealed subtypes.
Diffstat (limited to 'src/reflect')
-rw-r--r-- | src/reflect/scala/reflect/internal/Types.scala | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/src/reflect/scala/reflect/internal/Types.scala b/src/reflect/scala/reflect/internal/Types.scala index 0dd98fb6ae..b708ca0fd6 100644 --- a/src/reflect/scala/reflect/internal/Types.scala +++ b/src/reflect/scala/reflect/internal/Types.scala @@ -586,9 +586,9 @@ trait Types extends api.Types { self: SymbolTable => * Expands type aliases and converts higher-kinded TypeRefs to PolyTypes. * Functions on types are also implemented as PolyTypes. * - * Example: (in the below, <List> is the type constructor of List) - * TypeRef(pre, <List>, List()) is replaced by - * PolyType(X, TypeRef(pre, <List>, List(X))) + * Example: (in the below, `<List>` is the type constructor of List) + * TypeRef(pre, `<List>`, List()) is replaced by + * PolyType(X, TypeRef(pre, `<List>`, List(X))) */ def normalize = this // @MAT @@ -4974,6 +4974,51 @@ trait Types extends api.Types { self: SymbolTable => } } + /** + * A more persistent version of `Type#memberType` which does not require + * that the symbol is a direct member of the prefix. + * + * For instance: + * + * {{{ + * class C[T] { + * sealed trait F[A] + * object X { + * object S1 extends F[T] + * } + * class S2 extends F[T] + * } + * object O extends C[Int] { + * def foo(f: F[Int]) = f match {...} // need to enumerate sealed subtypes of the scrutinee here. + * } + * class S3 extends O.F[String] + * + * nestedMemberType(<S1>, <O.type>, <C>) = O.X.S1.type + * nestedMemberType(<S2>, <O.type>, <C>) = O.S2.type + * nestedMemberType(<S3>, <O.type>, <C>) = S3.type + * }}} + * + * @param sym The symbol of the subtype + * @param pre The prefix from which the symbol is seen + * @param owner + */ + def nestedMemberType(sym: Symbol, pre: Type, owner: Symbol): Type = { + def loop(tp: Type): Type = + if (tp.isTrivial) tp + else if (tp.prefix.typeSymbol isNonBottomSubClass owner) { + val widened = tp match { + case _: ConstantType => tp // Java enum constants: don't widen to the enum type! + case _ => tp.widen // C.X.type widens to C.this.X.type, otherwise `tp asSeenFrom (pre, C)` has no effect. + } + widened asSeenFrom (pre, tp.typeSymbol.owner) + } + else loop(tp.prefix) memberType tp.typeSymbol + + val result = loop(sym.tpeHK) + assert(sym.isTerm || result.typeSymbol == sym, s"($result).typeSymbol = ${result.typeSymbol}; expected ${sym}") + result + } + /** The most deeply nested owner that contains all the symbols * of thistype or prefixless typerefs/singletype occurrences in given type. */ |