Skip to content

Fix #3638: Fix protected setter generation #3715

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 4 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
super.transform(tree1)
}
case tree @ Assign(sel: Select, _) =>
superAcc.transformAssign(super.transform(tree))
super.transform(superAcc.transformAssign(tree))
case Inlined(call, bindings, expansion) =>
// Leave only a call trace consisting of
// - a reference to the top-level class from which the call was inlined,
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class SuperAccessors(thisPhase: DenotTransformer) {
.suchThat(_.signature == superInfo.signature).symbol
.orElse {
ctx.debuglog(s"add super acc ${sym.showLocated} to $clazz")
val deferredOrPrivate = if (clazz is Trait) Deferred else Private
val maybeDeferred = if (clazz is Trait) Deferred else EmptyFlags
val acc = ctx.newSymbol(
clazz, superName, Artifact | Method | deferredOrPrivate,
clazz, superName, Artifact | Method | maybeDeferred,
superInfo, coord = accPos).enteredAfter(thisPhase)
// Diagnostic for SI-7091
if (!accDefs.contains(clazz))
Expand Down Expand Up @@ -271,7 +271,7 @@ class SuperAccessors(thisPhase: DenotTransformer) {
val accPos = tree.pos.focus
val protectedAccessor = clazz.info.decl(accName).symbol orElse {
val newAcc = ctx.newSymbol(
clazz, accName, Artifact, accType, coord = accPos).enteredAfter(thisPhase)
clazz, accName, Artifact | Method, accType, coord = accPos).enteredAfter(thisPhase)
val code = DefDef(newAcc, vrefss => {
val (receiver :: value :: Nil) :: Nil = vrefss
Assign(receiver.select(field), value).withPos(accPos)
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/i3638.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package p {
package a {
class JavaInteraction(arr: Array[Char])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this test was minimized from https://github.com/scala/scala/blob/2.13.x/test/files/jvm/protectedacc.scala, it might be worthwhile to port over the whole test (I think it's a run test but it's in /jvm and not /run so it might need to be run in a special way?).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can't run on JS because it uses Java classes, that's why it's in jvm. I added the full test to run.

extends java.io.CharArrayReader(arr) {
class Inner {
{
count = count
}
}
}
}
}
15 changes: 15 additions & 0 deletions tests/run/protectedacc.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
10
meth1(1) = 2
meth1(1.0) = 2.0
meth2(1)(1) = prefix: 0
meth2(1)(1) = prefix: 0
meth3 = class [I
100 = 100
id(1) = 1
id('a') = a
count before: 3
count after: 4
10
meth1(1) = 2
meth2(1)(1) = 10
100 = 100
159 changes: 159 additions & 0 deletions tests/run/protectedacc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
//############################################################################
// Test Java interaction with scala inner classes
//############################################################################

import java.io.{BufferedReader, File, FileWriter, InputStreamReader}

/** The entry point of this test. It has to come first,
* before the package declarations. The parser wouldn't want it
* any other way.
*/
object Test {
def main(args: Array[String]): Unit = {
val b = new p.b.B;
val c = new b.C;
c.m

val ji = new p.b.JavaInteraction(Array('a', 'b', 'c'));
(new ji.Inner).m;

(new p.b.OuterObj.Inner).m
}
}

package p {
package a {

class A {
protected val x = 10;

protected def meth1(x: Int) = x + 1;
protected def meth1(x: Double) = x + 1
protected def meth2(x: Int)(y: String) = y + (x - 1);
protected def meth3 = Array(1, 2)

protected def f[a](x: a) = x

def getA: this.type = this;
}

/** Test type members */
trait HighlighterXXX {
type Node;
protected def highlight(node : Node) : Unit;
}

/** Test type parameters */
abstract class PolyA[a] {
protected def m(x: a): Unit;

class B {

trait Node {
def s: String = "";
}
protected def tie(x: Node): Unit = { x.s; () }
}
}

/** bug 853, longer path members */
class Global {
abstract class Tree;
}

trait HasNSC {
trait GlobalExt extends Global;
val global : GlobalExt;
import global._;
protected def doTyped(tree : Tree): Tree = tree;
def mkTree : Tree;
doTyped(mkTree);
}
}

package b {
import a._;

/** Test interaction with Scala inherited methods and currying. */
class B extends A {
class C {
def m = {
Console.println(x);
Console.println("meth1(1) = " + meth1(1));
Console.println("meth1(1.0) = " + meth1(1.0));
// test accesses from closures
for (x <- 1 until 3)
Console.println("meth2(1)(1) = " + meth2(1)("prefix: "));

Console.println("meth3 = " + meth3.getClass);

val inc = meth2(1)_;
Console.println("100 = " + inc("10"));

Console.println("id(1) = " + f(1))
Console.println("id('a') = " + f("a"))

getA.x;
}
}
}

/** Test interaction with Java inherited protected fields. */
class JavaInteraction(arr: Array[Char]) extends java.io.CharArrayReader(arr) {
class Inner {
def m = {
Console.println("count before: " + count);
count = count + 1;
Console.println("count after: " + count);
}
}
}

/** Test interaction when outer is an object. */
object OuterObj extends p.a.A {
class Inner {
def m = {
Console.println(x);
Console.println("meth1(1) = " + meth1(1));
Console.println("meth2(1)(1) = " + meth2(1)("1"));

val inc = meth2(1)_;
Console.println("100 = " + inc("10"));

getA.x;
}
}
}

trait ScalaAutoEditXXX extends HighlighterXXX {
trait NodeImpl {
def self : Node;
highlight(self);
}
}

abstract class X[T] extends PolyA[T] {

trait Inner extends B {
def self: T;
def self2: Node;
def getB: Inner;

m(self)

trait InnerInner {
val g = getB
g.tie(self2.asInstanceOf[g.Node])
}
}
}

trait ScalaTyperXXX extends HasNSC {
val global : GlobalExt;
import global._;
trait XXX {
def foo(tree : Tree) = doTyped(tree);
}
}
}
}