Skip to content

Commit 45b6de0

Browse files
authored
Merge pull request #147 from jswrenn/unsafe-fields
Propose "unsafe fields" as a 2025H1 Project Goal
2 parents 8d4b4ef + ad5ce07 commit 45b6de0

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

src/2025h1/unsafe-fields.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Unsafe Fields
2+
3+
| Metadata | |
4+
| --- | --- |
5+
| Owner(s) | @jswrenn |
6+
| Teams | [lang] |
7+
| Status | Proposed |
8+
9+
## Summary
10+
11+
Design and implement a mechanism for denoting when fields carry library safety invariants.
12+
13+
## Motivation
14+
15+
The absence of a mechanism for denoting the presence of library safety invariants increases both the risk of working with `unsafe` code and the difficulty of evaluating its soundness.
16+
17+
### The status quo
18+
19+
Presently, Rust lacks mechanisms for denoting when fields carry library safety invariants, and for enforcing extra care around their use. Consequently, to evaluate the soundness of `unsafe` code (i.e., code which relies on safety invariants being upheld), it is not enough to check the contents of `unsafe` blocks — one must check all places (including safe contexts) in which safety invariants might be violated. (See [*The Scope of Unsafe*])
20+
21+
For example, consider this idealized `Vec`:
22+
23+
```rust
24+
pub struct Vec<T> {
25+
data: Box<[MaybeUninit<T>]>,
26+
len: usize,
27+
}
28+
```
29+
30+
Although `len` is bound by a safety invariant, it is trivial to violate its invariant in entirely safe code:
31+
32+
```rust
33+
impl Vec<T> {
34+
pub fn evil(&mut self) {
35+
self.len += 2;
36+
}
37+
}
38+
```
39+
40+
Rust cannot enforce that modifications of `len` require `unsafe`, because the language does not provide the programmer a way of communicating to the compiler that `len` carries safety invariants.
41+
42+
[*The Scope of Unsafe*]: https://www.ralfj.de/blog/2016/01/09/the-scope-of-unsafe.html
43+
44+
### The "shiny future" we are working towards
45+
46+
Rust programmers will use the `unsafe` keyword to denote fields that carry library safety invariants; e.g.:
47+
48+
```rust
49+
struct Vec<T> {
50+
// SAFETY: The elements `data[i]` for
51+
// `i < len` are in a valid state.
52+
unsafe data: Box<[MaybeUninit<T>]>,
53+
unsafe len: usize,
54+
}
55+
```
56+
57+
Rust will require that usages of `unsafe` fields which could violate their safety invariants must *only* occur within `unsafe` contexts.
58+
59+
### The next 6 months
60+
61+
In the next six months, we will iterate on the design and implementation of unsafe fields. An RFC for `unsafe` fields will be accepted, and a candidate implementation will — at the very least — be ready to enter the stabilization process.
62+
63+
## Design axioms
64+
65+
The design of `unsafe` fields is guided by three axioms:
66+
67+
1. **Unsafe Fields Denote Safety Invariants**
68+
A field *should* be marked `unsafe` if it carries arbitrary library safety invariants with respect to its enclosing type.
69+
2. **Unsafe Usage is Always Unsafe**
70+
Uses of `unsafe` fields which could violate their invariants *must* occur in the scope of an `unsafe` block.
71+
3. **Safe Usage is Usually Safe**
72+
Uses of `unsafe` fields which cannot violate their invariants *should not* require an unsafe block.
73+
74+
## Ownership and team asks
75+
76+
**Owner:** @jswrenn
77+
78+
| Subgoal | Owner(s) or team(s) | Notes |
79+
| ---------------------------------------------- | -------------------- | --------- |
80+
| Discussion and moral support | ![Team][] [lang] | [Zulip] |
81+
| Stabilize Unsafe Fields | | |
82+
| ↳ Author RFC | @jhpratt | [RFC3458], [Living Design Doc] |
83+
| ↳ Implementation | @veluca93 | |
84+
| ↳ Standard reviews | ![Team][] [compiler] | |
85+
| ↳ Design meeting | ![Team][] [lang] | |
86+
| ↳ RFC decision | ![Team][] [lang] | |
87+
| ↳ Author stabilization report | @jswrenn | |
88+
89+
[Zulip]: https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/unsafe.20fields.20RFC
90+
[RFC3458]: https://github.com/rust-lang/rfcs/pull/3458
91+
[Living Design Doc]: https://hackmd.io/SJqXa_8lJe
92+
93+
### Definitions
94+
95+
Definitions for terms used above:
96+
97+
* *Discussion and moral support* is the lowest level offering, basically committing the team to nothing but good vibes and general support for this endeavor.
98+
* *Author RFC* and *Implementation* means actually writing the code, document, whatever.
99+
* *Design meeting* means holding a synchronous meeting to review a proposal and provide feedback (no decision expected).
100+
* *RFC decisions* means reviewing an RFC and deciding whether to accept.
101+
* *Org decisions* means reaching a decision on an organizational or policy matter.
102+
* *Secondary review* of an RFC means that the team is "tangentially" involved in the RFC and should be expected to briefly review.
103+
* *Stabilizations* means reviewing a stabilization and report and deciding whether to stabilize.
104+
* *Standard reviews* refers to reviews for PRs against the repository; these PRs are not expected to be unduly large or complicated.
105+
* *Prioritized nominations* refers to prioritized lang-team response to nominated issues, with the expectation that there will be *some* response from the next weekly triage meeting.
106+
* *Dedicated review* means identifying an individual (or group of individuals) who will review the changes, as they're expected to require significant context.
107+
* Other kinds of decisions:
108+
* [Lang team experiments](https://lang-team.rust-lang.org/how_to/experiment.html) are used to add nightly features that do not yet have an RFC. They are limited to trusted contributors and are used to resolve design details such that an RFC can be written.
109+
* Compiler [Major Change Proposal (MCP)](https://forge.rust-lang.org/compiler/mcp.html) is used to propose a 'larger than average' change and get feedback from the compiler team.
110+
* Library [API Change Proposal (ACP)](https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html) describes a change to the standard library.
111+
112+
## Frequently asked questions
113+
114+
TBD

0 commit comments

Comments
 (0)