Skip to content

Commit

Permalink
fix Option<~ZeroSizeType>
Browse files Browse the repository at this point in the history
1778b63 provided the guarantee of no
`exchange_free` calls for ~ZeroSizeType, so a sentinel can now be used
without overhead.

Closes #11998
  • Loading branch information
thestinger committed Apr 2, 2014
1 parent 3786b55 commit 05d0045
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
21 changes: 14 additions & 7 deletions src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use middle::typeck::MethodCall;
use util::common::indenter;
use util::ppaux::Repr;
use util::nodemap::NodeMap;
use middle::trans::machine::llsize_of;
use middle::trans::machine::{llsize_of, llsize_of_alloc};
use middle::trans::type_::Type;

use std::slice;
Expand Down Expand Up @@ -1200,12 +1200,19 @@ fn trans_boxed_expr<'a>(bcx: &'a Block<'a>,
let size = llsize_of(bcx.ccx(), llty);
let Result { bcx: bcx, val: val } = malloc_raw_dyn(bcx, contents_ty,
heap_exchange, size);
let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
fcx.schedule_free_value(cleanup::CustomScope(custom_cleanup_scope),
val, heap_exchange);
let bcx = trans_into(bcx, contents, SaveIn(val));
fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
immediate_rvalue_bcx(bcx, val, box_ty).to_expr_datumblock()
// Unique boxes do not allocate for zero-size types. The standard library may assume
// that `free` is never called on the pointer returned for `~ZeroSizeType`.
if llsize_of_alloc(bcx.ccx(), llty) == 0 {
let bcx = trans_into(bcx, contents, SaveIn(val));
immediate_rvalue_bcx(bcx, val, box_ty).to_expr_datumblock()
} else {
let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
fcx.schedule_free_value(cleanup::CustomScope(custom_cleanup_scope),
val, heap_exchange);
let bcx = trans_into(bcx, contents, SaveIn(val));
fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
immediate_rvalue_bcx(bcx, val, box_ty).to_expr_datumblock()
}
} else {
let base::MallocResult { bcx, smart_ptr: bx, body } =
base::malloc_general(bcx, contents_ty, heap);
Expand Down
13 changes: 11 additions & 2 deletions src/libstd/rt/global_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,21 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
}
}

// The compiler never calls `exchange_free` on ~ZeroSizeType, so zero-size
// allocations can point to this `static`. It would be incorrect to use a null
// pointer, due to enums assuming types like unique pointers are never null.
static EMPTY: () = ();

/// The allocator for unique pointers without contained managed pointers.
#[cfg(not(test))]
#[lang="exchange_malloc"]
#[inline]
pub unsafe fn exchange_malloc(size: uint) -> *u8 {
malloc_raw(size) as *u8
pub unsafe fn exchange_malloc(size: uint) -> *mut u8 {
if size == 0 {
&EMPTY as *() as *mut u8
} else {
malloc_raw(size)
}
}

// FIXME: #7496
Expand Down
19 changes: 19 additions & 0 deletions src/test/run-pass/empty-allocation-non-null.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub fn main() {
assert!(Some(~()).is_some());

struct Foo;
assert!(Some(~Foo).is_some());

let xs: ~[()] = ~[];
assert!(Some(xs).is_some());
}

12 comments on commit 05d0045

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging thestinger/rust/zero_size_non_null = 05d0045 into auto

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thestinger/rust/zero_size_non_null = 05d0045 merged ok, testing candidate = 13dc788d

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging thestinger/rust/zero_size_non_null = 05d0045 into auto

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thestinger/rust/zero_size_non_null = 05d0045 merged ok, testing candidate = f49c8764

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging thestinger/rust/zero_size_non_null = 05d0045 into auto

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thestinger/rust/zero_size_non_null = 05d0045 merged ok, testing candidate = 72bcdf06

@bors
Copy link
Contributor

@bors bors commented on 05d0045 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.