Skip to content

Commit 2535b97

Browse files
committed
explicitly annotate lifetimes when unsafe is involved
1 parent 7e4ecd9 commit 2535b97

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

core/src/environment.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<K: Hash + Eq, V: PartialEq> Environment<K, V> {
100100

101101
/// Creates an iterator that visits all layers from the most recent one to the oldest.
102102
/// The element iterator type is `Rc<HashMap<K, V>>`.
103-
pub fn iter_layers(&self) -> EnvLayerIter<'_, K, V> {
103+
pub fn iter_layers<'slf>(&'slf self) -> EnvLayerIter<'slf, K, V> {
104104
EnvLayerIter {
105105
env: if !self.was_cloned() {
106106
Some(NonNull::from(self))
@@ -121,7 +121,7 @@ impl<K: Hash + Eq, V: PartialEq> Environment<K, V> {
121121
/// the most recent one. It uses this order, so calling `collect` on this iterator to create a
122122
/// hashmap would have the same values as the Environment. The element iterator type is `(&'env
123123
/// K, &'env V)`, with `'env` being the lifetime of the Environment.
124-
pub fn iter_elems(&self) -> EnvElemIter<'_, K, V> {
124+
pub fn iter_elems<'slf>(&'slf self) -> EnvElemIter<'slf, K, V> {
125125
let mut env: Vec<NonNull<HashMap<K, V>>> = self
126126
.iter_layers()
127127
// SAFETY: Rc::as_ptr never returnes null

core/src/identifier.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ mod interner {
258258
///
259259
/// This operation cannot fails since the only way to have a [Symbol] is to have
260260
/// [interned](Interner::intern) the corresponding string first.
261-
pub(crate) fn lookup(&self, sym: Symbol) -> &str {
261+
pub(crate) fn lookup<'slf>(&'slf self, sym: Symbol) -> &'slf str {
262262
// SAFETY: We are making the returned &str lifetime the same as our struct,
263263
// which is okay here since the InnerInterner uses a typed_arena which prevents
264264
// deallocations, so the reference will be valid while the InnerInterner exists,
265265
// hence while the struct exists.
266-
unsafe { std::mem::transmute(self.0.read().unwrap().lookup(sym)) }
266+
unsafe { std::mem::transmute::<&'_ str, &'slf str>(self.0.read().unwrap().lookup(sym)) }
267267
}
268268
}
269269

@@ -300,8 +300,11 @@ mod interner {
300300
// It is also okay to use it from inside the mutex, since typed_arena does not allow
301301
// deallocation, so references are valid until the arena drop, which is tied to the
302302
// struct drop.
303+
// XXX: we have to use &'a str here, not &'self str like the comment indicates. what's going on?
303304
let in_string = unsafe {
304-
std::mem::transmute(self.arena.lock().unwrap().alloc_str(string.as_ref()))
305+
std::mem::transmute::<&'_ str, &'a str>(
306+
self.arena.lock().unwrap().alloc_str(string.as_ref()),
307+
)
305308
};
306309
let sym = Symbol(self.vec.len() as u32);
307310
self.vec.push(in_string);
@@ -312,7 +315,7 @@ mod interner {
312315
///
313316
/// This operation cannot fails since the only way to have a [Symbol]
314317
/// is to have [interned](InnerInterner::intern) the corresponding string first.
315-
fn lookup(&self, sym: Symbol) -> &str {
318+
fn lookup<'slf>(&'slf self, sym: Symbol) -> &'slf str {
316319
self.vec[sym.0 as usize]
317320
}
318321
}

core/src/term/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl IntoIterator for Array {
155155
// Otherwise, we clone everything.
156156

157157
unsafe {
158-
let mut inner: Rc<[ManuallyDrop<RichTerm>]> = transmute(self.inner);
158+
let mut inner = transmute::<Rc<[RichTerm]>, Rc<[ManuallyDrop<RichTerm>]>>(self.inner);
159159
let idx = self.start;
160160
let end = self.end;
161161

0 commit comments

Comments
 (0)