Skip to content

Commit

Permalink
Remove import companion module in java class
Browse files Browse the repository at this point in the history
  • Loading branch information
changvvb committed Jun 24, 2021
1 parent 0f895a1 commit dcccaaa
Showing 1 changed file with 2 additions and 29 deletions.
31 changes: 2 additions & 29 deletions compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -751,35 +751,8 @@ object JavaParsers {
makeTemplate(List(), statics, List(), false)).withMods((cdef.mods & Flags.RetainedModuleClassFlags).toTermFlags)
}

def importCompanionObject(cdef: TypeDef): Tree =
Import(
Ident(cdef.name.toTermName).withSpan(NoSpan),
ImportSelector(Ident(nme.WILDCARD)) :: Nil)

// 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: TypeDef): List[Tree] = {
// 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 {
val template = cdef.rhs.asInstanceOf[Template]
cpy.TypeDef(cdef)(cdef.name,
cpy.Template(template)(body = importCompanionObject(cdef) :: template.body))
.withMods(cdef.mods)
}

List(makeCompanionObject(cdefNew, statics), cdefNew)
}
def addCompanionObject(statics: List[Tree], cdef: TypeDef): List[Tree] =
List(makeCompanionObject(cdef, statics), cdef)

def importDecl(): List[Tree] = {
val start = in.offset
Expand Down

0 comments on commit dcccaaa

Please sign in to comment.