Skip to content

Commit 0a8d6aa

Browse files
committed
Inital draft of @binaryCompatible proposal.
1 parent af0be46 commit 0a8d6aa

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
layout: sip
3+
title: SIP XX - Improving binary compatibility with @binaryCompatible
4+
disqus: true
5+
---
6+
7+
__Dmitry Petrashko__
8+
9+
__first submitted 13 February 2017__
10+
11+
## Introduction ##
12+
13+
Scala is a language which evolves fast and thus made a decision to only promise binary compatibility across minor releases.
14+
At the same time, there is a demand to develop APIs that live longer than a major release cycle of Scala.
15+
This SIP introduces an annotation `@binaryCompatible` that checks that `what you write is what you get`.
16+
It will fail compilation in case emitted methods or their signatures
17+
are different from those written by users.
18+
As long as signatures of methods in source is not changed, `@binaryCompatible` annotated class
19+
will be compatible across major version of Scala.
20+
21+
## Use Cases
22+
Dotty currently uses java defined interfaces as public API for SBT in order to ensure binary compatibility.
23+
These interfaces can be replaced by `@binaryCompatible` annotated traits to reach the same goal.
24+
25+
## Design Guidelines
26+
`@binaryCompatible` is a feature which is supposed to be used by a small subset of the ecosystem to be binary compatible across major versions of Scala.
27+
Thus this is designed as an advanced feature that is used rarely and thus is intentionally verbose.
28+
It's designed to provide strong guarantees, in some cases sacrificing ease of use.
29+
30+
The limitations enforced by `@binaryCompatible` are designed to be an overapproximation:
31+
instead of permitting a list of features known to be compatible, `@binaryCompatible` enforces a stronger
32+
check which is sufficient to promise binary compatibility.
33+
34+
## Overview ##
35+
In order for a class or a trait to succeed compilation with the `@binaryCompatible` annotation it has to be:
36+
- defined on the top level;
37+
- use a subset of Scala that during compilation does not require changes to public API of the class, including
38+
- synthesizing new members, either concrete or abstract;
39+
- changing binary signatures of existing members, either concrete or abstract;
40+
41+
`@binaryCompatible` does not change the compilation scheme of a class:
42+
compiling a class previously annotated with the `@binaryCompatible`, will produce the same bytecode with or without `@binaryCompatible` annotation.
43+
44+
Below are several examples of classes and traits that succeed compilation with `@binaryCompatible`
45+
```scala
46+
{% highlight scala %}
47+
@binaryCompatible
48+
trait AbstractFile {
49+
def name(): String
50+
51+
def path(): String
52+
53+
def jfile(): Optional[File]
54+
}
55+
56+
@binaryCompatible
57+
trait SourceFile extends AbstractFile {
58+
/** @return The content of this file as seen by the compiler. */
59+
def content(): Array[Char]
60+
}
61+
62+
@binaryCompatible
63+
trait Diagnostic {
64+
def message(): String
65+
66+
def level(): Int
67+
68+
def position(): Optional[SourcePosition]
69+
}
70+
71+
@binaryCompatible
72+
object Diagnostic {
73+
@static final val ERROR: Int = 2
74+
@static final val WARNING: Int = 1
75+
@static final val INFO: Int = 0
76+
}
77+
78+
{% endhighlight %}
79+
```
80+
81+
## Features that will fail compilation with `@binaryCompatible`
82+
The features listed below have complex encodings that may change in future versions. We prefer not to compromise on them.
83+
Most of those features can be simulated in a binary compatible way by writing a verbose re-impelemtation
84+
which won't rely on desugaring performed inside compiler.
85+
86+
- public fields. Can be simulated by explicitly defining public getters and setters that access a private field;
87+
- lazy vals. Can be simulated by explicitly writing an implementation in source;
88+
- case classes. Can be simulated by explicitly defining getters and other members synthesized for a case class(`copy`, `productArity`, `apply`, `unApply`, `unapply`).
89+
90+
The features listed below cannot be easily re-implemented in a class or trait annotated with `@binaryCompatible`.
91+
- default arguments;
92+
- default methods. See Addendum;
93+
- constant types(both explicit and inferred);
94+
- inline.
95+
96+
## `@binaryCompatible` and Scala.js
97+
98+
Allowing to write API-defining classes in Scala instead of Java will allow them to compile with Scala.js,
99+
which would have benefit of sharing the same source for two ecosystems.
100+
101+
Scala.js currently is binary compatible as long as original bytecode compiled by Scala JVM is binary compatible.
102+
Providing stronger binary compatibility guarantees for JVM will automatically provide stronger guarantees for Scala.js.
103+
104+
105+
## Comparison with MiMa ##
106+
The Migration Manager for Scala (MiMa in short) is a tool for diagnosing binary incompatibilities for Scala libraries.
107+
MiMa allows to compare binary APIs of two already compiled classfiles and reports errors if APIs do not match perfectly.
108+
109+
MiMa and `@binaryCompatible` complement each other, as `@binaryCompatible` helps to develop APIs that stay compatible
110+
across major versions, while MiMa checks that previously published artifacts indeed have the same API.
111+
112+
`@binaryCompatible` does not compare the currently compiled class or trait against previous version,
113+
so introduction of new members won't be prohibited. This is a use-case for MiMa.
114+
115+
MiMa does not indicate how hard, if possible, would it be to maintain compatibility of a class across future versions of Scala.
116+
Multiple features of Scala, most notably lazy vals and traits, has been compiled diffently by different Scala versions
117+
making porting existing compiled bytecode across versions very hard.
118+
MiMa will complain retroactively that the new version is incompatible with the old one.
119+
`@binaryCompatible` will instead indicate at compile time that the old version had used features whose encoding is prone to change.
120+
This provides early guidance and warning when designing long-living APIs before they are publicly released.
121+
122+
## Compilation scheme ##
123+
No modification of typer or any existing phase is planned. The current proposed scheme introduces a late phase that runs before the very bytecode emission that checks that:
124+
- classes and traits annotated as `@binaryCompatible` are on the top level;
125+
- no non-private members where introduced inside classes and traits annotated as `@binaryCompatible` by compiler using phase travel;
126+
- no non-private members inside classes and traits annotated as `@binaryCompatible` has changed their signature from the one written by developer.
127+
128+
The current prototype is implemented for Dotty and supports everything descibed in this SIP.
129+
The implementation is simple with less than 50 lines of non-boilerplate code.
130+
The current implementation has a scope for improvement of error messages that will report domain specific details for disallowed features, but it already prohibits them.
131+
132+
## Addendum: Default methods ##
133+
By `default methods` we mean non-abstract methods defined and implemented by a trait.
134+
135+
The way how those methods are implemented by compiler has changed substantially over years.
136+
At the same time, `invokeinterface` has always been a reliable way to invoke such a method,
137+
independently from how it was implemented under the hood.
138+
139+
One might reason that, as there has been a reliable way to call methods on the binary level,
140+
it should be allowed to use them in binary compatible APIs.
141+
142+
At the same time, the mixin composition protocol that is followed when a class inherits those traits has also
143+
changed substantially.
144+
The classes which have been correctly inheriting those traits compiled by previous versions of Scala
145+
may need recompilation if trait has been recompiled with a new major version of Scala.
146+
147+
Thus, the authors of this SIP has decided not to allow default methods in the
148+
`@binaryCompatible` traits.
149+
150+
## See Also ##
151+
* [dotty#1900](https://github.com/lampepfl/dotty/pull/1900) is an implementation for Dotty
152+
* [MiMa](https://github.com/typesafehub/migration-manager)

0 commit comments

Comments
 (0)