Skip to content

librustc: Implement explicit self for Add and Index; add a hack in the b... #4057

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
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
10 changes: 10 additions & 0 deletions src/libcore/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,22 @@ pub pure fn from_elem<T: Copy>(n_elts: uint, t: T) -> @[T] {

#[cfg(notest)]
pub mod traits {
#[cfg(stage0)]
pub impl<T: Copy> @[T] : Add<&[const T],@[T]> {
#[inline(always)]
pure fn add(rhs: & &self/[const T]) -> @[T] {
append(self, (*rhs))
}
}

#[cfg(stage1)]
#[cfg(stage2)]
pub impl<T: Copy> @[T] : Add<&[const T],@[T]> {
#[inline(always)]
pure fn add(&self, rhs: & &self/[const T]) -> @[T] {
append(*self, (*rhs))
}
}
}

#[cfg(test)]
Expand Down
9 changes: 9 additions & 0 deletions src/libcore/dvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,19 @@ impl<A: Copy> DVec<A> {
}
}

#[cfg(stage0)]
impl<A:Copy> DVec<A>: Index<uint,A> {
#[inline(always)]
pure fn index(idx: uint) -> A {
self.get_elt(idx)
}
}
#[cfg(stage1)]
#[cfg(stage2)]
impl<A:Copy> DVec<A>: Index<uint,A> {
#[inline(always)]
pure fn index(&self, idx: uint) -> A {
self.get_elt(idx)
}
}

16 changes: 16 additions & 0 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ pub trait Drop {
fn finalize(); // XXX: Rename to "drop"? --pcwalton
}

#[cfg(stage0)]
#[lang="add"]
pub trait Add<RHS,Result> {
pure fn add(rhs: &RHS) -> Result;
}

#[cfg(stage1)]
#[cfg(stage2)]
#[lang="add"]
pub trait Add<RHS,Result> {
pure fn add(&self, rhs: &RHS) -> Result;
}

