diff options
author | Paul Phillips <paulp@improving.org> | 2009-09-23 18:26:59 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-09-23 18:26:59 +0000 |
commit | 9992fe264c779e36e27bad215ac42012e05cfde4 (patch) | |
tree | c174b55852491d48db8f8e4e71243b6dcb35afbd | |
parent | 4edbecfe9baaec89ced227f5bd6b92fb1f54e1ec (diff) | |
download | scala-9992fe264c779e36e27bad215ac42012e05cfde4.tar.gz scala-9992fe264c779e36e27bad215ac42012e05cfde4.tar.bz2 scala-9992fe264c779e36e27bad215ac42012e05cfde4.zip |
Fix and test case for #2377.
-rwxr-xr-x | src/compiler/scala/tools/nsc/javac/JavaParsers.scala | 26 | ||||
-rw-r--r-- | test/files/pos/t2377/Q.java | 12 | ||||
-rw-r--r-- | test/files/pos/t2377/a.scala | 8 |
3 files changed, 43 insertions, 3 deletions
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 |