Skip to content

Adding support for regions in unsafe pointers #7967

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 13 commits 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
4 changes: 2 additions & 2 deletions doc/tutorial-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ use std::unstable::intrinsics;

// a wrapper around the handle returned by the foreign code
pub struct Unique<T> {
priv ptr: *mut T
priv ptr: *'static mut T
}

impl<T: Send> Unique<T> {
pub fn new(value: T) -> Unique<T> {
unsafe {
let ptr = malloc(std::sys::size_of::<T>() as size_t) as *mut T;
let ptr = malloc(std::sys::size_of::<T>() as size_t) as *'static mut T;
assert!(!ptr::is_null(ptr));
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
intrinsics::move_val_init(&mut *ptr, value);
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn check_poison(is_mutex: bool, failed: bool) {

#[doc(hidden)]
struct PoisonOnFail {
failed: *mut bool,
failed: *'static mut bool,
}

impl Drop for PoisonOnFail {
Expand Down
15 changes: 7 additions & 8 deletions src/libextra/c_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use std::ptr;
*
*/
pub struct CVec<T> {
priv base: *mut T,
priv base: *'static mut T,
priv len: uint,
priv rsrc: @DtorRes
}
Expand Down Expand Up @@ -82,12 +82,12 @@ fn DtorRes(dtor: Option<@fn()>) -> DtorRes {
* * base - A foreign pointer to a buffer
* * len - The number of elements in the buffer
*/
pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
return CVec{
pub unsafe fn CVec<T>(base: *'static mut T, len: uint) -> CVec<T> {
CVec {
base: base,
len: len,
rsrc: @DtorRes(option::None)
};
}
}

/**
Expand All @@ -101,13 +101,12 @@ pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
* * dtor - A function to run when the value is destructed, useful
* for freeing the buffer, etc.
*/
pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: @fn())
-> CVec<T> {
return CVec{
pub unsafe fn c_vec_with_dtor<T>(base: *'static mut T, len: uint, dtor: @fn()) -> CVec<T> {
CVec {
base: base,
len: len,
rsrc: @DtorRes(option::Some(dtor))
};
}
}

/*
Expand Down
8 changes: 6 additions & 2 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct DList<T> {
}

type Link<T> = Option<~Node<T>>;
struct Rawlink<T> { priv p: *mut T }
struct Rawlink<T> { priv p: *'static mut T }

struct Node<T> {
priv next: Link<T>,
Expand Down Expand Up @@ -77,7 +77,11 @@ impl<T> Rawlink<T> {

/// Like Option::Some for Rawlink
fn some(n: &mut T) -> Rawlink<T> {
Rawlink{p: ptr::to_mut_unsafe_ptr(n)}
unsafe {
Rawlink {
p: cast::transmute(ptr::to_mut_unsafe_ptr(n))
}
}
}

/// Convert the `Rawlink` into an Option value
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ mod test {

#[test]
fn test_interface_unwrap() {
let mut f = from_value(~"fail");
let f = from_value(~"fail");
assert_eq!(f.unwrap(), ~"fail");
}

Expand Down
4 changes: 2 additions & 2 deletions src/libextra/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct RcBox<T> {
#[unsafe_no_drop_flag]
#[no_send]
pub struct Rc<T> {
priv ptr: *mut RcBox<T>,
priv ptr: *'static mut RcBox<T>,
}

impl<T> Rc<T> {
Expand Down Expand Up @@ -170,7 +170,7 @@ struct RcMutBox<T> {
#[no_freeze]
#[unsafe_no_drop_flag]
pub struct RcMut<T> {
priv ptr: *mut RcMutBox<T>,
priv ptr: *'static mut RcMutBox<T>,
}

impl<T> RcMut<T> {
Expand Down
32 changes: 16 additions & 16 deletions src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,41 +194,41 @@ pub enum AsmDialect {

// Opaque pointer types
pub enum Module_opaque {}
pub type ModuleRef = *Module_opaque;
pub type ModuleRef = *'static Module_opaque;
pub enum Context_opaque {}
pub type ContextRef = *Context_opaque;
pub type ContextRef = *'static Context_opaque;
pub enum Type_opaque {}
pub type TypeRef = *Type_opaque;
pub type TypeRef = *'static Type_opaque;
pub enum Value_opaque {}
pub type ValueRef = *Value_opaque;
pub type ValueRef = *'static Value_opaque;
pub enum BasicBlock_opaque {}
pub type BasicBlockRef = *BasicBlock_opaque;
pub type BasicBlockRef = *'static BasicBlock_opaque;
pub enum Builder_opaque {}
pub type BuilderRef = *Builder_opaque;
pub type BuilderRef = *'static Builder_opaque;
pub enum ExecutionEngine_opaque {}
pub type ExecutionEngineRef = *ExecutionEngine_opaque;
pub type ExecutionEngineRef = *'static ExecutionEngine_opaque;
pub enum MemoryBuffer_opaque {}
pub type MemoryBufferRef = *MemoryBuffer_opaque;
pub type MemoryBufferRef = *'static MemoryBuffer_opaque;
pub enum PassManager_opaque {}
pub type PassManagerRef = *PassManager_opaque;
pub type PassManagerRef = *'static PassManager_opaque;
pub enum PassManagerBuilder_opaque {}
pub type PassManagerBuilderRef = *PassManagerBuilder_opaque;
pub type PassManagerBuilderRef = *'static PassManagerBuilder_opaque;
pub enum Use_opaque {}
pub type UseRef = *Use_opaque;
pub type UseRef = *'static Use_opaque;
pub enum TargetData_opaque {}
pub type TargetDataRef = *TargetData_opaque;
pub type TargetDataRef = *'static TargetData_opaque;
pub enum ObjectFile_opaque {}
pub type ObjectFileRef = *ObjectFile_opaque;
pub type ObjectFileRef = *'static ObjectFile_opaque;
pub enum SectionIterator_opaque {}
pub type SectionIteratorRef = *SectionIterator_opaque;
pub type SectionIteratorRef = *'static SectionIterator_opaque;
pub enum Pass_opaque {}
pub type PassRef = *Pass_opaque;
pub type PassRef = *'static Pass_opaque;

pub mod debuginfo {
use super::{ValueRef};

pub enum DIBuilder_opaque {}
pub type DIBuilderRef = *DIBuilder_opaque;
pub type DIBuilderRef = *'static DIBuilder_opaque;

pub type DIDescriptor = ValueRef;
pub type DIScope = DIDescriptor;
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
}
'@' => return ty::mk_box(st.tcx, parse_mt(st, conv)),
'~' => return ty::mk_uniq(st.tcx, parse_mt(st, conv)),
'*' => return ty::mk_ptr(st.tcx, parse_mt(st, conv)),
'*' => {
let r = parse_region(st);
let mt = parse_mt(st, conv);
return ty::mk_ptr(st.tcx, r, mt);
}
'&' => {
let r = parse_region(st);
let mt = parse_mt(st, conv);
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ fn enc_sty(w: @io::Writer, cx: @ctxt, st: &ty::sty) {
}
ty::ty_box(mt) => { w.write_char('@'); enc_mt(w, cx, mt); }
ty::ty_uniq(mt) => { w.write_char('~'); enc_mt(w, cx, mt); }
ty::ty_ptr(mt) => { w.write_char('*'); enc_mt(w, cx, mt); }
ty::ty_ptr(r, mt) => {
w.write_char('*');
enc_region(w, cx, r);
enc_mt(w, cx, mt);
}
ty::ty_rptr(r, mt) => {
w.write_char('&');
enc_region(w, cx, r);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/borrowck/gather_loans/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl GuaranteeLifetimeContext {
mc::cat_arg(*) | // L-Local
mc::cat_self(*) | // L-Local
mc::cat_deref(_, _, mc::region_ptr(*)) | // L-Deref-Borrowed
mc::cat_deref(_, _, mc::unsafe_ptr) => {
mc::cat_deref(_, _, mc::unsafe_ptr(*)) => {
let scope = self.scope(cmt);
self.check_scope(scope)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/borrowck/gather_loans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl GatherLoanCtxt {
m_imm,
r)
}
ty::AutoUnsafe(_) => {}
ty::AutoUnsafe(*) => {}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/borrowck/gather_loans/restrictions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl RestrictionsContext {
}
}

mc::cat_deref(_, _, mc::unsafe_ptr) => {
mc::cat_deref(_, _, mc::unsafe_ptr(*)) => {
// We are very trusting when working with unsafe pointers.
Safe
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn check_crate(tcx: ty::ctxt,
debug!("effect: unary case, base type is %s",
ppaux::ty_to_str(tcx, base_type));
match ty::get(base_type).sty {
ty_ptr(_) => {
ty_ptr(*) => {
require_unsafe(expr.span,
"dereference of unsafe pointer")
}
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,9 @@ fn check_item_ctypes(cx: &Context, it: &ast::item) {
_ => ()
}
}
ast::ty_ptr(ref mt) => { check_ty(cx, mt.ty) }
ast::ty_ptr(_, ref mt) => {
check_ty(cx, mt.ty)
}
_ => ()
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub enum ptr_kind {
uniq_ptr(ast::mutability),
gc_ptr(ast::mutability),
region_ptr(ast::mutability, ty::Region),
unsafe_ptr
unsafe_ptr(ty::Region),
}

// We use the term "interior" to mean "something reachable from the
Expand Down Expand Up @@ -188,8 +188,8 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
Some(deref_ptr(gc_ptr(ast::m_imm)))
}

ty::ty_ptr(*) => {
Some(deref_ptr(unsafe_ptr))
ty::ty_ptr(r, _) => {
Some(deref_ptr(unsafe_ptr(r)))
}

ty::ty_enum(*) |
Expand Down Expand Up @@ -678,7 +678,7 @@ impl mem_categorization_ctxt {
uniq_ptr(*) => {
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
}
gc_ptr(*) | region_ptr(_, _) | unsafe_ptr => {
gc_ptr(*) | region_ptr(*) | unsafe_ptr(*) => {
MutabilityCategory::from_mutbl(mt.mutbl)
}
};
Expand Down Expand Up @@ -759,7 +759,7 @@ impl mem_categorization_ctxt {
uniq_ptr(*) => {
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
}
gc_ptr(_) | region_ptr(_, _) | unsafe_ptr => {
gc_ptr(_) | region_ptr(*) | unsafe_ptr(*) => {
MutabilityCategory::from_mutbl(mt.mutbl)
}
};
Expand Down Expand Up @@ -1232,7 +1232,7 @@ pub fn ptr_sigil(ptr: ptr_kind) -> ~str {
uniq_ptr(_) => ~"~",
gc_ptr(_) => ~"@",
region_ptr(_, _) => ~"&",
unsafe_ptr => ~"*"
unsafe_ptr(*) => ~"*"
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,16 @@ fn determine_rp_in_ty(ty: &ast::Ty,
// locations)
let sess = cx.sess;
match ty.node {
ast::ty_ptr(ref r, _) => {
debug!("referenced ptr type %s",
pprust::ty_to_str(ty, sess.intr()));

if cx.region_is_relevant(r) {
let rv = cx.add_variance(rv_contravariant);
cx.add_rp(cx.item_id, rv)
}
}

ast::ty_rptr(ref r, _) => {
debug!("referenced rptr type %s",
pprust::ty_to_str(ty, sess.intr()));
Expand Down Expand Up @@ -817,7 +827,7 @@ fn determine_rp_in_ty(ty: &ast::Ty,

match ty.node {
ast::ty_box(ref mt) | ast::ty_uniq(ref mt) | ast::ty_vec(ref mt) |
ast::ty_rptr(_, ref mt) | ast::ty_ptr(ref mt) => {
ast::ty_rptr(_, ref mt) | ast::ty_ptr(_, ref mt) => {
visit_mt(mt, (cx, visitor));
}

Expand Down
2 changes: 0 additions & 2 deletions src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,8 +1279,6 @@ pub fn compile_submatch(bcx: block,
assert!((m.len() > 0u || chk.is_some()));
let _icx = push_ctxt("match::compile_submatch");
let mut bcx = bcx;
let tcx = bcx.tcx();
let dm = tcx.def_map;
if m.len() == 0u {
Br(bcx, chk.get()());
return;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ pub fn compare_scalar_types(cx: block,

match ty::get(t).sty {
ty::ty_nil => rslt(cx, f(nil_type)),
ty::ty_bool | ty::ty_ptr(_) => rslt(cx, f(unsigned_int)),
ty::ty_bool | ty::ty_ptr(*) => rslt(cx, f(unsigned_int)),
ty::ty_int(_) => rslt(cx, f(signed_int)),
ty::ty_uint(_) => rslt(cx, f(unsigned_int)),
ty::ty_float(_) => rslt(cx, f(floating_point)),
Expand Down
8 changes: 6 additions & 2 deletions src/librustc/middle/trans/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ pub fn mk_closure_tys(tcx: ty::ctxt,
let bound_tys = bound_values.map(|bv| {
match bv.action {
EnvCopy | EnvMove => bv.datum.ty,
EnvRef => ty::mk_mut_ptr(tcx, bv.datum.ty)
EnvRef => ty::mk_mut_ptr(tcx,
ty::re_static,
bv.datum.ty)
}
});
let cdata_ty = ty::mk_tup(tcx, bound_tys);
Expand Down Expand Up @@ -210,7 +212,9 @@ pub fn store_environment(bcx: block,
// tuple. This could be a ptr in uniq or a box or on stack,
// whatever.
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
let cboxptr_ty = ty::mk_ptr(tcx, ty::mt {ty:cbox_ty, mutbl:ast::m_imm});
let cboxptr_ty = ty::mk_ptr(tcx,
ty::re_static,
ty::mt {ty:cbox_ty, mutbl:ast::m_imm});
let llboxptr_ty = type_of(ccx, cboxptr_ty);

// If there are no bound values, no point in allocating anything.
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ impl block_ {
pub fn tuplify_box_ty(tcx: ty::ctxt, t: ty::t) -> ty::t {
let ptr = ty::mk_ptr(
tcx,
ty::re_static,
ty::mt {ty: ty::mk_i8(), mutbl: ast::m_imm}
);
return ty::mk_tup(tcx, ~[ty::mk_uint(), ty::mk_type(tcx),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub fn const_expr(cx: @mut CrateContext, e: @ast::expr) -> ValueRef {
None => const_addr_of(cx, llconst)
};
match *autoref {
ty::AutoUnsafe(m) |
ty::AutoUnsafe(_, m) |
ty::AutoPtr(ty::re_static, m) => {
assert!(m != ast::m_mutbl);
llconst = llptr;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ impl Datum {
ty::ty_box(_) | ty::ty_uniq(_) => {
return (Some(self.box_body(bcx)), bcx);
}
ty::ty_ptr(mt) => {
ty::ty_ptr(_, mt) => {
if is_auto { // unsafe ptrs are not AUTO-derefable
return (None, bcx);
} else {
Expand Down
Loading