@@ -82,38 +82,39 @@ to satisfy the conditions required to perform this unsafe operation.
82
82
# Reference-level explanation
83
83
[ reference-level-explanation ] : #reference-level-explanation
84
84
85
- First of all, we no longer warn that an ` unsafe ` block is unnecessary when it is
86
- nested immediately inside an ` unsafe fn ` . So, the following compiles without
87
- any warning:
85
+ 1 . First of all, we no longer warn that an ` unsafe ` block is unnecessary when it is
86
+ nested immediately inside an ` unsafe fn ` . So, the following compiles without
87
+ any warning:
88
88
89
- ``` rust
90
- unsafe fn get_unchecked <T >(x : & [T ], i : usize ) -> & T {
91
- unsafe { x . get_unchecked (i ) }
92
- }
93
- ```
89
+ ``` rust
90
+ unsafe fn get_unchecked <T >(x : & [T ], i : usize ) -> & T {
91
+ unsafe { x . get_unchecked (i ) }
92
+ }
93
+ ```
94
94
95
- However, nested ` unsafe ` blocks are still redundant, so this warns:
95
+ However , nested `unsafe ` blocks are still redundant , so this warns :
96
96
97
- ``` rust
98
- unsafe fn get_unchecked <T >(x : & [T ], i : usize ) -> & T {
99
- unsafe { unsafe { x . get_unchecked (i ) } }
100
- }
101
- ```
97
+ ```rust
98
+ unsafe fn get_unchecked <T >(x : & [T ], i : usize ) -> & T {
99
+ unsafe { unsafe { x . get_unchecked (i ) } }
100
+ }
101
+ ```
102
102
103
- In a next step, we have a lint that fires when an unsafe operation is performed
104
- inside an ` unsafe fn ` but outside an ` unsafe ` block. So, this would trigger the
105
- lint:
103
+ 2 . In a next step , we have a lint that fires when an unsafe operation is performed
104
+ inside an `unsafe fn ` but outside an `unsafe ` block . So , this would trigger the
105
+ lint :
106
106
107
- ``` rust
108
- unsafe fn get_unchecked <T >(x : & [T ], i : usize ) -> & T {
109
- x . get_unchecked (i )
110
- }
111
- ```
107
+ ```rust
108
+ unsafe fn get_unchecked <T >(x : & [T ], i : usize ) -> & T {
109
+ x . get_unchecked (i )
110
+ }
111
+ ```
112
112
113
- This gets us into a state where programmers are much less likely to accidentally
114
- perform undesired unsafe operations inside ` unsafe fn ` .
113
+ This gets us into a state where programmers are much less likely to accidentally
114
+ perform undesired unsafe operations inside `unsafe fn `.
115
115
116
- Even later, it might be desirable to turn this warning into an error.
116
+ 3 . Even later (in the 2021 edition ), it might be desirable to turn this warning
117
+ into an error .
117
118
118
119
# Drawbacks
119
120
[drawbacks ]: #drawbacks
@@ -131,6 +132,10 @@ I explained the rationale in the motivation section.
131
132
132
133
The alternative is to not do anything , and live with the current situation .
133
134
135
+ We could introduce named proof obligations (proposed by @ Centril ) such that the
136
+ compiler can be be told (to some extend ) if the assumptions made by the `unsafe
137
+ fn ` are sufficient to discharge the requirements of the unsafe operations .
138
+
134
139
# Prior art
135
140
[prior - art ]: #prior - art
136
141
0 commit comments