Skip to content

Add outer param when java inner class instantiated #11198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ object Erasure {

/** The outer parameter definition of a constructor if it needs one */
private def outerParamDefs(constr: Symbol)(using Context): List[ValDef] =
if constr.isConstructor && hasOuterParam(constr.owner.asClass) then
if constr.isConstructor && needsOuterParam(constr.owner.asClass) then
constr.info match
case MethodTpe(outerName :: _, outerType :: _, _) =>
val outerSym = newSymbol(constr, outerName, Flags.Param, outerType)
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ object ExplicitOuter {
private def hasOuter(cls: ClassSymbol)(using Context): Boolean =
needsOuterIfReferenced(cls) && outerAccessor(cls).exists

/** Class constructor takes an outer argument. Can be called only after phase ExplicitOuter. */
def hasOuterParam(cls: ClassSymbol)(using Context): Boolean =
!cls.is(Trait) && needsOuterIfReferenced(cls) && outerAccessor(cls).exists
/** Class constructor needs an outer argument. Can be called only after phase ExplicitOuter. */
def needsOuterParam(cls: ClassSymbol)(using Context): Boolean =
!cls.is(Trait) && needsOuterIfReferenced(cls) && (cls.is(JavaDefined) || outerAccessor(cls).exists)

/** Tree references an outer class of `cls` which is not a static owner.
*/
Expand Down Expand Up @@ -357,7 +357,7 @@ object ExplicitOuter {

/** If `cls` has an outer parameter add one to the method type `tp`. */
def addParam(cls: ClassSymbol, tp: Type): Type =
if (hasOuterParam(cls)) {
if (needsOuterParam(cls)) {
val mt @ MethodTpe(pnames, ptypes, restpe) = tp
mt.derivedLambdaType(
nme.OUTER :: pnames, outerClass(cls).typeRef :: ptypes, restpe)
Expand All @@ -378,7 +378,7 @@ object ExplicitOuter {
case TypeApply(Select(r, nme.asInstanceOf_), args) =>
outerArg(r) // cast was inserted, skip
}
if (hasOuterParam(cls))
if (needsOuterParam(cls))
methPart(fun) match {
case Select(receiver, _) => outerArg(receiver).withSpan(fun.span) :: Nil
}
Expand All @@ -390,7 +390,7 @@ object ExplicitOuter {
* argument, the singleton list with the argument, otherwise Nil.
*/
def argsForNew(cls: ClassSymbol, tpe: Type): List[Tree] =
if (hasOuterParam(cls)) singleton(fixThis(outerPrefix(tpe))) :: Nil
if (needsOuterParam(cls)) singleton(fixThis(outerPrefix(tpe))) :: Nil
else Nil

/** A path of outer accessors starting from node `start`. `start` defaults to the
Expand Down
3 changes: 3 additions & 0 deletions tests/run/10838/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class A {
public class B {}
}
6 changes: 6 additions & 0 deletions tests/run/10838/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def main(args: Array[String]): Unit = {
val a = new A
new a.B
}
}