Skip to content

librustc: Replace the &static bound with 'static #5483

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 3 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
1 change: 1 addition & 0 deletions src/libcore/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct Guard<T, U> {
cond: &'self Condition/&self<T, U>
}

#[unsafe_destructor]
impl<T, U> Drop for Guard/&self<T, U> {
fn finalize(&self) {
unsafe {
Expand Down
14 changes: 8 additions & 6 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,15 +1230,17 @@ pub mod fsync {
arg: Arg<t>,
}

#[unsafe_destructor]
impl<T:Copy> Drop for Res<T> {
fn finalize(&self) {
match self.arg.opt_level {
None => (),
Some(level) => {
// fail hard if not succesful
fail_unless!(((self.arg.fsync_fn)(self.arg.val, level) != -1));
match self.arg.opt_level {
None => (),
Some(level) => {
// fail hard if not succesful
fail_unless!(((self.arg.fsync_fn)(self.arg.val, level)
!= -1));
}
}
}
}
}

Expand Down
36 changes: 28 additions & 8 deletions src/libcore/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,43 @@ pub pure fn mut_ptr_eq<T>(a: @mut T, b: @mut T) -> bool {
}

#[cfg(notest)]
impl<T:Eq> Eq for @const T {
impl<T:Eq> Eq for @T {
#[inline(always)]
pure fn eq(&self, other: &@const T) -> bool { *(*self) == *(*other) }
pure fn eq(&self, other: &@T) -> bool { *(*self) == *(*other) }
#[inline(always)]
pure fn ne(&self, other: &@const T) -> bool { *(*self) != *(*other) }
pure fn ne(&self, other: &@T) -> bool { *(*self) != *(*other) }
}

#[cfg(notest)]
impl<T:Ord> Ord for @const T {
impl<T:Eq> Eq for @mut T {
#[inline(always)]
pure fn lt(&self, other: &@const T) -> bool { *(*self) < *(*other) }
pure fn eq(&self, other: &@mut T) -> bool { *(*self) == *(*other) }
#[inline(always)]
pure fn le(&self, other: &@const T) -> bool { *(*self) <= *(*other) }
pure fn ne(&self, other: &@mut T) -> bool { *(*self) != *(*other) }
}

#[cfg(notest)]
impl<T:Ord> Ord for @T {
#[inline(always)]
pure fn lt(&self, other: &@T) -> bool { *(*self) < *(*other) }
#[inline(always)]
pure fn le(&self, other: &@T) -> bool { *(*self) <= *(*other) }
#[inline(always)]
pure fn ge(&self, other: &@T) -> bool { *(*self) >= *(*other) }
#[inline(always)]
pure fn gt(&self, other: &@T) -> bool { *(*self) > *(*other) }
}

#[cfg(notest)]
impl<T:Ord> Ord for @mut T {
#[inline(always)]
pure fn lt(&self, other: &@mut T) -> bool { *(*self) < *(*other) }
#[inline(always)]
pure fn le(&self, other: &@mut T) -> bool { *(*self) <= *(*other) }
#[inline(always)]
pure fn ge(&self, other: &@const T) -> bool { *(*self) >= *(*other) }
pure fn ge(&self, other: &@mut T) -> bool { *(*self) >= *(*other) }
#[inline(always)]
pure fn gt(&self, other: &@const T) -> bool { *(*self) > *(*other) }
pure fn gt(&self, other: &@mut T) -> bool { *(*self) > *(*other) }
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ fn test_unwrap_resource() {
i: @mut int,
}

#[unsafe_destructor]
impl ::ops::Drop for R {
fn finalize(&self) { *(self.i) += 1; }
}
Expand Down
16 changes: 10 additions & 6 deletions src/libcore/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ struct BufferResource<T> {

}

#[unsafe_destructor]
impl<T> ::ops::Drop for BufferResource<T> {
fn finalize(&self) {
unsafe {
Expand Down Expand Up @@ -445,16 +446,17 @@ pub fn try_recv<T:Owned,Tbuffer:Owned>(p: RecvPacketBuffered<T, Tbuffer>)
let p_ = p.unwrap();
let p = unsafe { &*p_ };

#[unsafe_destructor]
struct DropState {
p: &'self PacketHeader,

drop {
if task::failing() {
self.p.state = Terminated;
let old_task = swap_task(&mut self.p.blocked_task,
ptr::null());
if !old_task.is_null() {
unsafe {
unsafe {
if task::failing() {
self.p.state = Terminated;
let old_task = swap_task(&mut self.p.blocked_task,
ptr::null());
if !old_task.is_null() {
rustrt::rust_task_deref(old_task);
}
}
Expand Down Expand Up @@ -773,6 +775,7 @@ pub struct SendPacketBuffered<T, Tbuffer> {
mut buffer: Option<BufferResource<Tbuffer>>,
}

#[unsafe_destructor]
impl<T:Owned,Tbuffer:Owned> ::ops::Drop for SendPacketBuffered<T,Tbuffer> {
fn finalize(&self) {
//if self.p != none {
Expand Down Expand Up @@ -842,6 +845,7 @@ pub struct RecvPacketBuffered<T, Tbuffer> {
mut buffer: Option<BufferResource<Tbuffer>>,
}

#[unsafe_destructor]
impl<T:Owned,Tbuffer:Owned> ::ops::Drop for RecvPacketBuffered<T,Tbuffer> {
fn finalize(&self) {
//if self.p != none {
Expand Down
1 change: 1 addition & 0 deletions src/libcore/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct ArcDestruct<T> {
mut data: *libc::c_void,
}

#[unsafe_destructor]
impl<T> Drop for ArcDestruct<T>{
fn finalize(&self) {
unsafe {
Expand Down
1 change: 1 addition & 0 deletions src/libcore/unstable/finally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct Finallyalizer {
dtor: &'self fn()
}

#[unsafe_destructor]
impl Drop for Finallyalizer/&self {
fn finalize(&self) {
(self.dtor)();
Expand Down
103 changes: 96 additions & 7 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use core::str;
use core::vec;
use std::oldmap::HashMap;
use syntax::ast::*;
use syntax::attr::attrs_contains_name;
use syntax::codemap::{span, spanned};
use syntax::print::pprust::expr_to_str;
use syntax::{visit, ast_util};
Expand Down Expand Up @@ -55,6 +56,8 @@ use syntax::{visit, ast_util};
// primitives in the stdlib are explicitly annotated to only take sendable
// types.

use core::hashmap::linear::LinearSet;

pub const try_adding: &'static str = "Try adding a move";

pub type rval_map = HashMap<node_id, ()>;
Expand All @@ -63,7 +66,7 @@ pub struct Context {
tcx: ty::ctxt,
method_map: typeck::method_map,
last_use_map: liveness::last_use_map,
current_item: node_id
current_item: node_id,
}

pub fn check_crate(tcx: ty::ctxt,
Expand All @@ -74,16 +77,15 @@ pub fn check_crate(tcx: ty::ctxt,
tcx: tcx,
method_map: method_map,
last_use_map: last_use_map,
current_item: -1
current_item: -1,
};
let visit = visit::mk_vt(@visit::Visitor {
visit_arm: check_arm,
visit_expr: check_expr,
visit_fn: check_fn,
visit_ty: check_ty,
visit_item: |i, cx, v| {
visit::visit_item(i, Context { current_item: i.id,.. cx }, v);
},
visit_item: check_item,
visit_block: check_block,
.. *visit::default_visitor()
});
visit::visit_crate(*crate, ctx, visit);
Expand All @@ -92,6 +94,93 @@ pub fn check_crate(tcx: ty::ctxt,

type check_fn = @fn(Context, @freevar_entry);

fn check_struct_safe_for_destructor(cx: Context,
span: span,
struct_did: def_id) {
let struct_tpt = ty::lookup_item_type(cx.tcx, struct_did);
if struct_tpt.bounds.len() == 0 {
let struct_ty = ty::mk_struct(cx.tcx, struct_did, ty::substs {
self_r: None,
self_ty: None,
tps: ~[]
});
if !ty::type_is_owned(cx.tcx, struct_ty) {
cx.tcx.sess.span_err(span,
~"cannot implement a destructor on a struct \
that is not Owned");
cx.tcx.sess.span_note(span,
~"use \"#[unsafe_destructor]\" on the \
implementation to force the compiler to \
allow this");
}
} else {
cx.tcx.sess.span_err(span,
~"cannot implement a destructor on a struct \
with type parameters");
cx.tcx.sess.span_note(span,
~"use \"#[unsafe_destructor]\" on the \
implementation to force the compiler to \
allow this");
}
}

fn check_block(block: &blk, cx: Context, visitor: visit::vt<Context>) {
visit::visit_block(block, cx, visitor);
}

fn check_item(item: @item, cx: Context, visitor: visit::vt<Context>) {
// If this is a destructor, check kinds.
if !attrs_contains_name(item.attrs, "unsafe_destructor") {
match item.node {
item_impl(_, Some(trait_ref), self_type, _) => {
match cx.tcx.def_map.find(&trait_ref.ref_id) {
None => cx.tcx.sess.bug(~"trait ref not in def map!"),
Some(trait_def) => {
let trait_def_id = ast_util::def_id_of_def(trait_def);
if cx.tcx.lang_items.drop_trait() == trait_def_id {
// Yes, it's a destructor.
match self_type.node {
ty_path(_, path_node_id) => {
let struct_def = cx.tcx.def_map.get(
&path_node_id);
let struct_did =
ast_util::def_id_of_def(struct_def);
check_struct_safe_for_destructor(
cx,
self_type.span,
struct_did);
}
_ => {
cx.tcx.sess.span_bug(self_type.span,
~"the self type for \
the Drop trait \
impl is not a \
path");
}
}
}
}
}
}
item_struct(struct_def, _) => {
match struct_def.dtor {
None => {}
Some(ref dtor) => {
let struct_did = def_id { crate: 0, node: item.id };
check_struct_safe_for_destructor(cx,
dtor.span,
struct_did);
}
}
}
_ => {}
}
}

let cx = Context { current_item: item.id, ..cx };
visit::visit_item(item, cx, visitor);
}

// Yields the appropriate function to check the kind of closed over
// variables. `id` is the node_id for some expression that creates the
// closure.
Expand Down Expand Up @@ -287,7 +376,7 @@ pub fn check_bounds(cx: Context,

ty::bound_durable => {
if !kind.is_durable(cx.tcx) {
missing.push("&static");
missing.push("'static");
}
}

Expand Down Expand Up @@ -378,7 +467,7 @@ pub fn check_durable(tcx: ty::ctxt, ty: ty::t, sp: span) -> bool {
match ty::get(ty).sty {
ty::ty_param(*) => {
tcx.sess.span_err(sp, ~"value may contain borrowed \
pointers; use `&static` bound");
pointers; use `'static` bound");
}
_ => {
tcx.sess.span_err(sp, ~"value may contain borrowed \
Expand Down
9 changes: 6 additions & 3 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ pub struct icx_popper {
ccx: @CrateContext,
}

#[unsafe_destructor]
impl Drop for icx_popper {
fn finalize(&self) {
if self.ccx.sess.count_llvm_insns() {
self.ccx.stats.llvm_insn_ctxt.pop();
}
unsafe {
if self.ccx.sess.count_llvm_insns() {
self.ccx.stats.llvm_insn_ctxt.pop();
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ pub fn substs_to_str(cx: ctxt, substs: &substs) -> ~str {
pub fn param_bound_to_str(cx: ctxt, pb: &param_bound) -> ~str {
match *pb {
bound_copy => ~"copy",
bound_durable => ~"&static",
bound_durable => ~"'static",
bound_owned => ~"owned",
bound_const => ~"const",
bound_trait(t) => ::util::ppaux::ty_to_str(cx, t)
Expand Down
1 change: 1 addition & 0 deletions src/libstd/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub struct Arena {
priv mut chunks: @List<Chunk>,
}

#[unsafe_destructor]
impl Drop for Arena {
fn finalize(&self) {
unsafe {
Expand Down
9 changes: 6 additions & 3 deletions src/libstd/c_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ struct DtorRes {
dtor: Option<@fn()>,
}

#[unsafe_destructor]
impl Drop for DtorRes {
fn finalize(&self) {
match self.dtor {
option::None => (),
option::Some(f) => f()
unsafe {
match self.dtor {
option::None => (),
option::Some(f) => f()
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libstd/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Future<A> {

// FIXME(#2829) -- futures should not be copyable, because they close
// over ~fn's that have pipes and so forth within!
#[unsafe_destructor]
impl<A> Drop for Future<A> {
fn finalize(&self) {}
}
Expand Down
1 change: 1 addition & 0 deletions src/libstd/net_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub struct TcpSocket {
socket_data: @TcpSocketData,
}

#[unsafe_destructor]
impl Drop for TcpSocket {
fn finalize(&self) {
unsafe {
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,7 @@ mod big_tests {
key: &'self fn(@uint),
}

#[unsafe_destructor]
impl Drop for LVal/&self {
fn finalize(&self) {
let x = unsafe { task::local_data::local_data_get(self.key) };
Expand Down
Loading