Skip to content

Commit 9fd6c72

Browse files
committed
Revise suggestion for the Arena API.
Namely, now officially propose that `Arena` be changed to `Arena<'a>`. (As an aside, suggest that we *could* also provide the old API with an `UnsafeArena` type; but that is not part of the proposal proper).
1 parent 4b4e5f5 commit 9fd6c72

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

text/0000-sound-generic-drop.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,33 @@ thread '<main>' panicked at 'assertion failed: neighbor.usable', ../src/test/com
768768
This is unsound. It should not be possible to express such a
769769
scenario without using `unsafe` code.
770770

771-
This RFC suggests is that we deprecate the `Arena` type (which is not
772-
marked as stable anyway). We might also attempt find a way to revise
773-
its API to not allow a case like the above, but that is out of scope
774-
for this RFC.
771+
This RFC suggests that we revise the `Arena` API by adding a phantom
772+
lifetime parameter to its type, and bound the values the arena
773+
allocates by that phantom lifetime, like so:
774+
```rust
775+
pub struct Arena<'longer_than_self> {
776+
_invariant: marker::InvariantLifetime<'longer_than_self>,
777+
...
778+
}
779+
780+
impl<'longer_than_self> Arena<'longer_than_self> {
781+
pub fn alloc<T:'longer_than_self, F>(&self, op: F) -> &mut T
782+
where F: FnOnce() -> T {
783+
...
784+
}
785+
}
786+
```
787+
Admittedly, this is a severe limitation, since it forces the data
788+
allocated by the Arena to store only references to data that strictly
789+
outlives the arena, regardless of whether the allocated data itself
790+
even has a destructor. (I.e., `Arena` would become much weaker than
791+
`TypedArena` when attempting to work with cyclic structures).
792+
(pnkfelix knows of no way to fix this without adding further extensions
793+
to the language, e.g. some way to express "this type's destructor accesses
794+
none of its borrowed data", which is out of scope for this RFC.)
795+
796+
Alternatively, we could just deprecate the `Arena` API, (which is not
797+
marked as stable anyway.
775798

776799
The example given here can be adapted to other kinds of backing
777800
storage structures, in order to double-check whether the API is likely
@@ -788,15 +811,24 @@ remove `#[unsafe_destructor]`!
788811

789812
# Drawbacks
790813

791-
The Drop-Check rule is a little complex, and does disallow some
792-
sound code that would compile today.
793-
794-
The change proposed in this RFC places restrictions on uses of types
795-
with attached destructors, but provides no way for a type `Foo<'a>` to
796-
state as part of its public interface that its drop implementation
797-
will not read from any borrowed data of lifetime `'a`. (Extending the
798-
language with such a feature is potential future work, but is out of
799-
scope for this RFC.)
814+
* The Drop-Check rule is a little complex, and does disallow some
815+
sound code that would compile today.
816+
817+
* The change proposed in this RFC places restrictions on uses of types
818+
with attached destructors, but provides no way for a type `Foo<'a>` to
819+
state as part of its public interface that its drop implementation
820+
will not read from any borrowed data of lifetime `'a`. (Extending the
821+
language with such a feature is potential future work, but is out of
822+
scope for this RFC.)
823+
824+
* Some useful interfaces are going to be disallowed by this RFC.
825+
For example, the RFC recommends that the current `arena::Arena`
826+
be revised or simply deprecated, due to its unsoundness.
827+
(If desired, we could add an `UnsafeArena` that continues
828+
to support the current `Arena` API with the caveat that its users need to
829+
*manually* enforce the constraint that the destructors do not access
830+
data that has been already dropped. But again, that decision is out
831+
of scope for this RFC.)
800832

801833
# Alternatives
802834

0 commit comments

Comments
 (0)