Skip to content

Commit d1ebdbe

Browse files
committed
librustc: Implement explicit self for Add and Index; add a hack in the borrow checker to support this. r=nmatsakis
1 parent 56ece46 commit d1ebdbe

File tree

10 files changed

+128
-15
lines changed

10 files changed

+128
-15
lines changed

src/libcore/at_vec.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,22 @@ pub pure fn from_elem<T: Copy>(n_elts: uint, t: T) -> @[T] {
145145

146146
#[cfg(notest)]
147147
pub mod traits {
148+
#[cfg(stage0)]
148149
pub impl<T: Copy> @[T] : Add<&[const T],@[T]> {
149150
#[inline(always)]
150151
pure fn add(rhs: & &self/[const T]) -> @[T] {
151152
append(self, (*rhs))
152153
}
153154
}
155+
156+
#[cfg(stage1)]
157+
#[cfg(stage2)]
158+
pub impl<T: Copy> @[T] : Add<&[const T],@[T]> {
159+
#[inline(always)]
160+
pure fn add(&self, rhs: & &self/[const T]) -> @[T] {
161+
append(*self, (*rhs))
162+
}
163+
}
154164
}
155165

156166
#[cfg(test)]

src/libcore/dvec.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,19 @@ impl<A: Copy> DVec<A> {
358358
}
359359
}
360360

361+
#[cfg(stage0)]
361362
impl<A:Copy> DVec<A>: Index<uint,A> {
362363
#[inline(always)]
363364
pure fn index(idx: uint) -> A {
364365
self.get_elt(idx)
365366
}
366367
}
368+
#[cfg(stage1)]
369+
#[cfg(stage2)]
370+
impl<A:Copy> DVec<A>: Index<uint,A> {
371+
#[inline(always)]
372+
pure fn index(&self, idx: uint) -> A {
373+
self.get_elt(idx)
374+
}
375+
}
367376

src/libcore/ops.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ pub trait Drop {
1818
fn finalize(&self); // XXX: Rename to "drop"? --pcwalton
1919
}
2020

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