#[lang="sub"]
pub trait Sub<RHS,Result> {
pure fn sub(&self, rhs: &RHS) -> Result;
Expand Down Expand Up @@ -83,8 +91,16 @@ pub trait Shr<RHS,Result> {
pure fn shr(&self, rhs: &RHS) -> Result;
}

#[cfg(stage0)]
#[lang="index"]
pub trait Index<Index,Result> {
pure fn index(index: Index) -> Result;
}

#[cfg(stage1)]
#[cfg(stage2)]
#[lang="index"]
pub trait Index<Index,Result> {
pure fn index(&self, index: Index) -> Result;
}

9 changes: 9 additions & 0 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2174,12 +2174,21 @@ impl ~str: Trimmable {

#[cfg(notest)]
pub mod traits {
#[cfg(stage0)]
impl ~str : Add<&str,~str> {
#[inline(always)]
pure fn add(rhs: & &self/str) -> ~str {
append(copy self, (*rhs))
}
}
#[cfg(stage1)]
#[cfg(stage2)]
impl ~str : Add<&str,~str> {
#[inline(always)]
pure fn add(&self, rhs: & &self/str) -> ~str {
append(copy *self, (*rhs))
}
}
}

#[cfg(test)]
Expand Down
18 changes: 18 additions & 0 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,19 +1495,37 @@ impl<T: Ord> @[T] : Ord {

#[cfg(notest)]
pub mod traits {
#[cfg(stage0)]
impl<T: Copy> ~[T] : Add<&[const T],~[T]> {
#[inline(always)]
pure fn add(rhs: & &self/[const T]) -> ~[T] {
append(copy self, (*rhs))
}
}
#[cfg(stage1)]
#[cfg(stage2)]
impl<T: Copy> ~[T] : Add<&[const T],~[T]> {
#[inline(always)]
pure fn add(&self, rhs: & &self/[const T]) -> ~[T] {
append(copy *self, (*rhs))
}
}

#[cfg(stage0)]
impl<T: Copy> ~[mut T] : Add<&[const T],~[mut T]> {
#[inline(always)]
pure fn add(rhs: & &self/[const T]) -> ~[mut T] {
append_mut(copy self, (*rhs))
}
}
#[cfg(stage1)]
#[cfg(stage2)]
impl<T: Copy> ~[mut T] : Add<&[const T],~[mut T]> {
#[inline(always)]
pure fn add(&self, rhs: & &self/[const T]) -> ~[mut T] {
append_mut(copy *self, (*rhs))
}
}
}

#[cfg(test)]
Expand Down
22 changes: 17 additions & 5 deletions src/librustc/middle/borrowck/gather_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use mem_categorization::{mem_categorization_ctxt, opt_deref_kind};
use preserve::{preserve_condition, pc_ok, pc_if_pure};
use ty::{ty_region};

use core::send_map::linear::LinearMap;

export gather_loans;

/// Context used while gathering loans:
Expand Down Expand Up @@ -43,14 +45,16 @@ export gather_loans;
enum gather_loan_ctxt = @{bccx: borrowck_ctxt,
req_maps: req_maps,
mut item_ub: ast::node_id,
mut root_ub: ast::node_id};
mut root_ub: ast::node_id,
mut ignore_adjustments: LinearMap<ast::node_id,()>};

fn gather_loans(bccx: borrowck_ctxt, crate: @ast::crate) -> req_maps {
let glcx = gather_loan_ctxt(@{bccx: bccx,
req_maps: {req_loan_map: HashMap(),
pure_map: HashMap()},
mut item_ub: 0,
mut root_ub: 0});
mut root_ub: 0,
mut ignore_adjustments: LinearMap()});
let v = visit::mk_vt(@{visit_expr: req_loans_in_expr,
visit_fn: req_loans_in_fn,
.. *visit::default_visitor()});
Expand Down Expand Up @@ -94,8 +98,10 @@ fn req_loans_in_expr(ex: @ast::expr,
ex.id, pprust::expr_to_str(ex, tcx.sess.intr()));

// If this expression is borrowed, have to ensure it remains valid:
for tcx.adjustments.find(ex.id).each |adjustments| {
self.guarantee_adjustments(ex, *adjustments);
if !self.ignore_adjustments.contains_key(&ex.id) {
for tcx.adjustments.find(ex.id).each |adjustments| {
self.guarantee_adjustments(ex, *adjustments);
}
}

// Special checks for various kinds of expressions:
Expand Down Expand Up @@ -137,7 +143,8 @@ fn req_loans_in_expr(ex: @ast::expr,

ast::expr_index(rcvr, _) |
ast::expr_binary(_, rcvr, _) |
ast::expr_unary(_, rcvr)
ast::expr_unary(_, rcvr) |
ast::expr_assign_op(_, rcvr, _)
if self.bccx.method_map.contains_key(ex.id) => {
// Receivers in method calls are always passed by ref.
//
Expand All @@ -151,6 +158,11 @@ fn req_loans_in_expr(ex: @ast::expr,
let scope_r = ty::re_scope(ex.id);
let rcvr_cmt = self.bccx.cat_expr(rcvr);
self.guarantee_valid(rcvr_cmt, m_imm, scope_r);

// FIXME (#3387): Total hack: Ignore adjustments for the left-hand
// side. Their regions will be inferred to be too large.
self.ignore_adjustments.insert(rcvr.id, ());

visit::visit_expr(ex, self, vt);
}

Expand Down
8 changes: 8 additions & 0 deletions src/libstd/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,19 @@ pure fn land(w0: uint, w1: uint) -> uint { return w0 & w1; }

pure fn right(_w0: uint, w1: uint) -> uint { return w1; }

#[cfg(stage0)]
impl Bitv: ops::Index<uint,bool> {
pure fn index(i: uint) -> bool {
self.get(i)
}
}
#[cfg(stage1)]
#[cfg(stage2)]
impl Bitv: ops::Index<uint,bool> {
pure fn index(&self, i: uint) -> bool {
self.get(i)
}
}

#[cfg(test)]
mod tests {
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ pub mod Reader {

// ebml reading

#[cfg(stage0)]
impl Doc: ops::Index<uint,Doc> {
pure fn index(tag: uint) -> Doc {
unsafe {
get_doc(self, tag)
}
}
}
#[cfg(stage1)]
#[cfg(stage2)]
impl Doc: ops::Index<uint,Doc> {
pure fn index(&self, tag: uint) -> Doc {
unsafe {
get_doc(*self, tag)
}
}
}

fn vuint_at(data: &[u8], start: uint) -> {val: uint, next: uint} {
let a = data[start];
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,23 @@ pub mod chained {
}
}

#[cfg(stage0)]
impl<K:Eq IterBytes Hash Copy, V: Copy> T<K, V>: ops::Index<K, V> {
pure fn index(k: K) -> V {
unsafe {
self.get(k)
}
}
}
#[cfg(stage1)]
#[cfg(stage2)]
impl<K:Eq IterBytes Hash Copy, V: Copy> T<K, V>: ops::Index<K, V> {
pure fn index(&self, k: K) -> V {
unsafe {
self.get(k)
}
}
}

fn chains<K,V>(nchains: uint) -> ~[mut Option<@Entry<K,V>>] {
vec::to_mut(vec::from_elem(nchains, None))
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,23 @@ impl<V: Copy> SmallIntMap<V>: map::Map<uint, V> {
}
}

#[cfg(stage0)]
impl<V: Copy> SmallIntMap<V>: ops::Index<uint, V> {
pure fn index(key: uint) -> V {
unsafe {
get(self, key)
}
}
}
#[cfg(stage1)]
#[cfg(stage2)]
impl<V: Copy> SmallIntMap<V>: ops::Index<uint, V> {
pure fn index(&self, key: uint) -> V {
unsafe {
get(*self, key)
}
}
}

/// Cast the given smallintmap to a map::map
pub fn as_map<V: Copy>(s: SmallIntMap<V>) -> map::Map<uint, V> {
Expand Down