Skip to content

Commit 2d86ca2

Browse files
committed
Update docs
1 parent 1185acd commit 2d86ca2

File tree

1 file changed

+19
-82
lines changed

1 file changed

+19
-82
lines changed

docs/docs/reference/other-new-features/safe-initialization.md

Lines changed: 19 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -201,91 +201,29 @@ With the established principles and design goals, following rules are imposed:
201201
reasoning about initialization: programmers may safely assume that all local
202202
definitions only point to transitively initialized objects.
203203

204-
## Modularity (considered)
204+
## Modularity
205205

206-
Currently, the analysis works across project boundaries based on TASTy.
207-
The following is a proposal to make the checking more modular.
208-
The feedback from the community is welcome.
206+
The analysis takes the primary constructor of concrete classes as entry points.
207+
It follows the constructors of super classes, which might be defined in another project.
208+
The analysis takes advantage of TASTy for analyzing super classes defined in another project.
209209

210-
For modularity, we need to forbid subtle initialization interaction beyond
211-
project boundaries. For example, the following code passes the check when the
212-
two classes are defined in the same project:
210+
The crossing of project boundary raises a concern about modularity. It is
211+
well-known in object-oriented programming that superclass and subclass are
212+
tightly coupled. For example, adding a method in the superclass requires
213+
recompiling the child class for checking safe overriding.
213214

214-
```Scala
215-
class Base:
216-
private val map: mutable.Map[Int, String] = mutable.Map.empty
217-
def enter(k: Int, v: String) = map(k) = v
215+
Initialization is no exception in this respect. The initialization of an object
216+
essentially invovles close interaction between subclass and superclass. If the
217+
superclass is defined in another project, the crossing of project boundary
218+
cannot be avoided for soundness of the analysis.
218219

219-
class Child extends Base:
220-
enter(1, "one")
221-
enter(2, "two")
222-
```
223-
224-
However, when the class `Base` and `Child` are defined in two different
225-
projects, the check can emit a warning for the calls to `enter` in the class
226-
`Child`. This restricts subtle initialization within project boundaries,
227-
and avoids accidental violation of contracts across library versions.
228-
229-
We can impose the following rules to enforce modularity:
230-
231-
1. A class or trait that may be extended in another project should not
232-
call _virtual_ methods on `this` in its template/mixin evaluation,
233-
directly or indirectly.
234-
235-
2. The method call `o.m(args)` is forbidden if `o` is not transitively
236-
initialized and the target of `m` is defined in an external project.
237-
238-
3. The expression `new p.C(args)` is forbidden, if `p` is not transitively
239-
initialized and `C` is defined in an external project.
240-
241-
## Theory
242-
243-
The theory is based on type-and-effect systems [2, 3]. We introduce two concepts,
244-
_effects_ and _potentials_:
245-
246-
```
247-
π = this | Warm(C, π) | π.f | π.m | π.super[D] | Cold | Fun(Π, Φ) | π.outer[C]
248-
ϕ = π↑ | π.f! | π.m!
249-
```
250-
251-
Potentials (π) represent values that are possibly under initialization.
252-
253-
- `this`: current object
254-
- `Warm(C, π)`: an object of type `C` where all its fields are assigned, and the potential for `this` of its enclosing class is `π`.
255-
- `π.f`: the potential of the field `f` in the potential `π`
256-
- `π.m`: the potential of the field `f` in the potential `π`
257-
- `π.super[D]`: essentially the object π, used for virtual method resolution
258-
- `Cold`: an object with unknown initialization status
259-
- `Fun(Π, Φ)`: a function, when called produce effects Φ and return potentials Π.
260-
- `π.outer[C]`: the potential of `this` for the enclosing class of `C` when `C.this` is `π`.
261-
262-
Effects are triggered from potentials:
263-
264-
- `π↑`: promote the object pointed to by the potential `π` to fully-initialized
265-
- `π.f!`: access field `f` on the potential `π`
266-
- `π.m!`: call the method `m` on the potential `π`
267-
268-
To ensure that the checking always terminate and for better
269-
performance, we restrict the length of potentials to be finite (by
270-
default 2). If the potential is too long, the checker stops
271-
tracking it by checking that the potential is actually transitively
272-
initialized.
273-
274-
For an expression `e`, it may be summarized by the pair `(Π, Φ)`,
275-
which means evaluation of `e` may produce the effects Φ and return the
276-
potentials Π. Each field and method is associated with such a pair.
277-
We call such a pair _summary_. The expansion of proxy potentials and effects,
278-
such as `π.f`, `π.m` and `π.m!`, will take advantage of the summaries.
279-
Depending on the potential `π` for `this`, the summaries need to be rebased (`asSeenFrom`) before usage.
280-
281-
The checking treats the templates of concrete classes as entry points.
282-
It maintains the set of initialized fields as initialization
283-
progresses, and check that only initialized fields are accessed during
284-
the initialization and there is no leaking of values under initialization.
285-
Virtual method calls on `this` is not a problem,
286-
as they can always be resolved statically.
220+
Meanwhile, inheritance across project boundary has been under scrutiny and the
221+
introduction of [open classes](./open-classes.html) mitigate the concern here.
222+
For example, the initialization check could enforce that the constructors of
223+
open classes may not contain method calls on `this` or introduce annotations as
224+
a contract.
287225

288-
For a more detailed introduction of the theory, please refer to the paper _a type-and-effect system for safe initialization_ [3].
226+
The feedback from the community on the topic is welcome.
289227

290228
## Back Doors
291229

@@ -302,5 +240,4 @@ mark some fields as lazy.
302240
## References
303241

304242
1. Fähndrich, M. and Leino, K.R.M., 2003, July. [_Heap monotonic typestates_](https://www.microsoft.com/en-us/research/publication/heap-monotonic-typestate/). In International Workshop on Aliasing, Confinement and Ownership in object-oriented programming (IWACO).
305-
2. Lucassen, J.M. and Gifford, D.K., 1988, January. [_Polymorphic effect systems_](https://dl.acm.org/doi/10.1145/73560.73564). In Proceedings of the 15th ACM SIGPLAN-SIGACT symposium on Principles of programming languages (pp. 47-57). ACM.
306-
3. Fengyun Liu, Ondřej Lhoták, Aggelos Biboudis, Paolo G. Giarrusso, and Martin Odersky. 2020. [_A type-and-effect system for object initialization_](https://dl.acm.org/doi/10.1145/3428243). OOPSLA, 2020.
243+
2. Fengyun Liu, Ondřej Lhoták, Aggelos Biboudis, Paolo G. Giarrusso, and Martin Odersky. 2020. [_A type-and-effect system for object initialization_](https://dl.acm.org/doi/10.1145/3428243). OOPSLA, 2020.

0 commit comments

Comments
 (0)