Skip to content

Unit-test LUBs. #15

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

Closed
wants to merge 21 commits into from
Closed

Unit-test LUBs. #15

wants to merge 21 commits into from

Conversation

folone
Copy link

@folone folone commented Nov 23, 2014

Here are some unit tests. @adriaanm is this something you had in mind for this?

adriaanm and others added 20 commits November 18, 2014 14:07
This requires -Xexperimental in 2.11.x.

Update spec.

TODO:
 - test!
 - include SIP as spec addendum
 - don't predicate on -Xexperimental -- introduce -Xsip:23?

Notes:
 - `()` is not a literal, neither are `Symbol`s
`LiteralType` is a `SingleType` with a literal for its path.
With an example: `1` is to `p.type`, as `1` is to `p`.

`lit` is the most precise type for an expression known to evaluate to `lit`.
Note that we cannot constant fold the expression to `lit`, since it may have a side effect.

`ConstantType(c)` is the type of an expression that can be constant folded to `c`.

When an expressions should not be constant folded, the corresponding `LiteralType`
is created: `ConstantType(c).deconst == LiteralType(c)`
(this is where all `LiteralType`s originate, except for unpickling).

`typedLiteral` always infers a `ConstantType`, which is subsequently
`deconst`ed or `widen`ed where necessary (see `widenIfNecessary`).
In short, only `final val`s may have `ConstantType`s, and
`widen`ing is required to ensure that a path is stable and accessible (roughly speaking).

When checking subtyping, this invariant is useful: `LiteralType(c).widen == c.tpe`.

TODO:
  - test! (subtyping, type inference, type tests, patmat, dependent method types)
  - fully predicate implementation on -Xsip:23
  - is it safe to reduce LITERAL in pickler?
test case:

```
val y: 5 = 5
def g(x: Int) = x match {
  case _: y.type => 0
}
```
Before:

	sandbox/test.scala:2: error: type mismatch;
	 found   : Array[1]
	 required: Array[Int]
TODO: deriving this from type param bounds is not the best way,
should consider all constraints that we encounter

scala> def stable[T <: 1](x: T): T = x
stable: [T <: 1](x: T)T

scala> stable(1)
res0: 1 = 1

scala> stable(2)
<console>:9: error: inferred type arguments [2] do not conform to method stable's type parameter bounds [T <: 1]
              stable(2)
              ^
<console>:9: error: type mismatch;
 found   : Int(2)
 required: T
              stable(2)
                     ^

scala> def stable[T <: Singleton](x: T): T = x
stable: [T <: Singleton](x: T)T

scala> val x: Int = 2
x: Int = 2

scala> val y: x.type = x
y: x.type = 2

scala> stable(y)
res3: x.type = 2
this bootstraps
```
scala> val x = "a"
x: "a" = a

scala> x.asInstanceOf["a"]
res1: "a" = a

scala> "1".asInstanceOf["2"]
x asInstanceOf Throwable
java.lang.ClassCastException
  ... 33 elided

scala> "1".asInstanceOf["1"]
res3: "1" = 1

scala> 1.asInstanceOf[1]
res4: 1 = 1

scala> 1.asInstanceOf[2]
x asInstanceOf Throwable
java.lang.ClassCastException
  ... 33 elided

```
@adriaanm
Copy link
Owner

Thanks, @folone! I'll have a look tomorrow. Just moved back to SF ;-)

@folone
Copy link
Author

folone commented Nov 24, 2014

Oh, get un jet lagged first then!

@adriaanm
Copy link
Owner

Thanks! Good start! I added some comments over at 8ae5c19.
Would be good to have one test method for each category ("category" as suggested in the other comment).

@adriaanm adriaanm force-pushed the sip23 branch 3 times, most recently from 53f7603 to f68aad8 Compare March 24, 2015 09:20
adriaanm pushed a commit that referenced this pull request Jun 7, 2016
This corrects an error in the change to the trait encoding
in scala#5003: getters in traits should have empty bodies and
be emitted as abstract.

```
% ~/scala/2.12.0-M4/bin/scalac sandbox/test.scala && javap -c T
Compiled from "test.scala"
public interface T {
  public abstract void T$_setter_$x_$eq(int);

  public int x();
    Code:
       0: aload_0
       1: invokeinterface #15,  1           // InterfaceMethod x:()I
       6: ireturn

  public int y();
    Code:
       0: aload_0
       1: invokeinterface #20,  1           // InterfaceMethod y:()I
       6: ireturn

  public void y_$eq(int);
    Code:
       0: aload_0
       1: iload_1
       2: invokeinterface #24,  2           // InterfaceMethod y_$eq:(I)V
       7: return

  public void $init$();
    Code:
       0: aload_0
       1: bipush        42
       3: invokeinterface scala#29,  2           // InterfaceMethod T$_setter_$x_$eq:(I)V
       8: aload_0
       9: bipush        24
      11: invokeinterface #24,  2           // InterfaceMethod y_$eq:(I)V
      16: return
}

% qscalac sandbox/test.scala && javap -c T
Compiled from "test.scala"
public interface T {
  public abstract void T$_setter_$x_$eq(int);

  public abstract int x();

  public abstract int y();

  public abstract void y_$eq(int);

  public static void $init$(T);
    Code:
       0: aload_0
       1: bipush        42
       3: invokeinterface #21,  2           // InterfaceMethod T$_setter_$x_$eq:(I)V
       8: aload_0
       9: bipush        24
      11: invokeinterface #23,  2           // InterfaceMethod y_$eq:(I)V
      16: return

  public void $init$();
    Code:
       0: aload_0
       1: invokestatic  scala#27                 // Method $init$:(LT;)V
       4: return
}
```
@milessabin
Copy link

