Permalink
Browse files

Ctor default-getters unique name and are typed in constructor context

  • Loading branch information...
som-snytt committed Mar 21, 2012
1 parent e295186 commit 47bfd744177121de08fed489a5b0b1b59a1ae06a
@@ -77,6 +77,7 @@ trait NameManglers {
val PROTECTED_SET_PREFIX = PROTECTED_PREFIX + "set"
val SINGLETON_SUFFIX = ".type"
val SUPER_PREFIX_STRING = "super$"
val INIT_DEFAULT_PREFIX = "init$"
val TRAIT_SETTER_SEPARATOR_STRING = "$_setter_$"
val SETTER_SUFFIX: TermName = encode("_=")
@@ -170,13 +171,15 @@ trait NameManglers {
}
def defaultGetterName(name: Name, pos: Int): TermName = {
val prefix = if (isConstructorName(name)) "init" else name
val prefix = if (isConstructorName(name)) INIT_DEFAULT_PREFIX else name
newTermName(prefix + DEFAULT_GETTER_STRING + pos)
}
def defaultGetterToMethod(name: Name): TermName = {
val p = name.pos(DEFAULT_GETTER_STRING)
if (p < name.length) name.toTermName.subName(0, p)
else name.toTermName
if (p < name.length) {
val q = name.toTermName.subName(0, p)
if (q.decoded == INIT_DEFAULT_PREFIX) CONSTRUCTOR else q
} else name.toTermName
}
// This isn't needed at the moment since I fixed $class$1 but
@@ -4399,7 +4399,9 @@ trait Typers extends Modes with Adaptations with PatMatVirtualiser {
typedValDef(vdef)
case ddef @ DefDef(_, _, _, _, _, _) =>
newTyper(context.makeNewScope(tree, sym)).typedDefDef(ddef)
// flag default getters for constructors. An actual flag would be nice. See SI-5543.
val flag = ddef.mods.hasDefaultFlag && nme.defaultGetterToMethod(sym.name) == nme.CONSTRUCTOR
newTyper(context.makeNewScope(tree, sym)).constrTyperIf(flag).typedDefDef(ddef)
case tdef @ TypeDef(_, _, _, _) =>
typedTypeDef(tdef)
@@ -0,0 +1,3 @@
Test, 7, 119
m, 3, 19
Test, 5, 85
View
@@ -0,0 +1,26 @@
object Test extends Function0[Int] {
// this and v resolve to Test.this, Test.v not A.this, A.v
class A(x: Function0[Int] = this)(val a: Int = v, val b: Int = v * x()) extends Function0[Int] {
val v = 3
override def toString = x.toString +", "+ a +", "+ b
// ordinary instance scope
def m(i: Int = v, y: Function0[Int] = this) = "m, "+ i +", "+ y()
def apply() = 19
}
object A {
val v = 5
// should happily coexist with default getters
def init(x: Function0[Int] = Test.this)(a: Int = v, b: Int = v * x()) = x.toString +", "+ a +", "+ b
override def toString = "A"
}
val v = 7
def apply() = 17
override def toString = "Test"
def main(args: Array[String]) {
val sut = new A()()
println(sut.toString)
println(sut.m())
println(A.init()())
}
}

0 comments on commit 47bfd74

Please sign in to comment.