From 9992fe264c779e36e27bad215ac42012e05cfde4 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 23 Sep 2009 18:26:59 +0000 Subject: Fix and test case for #2377. --- .../scala/tools/nsc/javac/JavaParsers.scala | 26 +++++++++++++++++++--- test/files/pos/t2377/Q.java | 12 ++++++++++ test/files/pos/t2377/a.scala | 8 +++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 test/files/pos/t2377/Q.java create mode 100644 test/files/pos/t2377/a.scala diff --git a/src/compiler/scala/tools/nsc/javac/JavaParsers.scala b/src/compiler/scala/tools/nsc/javac/JavaParsers.scala index 4d36816853..4d230470c6 100755 --- a/src/compiler/scala/tools/nsc/javac/JavaParsers.scala +++ b/src/compiler/scala/tools/nsc/javac/JavaParsers.scala @@ -633,9 +633,29 @@ trait JavaParsers extends JavaScanners { Import(Ident(cdef.name.toTermName), List((nme.WILDCARD, null))) } - // create companion object even if it has no statics, so import A._ works; bug #1700 - def addCompanionObject(statics: List[Tree], cdef: ClassDef): List[Tree] = - List(makeCompanionObject(cdef, statics), importCompanionObject(cdef), cdef) + // Importing the companion object members cannot be done uncritically: see + // ticket #2377 wherein a class contains two static inner classes, each of which + // has a static inner class called "Builder" - this results in an ambiguity error + // when each performs the import in the enclosing class's scope. + // + // To address this I moved the import Companion._ inside the class, as the first + // statement. This should work without compromising the enclosing scope, but may (?) + // end up suffering from the same issues it does in scala - specifically that this + // leaves auxiliary constructors unable to access members of the companion object + // as unqualified identifiers. + def addCompanionObject(statics: List[Tree], cdef: ClassDef): List[Tree] = { + def implWithImport(importStmt: Tree) = { + import cdef.impl._ + treeCopy.Template(cdef.impl, parents, self, importStmt :: body) + } + // if there are no statics we can use the original cdef, but we always + // create the companion so import A._ is not an error (see ticket #1700) + val cdefNew = + if (statics.isEmpty) cdef + else treeCopy.ClassDef(cdef, cdef.mods, cdef.name, cdef.tparams, implWithImport(importCompanionObject(cdef))) + + List(makeCompanionObject(cdefNew, statics), cdefNew) + } def importDecl(): List[Tree] = { accept(IMPORT) diff --git a/test/files/pos/t2377/Q.java b/test/files/pos/t2377/Q.java new file mode 100644 index 0000000000..ef5b0574a6 --- /dev/null +++ b/test/files/pos/t2377/Q.java @@ -0,0 +1,12 @@ +public final class Q { + public static final class Stage { + public static Builder newBuilder() { return new Builder(); } + public static final class Builder { } + public Builder toBuilder() { return newBuilder(); } + } + public static final class WorkUnit { + public static Builder newBuilder() { return new Builder(); } + public static final class Builder { } + public Builder toBuilder() { return newBuilder(); } + } +} \ No newline at end of file diff --git a/test/files/pos/t2377/a.scala b/test/files/pos/t2377/a.scala new file mode 100644 index 0000000000..5ec58987ff --- /dev/null +++ b/test/files/pos/t2377/a.scala @@ -0,0 +1,8 @@ +import Q._ + +class Bop(var workUnit: WorkUnit) { + def addStages(stageBuilder: Stage.Builder): Unit = { + val builder = workUnit.toBuilder + () + } +} \ No newline at end of file -- cgit v1.2.3