Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d76195

Browse files
committed
[WIP] companion nest
1 parent a527019 commit 9d76195

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

src/compiler/scala/tools/nsc/Global.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,7 @@ class Global(var currentSettings: Settings, reporter0: Reporter)
11811181
keepPhaseStack = settings.log.isSetByUser
11821182

11831183
val isScala3: Boolean = settings.isScala3: @nowarn
1184+
val isJDK11: Boolean = settings.releaseValue.map(_.toInt >= 11).getOrElse(false)
11841185

11851186
object sourceFeatures {
11861187
private val s = settings

src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,14 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
711711
generatedType = genPrimitiveOp(app, expectedType)
712712
} else { // normal method call
713713
def isTraitSuperAccessorBodyCall = app.hasAttachment[UseInvokeSpecial.type]
714+
def isPrivateSpecial = sym.isPrivate && (claszSymbol.info <:< sym.owner.info)
714715
val invokeStyle =
715716
if (sym.isStaticMember)
716717
InvokeStyle.Static
717-
else if (sym.isPrivate || sym.isClassConstructor) InvokeStyle.Special
718-
else if (isTraitSuperAccessorBodyCall)
718+
else if (isPrivateSpecial || sym.isClassConstructor || isTraitSuperAccessorBodyCall)
719719
InvokeStyle.Special
720-
else InvokeStyle.Virtual
720+
else
721+
InvokeStyle.Virtual
721722

722723
if (invokeStyle.hasInstance) genLoadQualifier(fun)
723724
genLoadArguments(args, paramTKs(app))

src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ abstract class BCodeSkelBuilder extends BCodeHelpers {
197197
if (emitSource) {
198198
cnode.visitSource(cunit.source.toString, null /* SourceDebugExtension */)
199199
}
200+
if (currentRun.isJDK11 && claszSymbol.isModuleClass) {
201+
val linked = claszSymbol.linkedClassOfClass
202+
if (linked.exists)
203+
cnode.visitNestHost(internalName(linked))
204+
}
200205

201206
enclosingMethodAttribute(claszSymbol, internalName, methodBTypeFromSymbol(_).descriptor) match {
202207
case Some(EnclosingMethodEntry(className, methodName, methodDescriptor)) =>
@@ -208,6 +213,13 @@ abstract class BCodeSkelBuilder extends BCodeHelpers {
208213
cnode.visitAttribute(if (ssa.isDefined) pickleMarkerLocal else pickleMarkerForeign)
209214
emitAnnotations(cnode, claszSymbol.annotations ++ ssa)
210215

216+
if (currentRun.isJDK11 && !claszSymbol.isModuleClass) {
217+
val companion = claszSymbol.companionModule
218+
val linked = companion.moduleClass
219+
if (linked.exists)
220+
cnode.visitNestMember(internalName(linked))
221+
}
222+
211223
if (isCZStaticModule || isCZParcelable) {
212224

213225
if (isCZStaticModule) { addModuleInstanceField() }

src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ abstract class ExplicitOuter extends InfoTransform
434434
else atPos(tree.pos)(outerPath(outerValue, currentClass.outerClass, sym)) // (5)
435435

436436
case Select(qual, name) =>
437+
def isNestable(c0: Symbol, c1: Symbol): Boolean = currentRun.isJDK11 && (
438+
(if (c0.isModuleClass) c0.linkedClassOfClass == c1 else c1.isModuleClass && c1.linkedClassOfClass == c0)
439+
|| c0.hasTransOwner(c1)
440+
)
437441
// make not private symbol accessed from inner classes, as well as
438442
// symbols accessed from @inline methods
439443
//
@@ -442,7 +446,7 @@ abstract class ExplicitOuter extends InfoTransform
442446
def enclMethodIsInline = closestEnclMethod(currentOwner) hasAnnotation ScalaInlineClass
443447
// scala/bug#8710 The extension method condition reflects our knowledge that a call to `new Meter(12).privateMethod`
444448
// with later be rewritten (in erasure) to `Meter.privateMethod$extension(12)`.
445-
if ((currentClass != sym.owner || enclMethodIsInline) && !sym.isMethodWithExtension)
449+
if ((currentClass != sym.owner || enclMethodIsInline) && !sym.isMethodWithExtension && !isNestable(currentClass, sym.owner))
446450
sym.makeNotPrivate(sym.owner)
447451

448452
val qsym = qual.tpe.widen.typeSymbol

test/files/run/t6882.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//> using options --release:11
2+
3+
class C(private val i: Int, private val j: Int) {
4+
private val c = i + C.secret
5+
6+
@inline def f = j * 2
7+
}
8+
object C {
9+
def unwrap(c: C): Int = c.c
10+
11+
private def secret = 5
12+
}
13+
object Test extends App {
14+
assert(C.unwrap(new C(42, 27)) == 47)
15+
}

0 commit comments

Comments
 (0)