27+
#[cfg(stage1)]
28+
#[cfg(stage2)]
29+
#[lang="add"]
30+
pub trait Add<RHS,Result> {
31+
pure fn add(&self, rhs: &RHS) -> Result;
32+
}
33+
2634
#[lang="sub"]
2735
pub trait Sub<RHS,Result> {
2836
pure fn sub(&self, rhs: &RHS) -> Result;
@@ -73,8 +81,16 @@ pub trait Shr<RHS,Result> {
7381
pure fn shr(&self, rhs: &RHS) -> Result;
7482
}
7583

84+
#[cfg(stage0)]
7685
#[lang="index"]
7786
pub trait Index<Index,Result> {
7887
pure fn index(index: Index) -> Result;
7988
}
8089

90+
#[cfg(stage1)]
91+
#[cfg(stage2)]
92+
#[lang="index"]
93+
pub trait Index<Index,Result> {
94+
pure fn index(&self, index: Index) -> Result;
95+
}
96+

src/libcore/str.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,12 +2107,21 @@ impl ~str: Trimmable {
21072107

21082108
#[cfg(notest)]
21092109
pub mod traits {
2110+
#[cfg(stage0)]
21102111
impl ~str : Add<&str,~str> {
21112112
#[inline(always)]
21122113
pure fn add(rhs: & &self/str) -> ~str {
21132114
append(copy self, (*rhs))
21142115
}
21152116
}
2117+
#[cfg(stage1)]
2118+
#[cfg(stage2)]
2119+
impl ~str : Add<&str,~str> {
2120+
#[inline(always)]
2121+
pure fn add(&self, rhs: & &self/str) -> ~str {
2122+
append(copy *self, (*rhs))
2123+
}
2124+
}
21162125
}
21172126

21182127
#[cfg(test)]

src/libcore/vec.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,18 +1438,37 @@ impl<T: Ord> @[T] : Ord {
14381438
}
14391439

14401440
#[cfg(notest)]
1441-
impl<T: Copy> ~[T] : Add<&[const T],~[T]> {
1442-
#[inline(always)]
1443-
pure fn add(rhs: & &self/[const T]) -> ~[T] {
1444-
append(copy self, (*rhs))
1441+
pub mod traits {
1442+
#[cfg(stage0)]
1443+
impl<T: Copy> ~[T] : Add<&[const T],~[T]> {
1444+
#[inline(always)]
1445+
pure fn add(rhs: & &self/[const T]) -> ~[T] {
1446+
append(copy self, (*rhs))
1447+
}
1448+
}
1449+
#[cfg(stage1)]
1450+
#[cfg(stage2)]
1451+
impl<T: Copy> ~[T] : Add<&[const T],~[T]> {
1452+
#[inline(always)]
1453+
pure fn add(&self, rhs: & &self/[const T]) -> ~[T] {
1454+
append(copy *self, (*rhs))
1455+
}
14451456
}
1446-
}
14471457

1448-
#[cfg(notest)]
1449-
impl<T: Copy> ~[mut T] : Add<&[const T],~[mut T]> {
1450-
#[inline(always)]
1451-
pure fn add(rhs: & &self/[const T]) -> ~[mut T] {
1452-
append_mut(copy self, (*rhs))
1458+
#[cfg(stage0)]
1459+
impl<T: Copy> ~[mut T] : Add<&[const T],~[mut T]> {
1460+
#[inline(always)]
1461+
pure fn add(rhs: & &self/[const T]) -> ~[mut T] {
1462+
append_mut(copy self, (*rhs))
1463+
}
1464+
}
1465+
#[cfg(stage1)]
1466+
#[cfg(stage2)]
1467+
impl<T: Copy> ~[mut T] : Add<&[const T],~[mut T]> {
1468+
#[inline(always)]
1469+
pure fn add(&self, rhs: & &self/[const T]) -> ~[mut T] {
1470+
append_mut(copy *self, (*rhs))
1471+
}
14531472
}
14541473
}
14551474

src/librustc/middle/borrowck/gather_loans.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use mem_categorization::{mem_categorization_ctxt, opt_deref_kind};
2020
use preserve::{preserve_condition, pc_ok, pc_if_pure};
2121
use ty::{ty_region};
2222

23+
use core::send_map::linear::LinearMap;
24+
2325
export gather_loans;
2426

2527
/// Context used while gathering loans:
@@ -53,14 +55,16 @@ export gather_loans;
5355
enum gather_loan_ctxt = @{bccx: borrowck_ctxt,
5456
req_maps: req_maps,
5557
mut item_ub: ast::node_id,
56-
mut root_ub: ast::node_id};
58+
mut root_ub: ast::node_id,
59+
mut ignore_adjustments: LinearMap<ast::node_id,()>};
5760

5861
fn gather_loans(bccx: borrowck_ctxt, crate: @ast::crate) -> req_maps {
5962
let glcx = gather_loan_ctxt(@{bccx: bccx,
6063
req_maps: {req_loan_map: HashMap(),
6164
pure_map: HashMap()},
6265
mut item_ub: 0,
63-
mut root_ub: 0});
66+
mut root_ub: 0,
67+
mut ignore_adjustments: LinearMap()});
6468
let v = visit::mk_vt(@{visit_expr: req_loans_in_expr,
6569
visit_fn: req_loans_in_fn,
6670
.. *visit::default_visitor()});
@@ -104,8 +108,10 @@ fn req_loans_in_expr(ex: @ast::expr,
104108
ex.id, pprust::expr_to_str(ex, tcx.sess.intr()));
105109

106110
// If this expression is borrowed, have to ensure it remains valid:
107-
for tcx.adjustments.find(ex.id).each |adjustments| {
108-
self.guarantee_adjustments(ex, *adjustments);
111+
if !self.ignore_adjustments.contains_key(&ex.id) {
112+
for tcx.adjustments.find(ex.id).each |adjustments| {
113+
self.guarantee_adjustments(ex, *adjustments);
114+
}
109115
}
110116

111117
// Special checks for various kinds of expressions:
@@ -179,7 +185,8 @@ fn req_loans_in_expr(ex: @ast::expr,
179185

180186
ast::expr_index(rcvr, _) |
181187
ast::expr_binary(_, rcvr, _) |
182-
ast::expr_unary(_, rcvr)
188+
ast::expr_unary(_, rcvr) |
189+
ast::expr_assign_op(_, rcvr, _)
183190
if self.bccx.method_map.contains_key(ex.id) => {
184191
// Receivers in method calls are always passed by ref.
185192
//
@@ -193,6 +200,11 @@ fn req_loans_in_expr(ex: @ast::expr,
193200
let scope_r = ty::re_scope(ex.id);
194201
let rcvr_cmt = self.bccx.cat_expr(rcvr);
195202
self.guarantee_valid(rcvr_cmt, m_imm, scope_r);
203+
204+
// FIXME (#3387): Total hack: Ignore adjustments for the left-hand
205+
// side. Their regions will be inferred to be too large.
206+
self.ignore_adjustments.insert(rcvr.id, ());
207+
196208
visit::visit_expr(ex, self, vt);
197209
}
198210

src/libstd/bitv.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,19 @@ pure fn land(w0: uint, w1: uint) -> uint { return w0 & w1; }
565565

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

568+
#[cfg(stage0)]
568569
impl Bitv: ops::Index<uint,bool> {
569570
pure fn index(i: uint) -> bool {
570571
self.get(i)
571572
}
572573
}
574+
#[cfg(stage1)]
575+
#[cfg(stage2)]
576+
impl Bitv: ops::Index<uint,bool> {
577+
pure fn index(&self, i: uint) -> bool {
578+
self.get(i)
579+
}
580+
}
573581

574582
#[cfg(test)]
575583
mod tests {

src/libstd/ebml.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,23 @@ pub mod Reader {
5757

5858
// ebml reading
5959

60+
#[cfg(stage0)]
6061
impl Doc: ops::Index<uint,Doc> {
6162
pure fn index(tag: uint) -> Doc {
6263
unsafe {
6364
get_doc(self, tag)
6465
}
6566
}
6667
}
68+
#[cfg(stage1)]
69+
#[cfg(stage2)]
70+
impl Doc: ops::Index<uint,Doc> {
71+
pure fn index(&self, tag: uint) -> Doc {
72+
unsafe {
73+
get_doc(*self, tag)
74+
}
75+
}
76+
}
6777

6878
fn vuint_at(data: &[u8], start: uint) -> {val: uint, next: uint} {
6979
let a = data[start];

src/libstd/map.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,23 @@ pub mod chained {
429429
}
430430
}
431431

432+
#[cfg(stage0)]
432433
impl<K:Eq IterBytes Hash Copy, V: Copy> T<K, V>: ops::Index<K, V> {
433434
pure fn index(k: K) -> V {
434435
unsafe {
435436
self.get(k)
436437
}
437438
}
438439
}
440+
#[cfg(stage1)]
441+
#[cfg(stage2)]
442+
impl<K:Eq IterBytes Hash Copy, V: Copy> T<K, V>: ops::Index<K, V> {
443+
pure fn index(&self, k: K) -> V {
444+
unsafe {
445+
self.get(k)
446+
}
447+
}
448+
}
439449

440450
fn chains<K,V>(nchains: uint) -> ~[mut Option<@Entry<K,V>>] {
441451
vec::to_mut(vec::from_elem(nchains, None))

src/libstd/smallintmap.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,23 @@ impl<V: Copy> SmallIntMap<V>: map::Map<uint, V> {
150150
}
151151
}
152152

153+
#[cfg(stage0)]
153154
impl<V: Copy> SmallIntMap<V>: ops::Index<uint, V> {
154155
pure fn index(key: uint) -> V {
155156
unsafe {
156157
get(self, key)
157158
}
158159
}
159160
}
161+
#[cfg(stage1)]
162+
#[cfg(stage2)]
163+
impl<V: Copy> SmallIntMap<V>: ops::Index<uint, V> {
164+
pure fn index(&self, key: uint) -> V {
165+
unsafe {
166+
get(*self, key)
167+
}
168+
}
169+
}
160170

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

0 commit comments

Comments
 (0)