Skip to content

generalize the Option<NonNullablePtr> to libs #8678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/libextra/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ struct RcBox<T> {
}

/// Immutable reference counted pointer type
#[unsafe_no_drop_flag]
#[no_send]
#[unsafe_no_drop_flag]
#[unsafe_non_zero_word]
pub struct Rc<T> {
priv ptr: *mut RcBox<T>,
}
Expand Down Expand Up @@ -167,6 +168,7 @@ struct RcMutBox<T> {
#[no_send]
#[no_freeze]
#[unsafe_no_drop_flag]
#[unsafe_non_zero_word]
pub struct RcMut<T> {
priv ptr: *mut RcMutBox<T>,
}
Expand Down Expand Up @@ -255,6 +257,7 @@ impl<T: DeepClone> DeepClone for RcMut<T> {
#[cfg(test)]
mod test_rc_mut {
use super::*;
use std::sys::size_of;

#[test]
fn test_clone() {
Expand Down Expand Up @@ -372,4 +375,10 @@ mod test_rc_mut {
do y.with_mut_borrow |_| {}
}
}

#[test]
fn non_zero_word() {
assert_eq!(size_of::<Option<Rc<int>>>(), size_of::<Rc<int>>());
assert_eq!(size_of::<Option<RcMut<int>>>(), size_of::<RcMut<int>>());
}
}
6 changes: 3 additions & 3 deletions src/librustc/middle/trans/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
fn is_zerolen(&self, cx: &mut CrateContext) -> bool {
mk_struct(cx, self.tys, false).size == 0
}
fn find_ptr(&self) -> Option<uint> {
self.tys.iter().position(|&ty| mono_data_classify(ty) == MonoNonNull)
fn find_ptr(&self, tcx: ty::ctxt) -> Option<uint> {
self.tys.iter().position(|&ty| mono_data_classify(tcx, ty) == MonoNonNull)
}
}

Expand Down Expand Up @@ -187,7 +187,7 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
let mut discr = 0;
while discr < 2 {
if cases[1 - discr].is_zerolen(cx) {
match cases[discr].find_ptr() {
match cases[discr].find_ptr(cx.tcx) {
Some(ptrfield) => {
return NullablePointer {
nndiscr: discr,
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,14 +904,16 @@ pub enum MonoDataClass {
MonoFloat
}

pub fn mono_data_classify(t: ty::t) -> MonoDataClass {
pub fn mono_data_classify(tcx: ty::ctxt, t: ty::t) -> MonoDataClass {
match ty::get(t).sty {
ty::ty_float(_) => MonoFloat,
ty::ty_rptr(*) | ty::ty_uniq(*) |
ty::ty_box(*) | ty::ty_opaque_box(*) |
ty::ty_estr(ty::vstore_uniq) | ty::ty_evec(_, ty::vstore_uniq) |
ty::ty_estr(ty::vstore_box) | ty::ty_evec(_, ty::vstore_box) |
ty::ty_bare_fn(*) => MonoNonNull,
ty::ty_struct(id, _) | ty::ty_enum(id, _)
if ty::has_attr(tcx, id, "unsafe_non_zero_word") => MonoNonNull,
// Is that everything? Would closures or slices qualify?
_ => MonoBits
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/monomorphize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ pub fn make_mono_id(ccx: @mut CrateContext,
let size = machine::llbitsize_of_real(ccx, llty);
let align = machine::llalign_of_min(ccx, llty);
let mode = datum::appropriate_mode(ccx.tcx, subst);
let data_class = mono_data_classify(subst);
let data_class = mono_data_classify(ccx.tcx, subst);

debug!("make_mono_id: type %s -> size %u align %u mode %? class %?",
ty_to_str(ccx.tcx, subst),
Expand Down
20 changes: 20 additions & 0 deletions src/test/run-pass/attr-unsafe_non_zero_word.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2013 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.

use std::sys::size_of;

#[unsafe_non_zero_word]
struct Test {
foo: *int
}

fn main() {
assert_eq!(size_of::<Option<Test>>(), size_of::<*int>());
}