From a19237f7be972870ca1c7477704a724419ef793f Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 25 Dec 2013 15:59:02 +0100 Subject: Making the name of self visible inside a class. Previously, it wasn't. --- src/dotty/tools/dotc/typer/Namer.scala | 8 +++++++- tests/pos/typers.scala | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/dotty/tools/dotc/typer/Namer.scala b/src/dotty/tools/dotc/typer/Namer.scala index 2baa3ea13..25006d72f 100644 --- a/src/dotty/tools/dotc/typer/Namer.scala +++ b/src/dotty/tools/dotc/typer/Namer.scala @@ -34,8 +34,14 @@ trait NamerContextOps { this: Context => /** The denotation with the given name in current context */ def denotNamed(name: Name): Denotation = if (owner.isClass) - if (outer.owner == owner) + if (outer.owner == owner) { // inner class scope; check whether we are referring to self + if (scope.size == 1) { + val elem = scope.asInstanceOf[MutableScope].lastEntry + if (elem.name == name) return elem.sym.denot // return self + } + assert(scope.size <= 1, scope) owner.thisType.member(name) + } else // we are in the outermost context belonging to a class; self is invisible here. See inClassContext. owner.findMember(name, owner.thisType, EmptyFlags) else diff --git a/tests/pos/typers.scala b/tests/pos/typers.scala index fd2cc9b74..dbbee3d74 100644 --- a/tests/pos/typers.scala +++ b/tests/pos/typers.scala @@ -76,6 +76,23 @@ object typers { val b: B = new B val y = b.x + val z: String = y + + } + + object Self { + + class A(self: Int) { self => + + class B { + val b = self + val c: A = b + } + + val a = self + val c: A = a + } + } -- cgit v1.2.3