Skip to content

Commit 7e2e4ce

Browse files
committed
return &mut T from the arenas, not &T
The arenas write the value to memory and then return a non-aliasing reference to it. The returned reference can be mutable and can be coerced to an immutable one. [breaking-change]
1 parent e05c3b7 commit 7e2e4ce

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/libarena/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ impl Arena {
208208
}
209209

210210
#[inline]
211-
fn alloc_copy<T>(&self, op: || -> T) -> &T {
211+
fn alloc_copy<T>(&self, op: || -> T) -> &mut T {
212212
unsafe {
213213
let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
214214
mem::min_align_of::<T>());
215215
let ptr = ptr as *mut T;
216216
ptr::write(&mut (*ptr), op());
217-
return &*ptr;
217+
return &mut *ptr;
218218
}
219219
}
220220

@@ -262,7 +262,7 @@ impl Arena {
262262
}
263263

264264
#[inline]
265-
fn alloc_noncopy<T>(&self, op: || -> T) -> &T {
265+
fn alloc_noncopy<T>(&self, op: || -> T) -> &mut T {
266266
unsafe {
267267
let tydesc = get_tydesc::<T>();
268268
let (ty_ptr, ptr) =
@@ -279,14 +279,14 @@ impl Arena {
279279
// the object is there.
280280
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
281281

282-
return &*ptr;
282+
return &mut *ptr;
283283
}
284284
}
285285

286286
/// Allocates a new item in the arena, using `op` to initialize the value,
287287
/// and returns a reference to it.
288288
#[inline]
289-
pub fn alloc<T>(&self, op: || -> T) -> &T {
289+
pub fn alloc<T>(&self, op: || -> T) -> &mut T {
290290
unsafe {
291291
if intrinsics::needs_drop::<T>() {
292292
self.alloc_noncopy(op)
@@ -459,12 +459,12 @@ impl<T> TypedArena<T> {
459459

460460
/// Allocates an object in the `TypedArena`, returning a reference to it.
461461
#[inline]
462-
pub fn alloc(&self, object: T) -> &T {
462+
pub fn alloc(&self, object: T) -> &mut T {
463463
if self.ptr == self.end {
464464
self.grow()
465465
}
466466

467-
let ptr: &T = unsafe {
467+
let ptr: &mut T = unsafe {
468468
let ptr: &mut T = mem::transmute(self.ptr);
469469
ptr::write(ptr, object);
470470
self.ptr.set(self.ptr.get().offset(1));

src/librustc/middle/typeck/variance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
715715
}
716716

717717
_ => {
718-
self.terms_cx.arena.alloc(|| TransformTerm(v1, v2))
718+
&*self.terms_cx.arena.alloc(|| TransformTerm(v1, v2))
719719
}
720720
}
721721
}

0 commit comments

Comments
 (0)