@folone any chance you could rebase against https://github.com/milessabin/scala/commits/topic/sip-23-redux and send those tests my way as a PR?

@folone
Copy link
Author

folone commented Jun 13, 2016

@milessabin will do tonight!

adriaanm pushed a commit that referenced this pull request Jun 28, 2016
This corrects an error in the change to the trait encoding
in scala#5003: getters in traits should have empty bodies and
be emitted as abstract.

```
% ~/scala/2.12.0-M4/bin/scalac sandbox/test.scala && javap -c T
Compiled from "test.scala"
public interface T {
  public abstract void T$_setter_$x_$eq(int);

  public int x();
    Code:
       0: aload_0
       1: invokeinterface #15,  1           // InterfaceMethod x:()I
       6: ireturn

  public int y();
    Code:
       0: aload_0
       1: invokeinterface #20,  1           // InterfaceMethod y:()I
       6: ireturn

  public void y_$eq(int);
    Code:
       0: aload_0
       1: iload_1
       2: invokeinterface #24,  2           // InterfaceMethod y_$eq:(I)V
       7: return

  public void $init$();
    Code:
       0: aload_0
       1: bipush        42
       3: invokeinterface scala#29,  2           // InterfaceMethod T$_setter_$x_$eq:(I)V
       8: aload_0
       9: bipush        24
      11: invokeinterface #24,  2           // InterfaceMethod y_$eq:(I)V
      16: return
}

% qscalac sandbox/test.scala && javap -c T
Compiled from "test.scala"
public interface T {
  public abstract void T$_setter_$x_$eq(int);

  public abstract int x();

  public abstract int y();

  public abstract void y_$eq(int);

  public static void $init$(T);
    Code:
       0: aload_0
       1: bipush        42
       3: invokeinterface #21,  2           // InterfaceMethod T$_setter_$x_$eq:(I)V
       8: aload_0
       9: bipush        24
      11: invokeinterface #23,  2           // InterfaceMethod y_$eq:(I)V
      16: return

  public void $init$();
    Code:
       0: aload_0
       1: invokestatic  scala#27                 // Method $init$:(LT;)V
       4: return
}
```
@folone folone closed this Aug 1, 2016
@folone folone deleted the sip23-tests branch August 1, 2016 14:19
adriaanm pushed a commit that referenced this pull request Aug 22, 2016
Top level modules in Scala currently desugar as:

```
class C; object O extends C { toString }
```

```
public final class O$ extends C {
  public static final O$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class O$
       3: invokespecial #12                 // Method "<init>":()V
       6: return

  private O$();
    Code:
       0: aload_0
       1: invokespecial #13                 // Method C."<init>":()V
       4: aload_0
       5: putstatic     #15                 // Field MODULE$:LO$;
       8: aload_0
       9: invokevirtual #21                 // Method java/lang/Object.toString:()Ljava/lang/String;
      12: pop
      13: return
}
```

The static initalizer `<clinit>` calls the constructor `<init>`, which
invokes superclass constructor, assigns `MODULE$= this`, and then runs
the remainder of the object's constructor (`toString` in the example
above.)

It turns out that this relies on a bug in the JVM's verifier: assignment to a
static final must occur lexically within the <clinit>, not from within `<init>`
(even if the latter is happens to be called by the former).

I'd like to move the assignment to <clinit> but that would
change behaviour of "benign" cyclic references between modules.

Example:

```
package p1; class CC { def foo = O.bar}; object O {new CC().foo; def bar = println(1)};

// Exiting paste mode, now interpreting.

scala> p1.O
1
```

This relies on the way that we assign MODULE$ field after the super class constructors
are finished, but before the rest of the module constructor is called.

Instead, this commit removes the ACC_FINAL bit from the field. It actually wasn't
behaving as final at all, precisely the issue that the stricter verifier
now alerts us to.

```
scala> :paste -raw
// Entering paste mode (ctrl-D to finish)

package p1; object O

// Exiting paste mode, now interpreting.

scala> val O1 = p1.O
O1: p1.O.type = p1.O$@ee7d9f1

scala> scala.reflect.ensureAccessible(p1.O.getClass.getDeclaredConstructor()).newInstance()
res0: p1.O.type = p1.O$@64cee07

scala> O1 eq p1.O
res1: Boolean = false
```

We will still achieve safe publication of the assignment to other threads
by virtue of the fact that `<clinit>` is executed within the scope of
an initlization lock, as specified by:

  https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html#jvms-5.5

Fixes scala/scala-dev#SD-194
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants