Skip to content

libcore: Add explicit self to all overloaded operators but Add and Index #4014

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 2 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
20 changes: 10 additions & 10 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,52 @@ pub trait Add<RHS,Result> {

#[lang="sub"]
pub trait Sub<RHS,Result> {
pure fn sub(rhs: &RHS) -> Result;
pure fn sub(&self, rhs: &RHS) -> Result;
}

#[lang="mul"]
pub trait Mul<RHS,Result> {
pure fn mul(rhs: &RHS) -> Result;
pure fn mul(&self, rhs: &RHS) -> Result;
}

#[lang="div"]
pub trait Div<RHS,Result> {
pure fn div(rhs: &RHS) -> Result;
pure fn div(&self, rhs: &RHS) -> Result;
}

#[lang="modulo"]
pub trait Modulo<RHS,Result> {
pure fn modulo(rhs: &RHS) -> Result;
pure fn modulo(&self, rhs: &RHS) -> Result;
}

#[lang="neg"]
pub trait Neg<Result> {
pure fn neg() -> Result;
pure fn neg(&self) -> Result;
}

#[lang="bitand"]
pub trait BitAnd<RHS,Result> {
pure fn bitand(rhs: &RHS) -> Result;
pure fn bitand(&self, rhs: &RHS) -> Result;
}

#[lang="bitor"]
pub trait BitOr<RHS,Result> {
pure fn bitor(rhs: &RHS) -> Result;
pure fn bitor(&self, rhs: &RHS) -> Result;
}

#[lang="bitxor"]
pub trait BitXor<RHS,Result> {
pure fn bitxor(rhs: &RHS) -> Result;
pure fn bitxor(&self, rhs: &RHS) -> Result;
}

#[lang="shl"]
pub trait Shl<RHS,Result> {
pure fn shl(rhs: &RHS) -> Result;
pure fn shl(&self, rhs: &RHS) -> Result;
}

#[lang="shr"]
pub trait Shr<RHS,Result> {
pure fn shr(rhs: &RHS) -> Result;
pure fn shr(&self, rhs: &RHS) -> Result;
}

#[lang="index"]
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ fn classify(e: @expr,
ast::expr_vstore_fixed(_) |
ast::expr_vstore_slice => classify(e, def_map, tcx),
ast::expr_vstore_uniq |
ast::expr_vstore_box => non_const
ast::expr_vstore_box |
ast::expr_vstore_mut_box => non_const
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ fn trans_rvalue_datum_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
trace_span!(bcx, expr.span, shorten(bcx.expr_to_str(expr)));

match expr.node {
ast::expr_vstore(contents, ast::expr_vstore_box) => {
ast::expr_vstore(contents, ast::expr_vstore_box) |
ast::expr_vstore(contents, ast::expr_vstore_mut_box) => {
return tvec::trans_uniq_or_managed_vstore(bcx, heap_shared,
expr, contents);
}
Expand Down
15 changes: 8 additions & 7 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2010,25 +2010,25 @@ fn remove_copyable(k: Kind) -> Kind {
}

impl Kind : ops::BitAnd<Kind,Kind> {
pure fn bitand(other: &Kind) -> Kind {
pure fn bitand(&self, other: &Kind) -> Kind {
unsafe {
lower_kind(self, (*other))
lower_kind(*self, *other)
}
}
}

impl Kind : ops::BitOr<Kind,Kind> {
pure fn bitor(other: &Kind) -> Kind {
pure fn bitor(&self, other: &Kind) -> Kind {
unsafe {
raise_kind(self, (*other))
raise_kind(*self, *other)
}
}
}

impl Kind : ops::Sub<Kind,Kind> {
pure fn sub(other: &Kind) -> Kind {
pure fn sub(&self, other: &Kind) -> Kind {
unsafe {
kind_(*self & !*(*other))
kind_(**self & !**other)
}
}
}
Expand Down Expand Up @@ -2278,7 +2278,7 @@ fn type_kind(cx: ctxt, ty: t) -> Kind {
// arbitrary threshold to prevent by-value copying of big records
if kind_is_safe_for_default_mode(result) {
if type_size(cx, ty) > 4 {
result -= kind_(KIND_MASK_DEFAULT_MODE);
result = result - kind_(KIND_MASK_DEFAULT_MODE);
}
}

Expand Down Expand Up @@ -3153,6 +3153,7 @@ fn expr_kind(tcx: ctxt,
ast::expr_addr_of(*) |
ast::expr_binary(*) |
ast::expr_vstore(_, ast::expr_vstore_box) |
ast::expr_vstore(_, ast::expr_vstore_mut_box) |
ast::expr_vstore(_, ast::expr_vstore_uniq) => {
RvalueDatumExpr
}
Expand Down
11 changes: 6 additions & 5 deletions src/librustc/middle/typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ fn ast_ty_to_ty<AC: ast_conv, RS: region_scope Copy Owned>(
let tcx = self.tcx();

match a_seq_ty.ty.node {
// to convert to an e{vec,str}, there can't be a
// mutability argument
_ if a_seq_ty.mutbl != ast::m_imm => (),
ast::ty_vec(mt) => {
return ty::mk_evec(tcx, ast_mt_to_mt(self, rscope, mt), vst);
let mut mt = ast_mt_to_mt(self, rscope, mt);
if a_seq_ty.mutbl == ast::m_mutbl {
mt = { ty: mt.ty, mutbl: ast::m_mutbl };
}
return ty::mk_evec(tcx, mt, vst);
}
ast::ty_path(path, id) => {
ast::ty_path(path, id) if a_seq_ty.mutbl == ast::m_imm => {
match tcx.def_map.find(id) {
Some(ast::def_prim_ty(ast::ty_str)) => {
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
Expand Down
9 changes: 7 additions & 2 deletions src/librustc/middle/typeck/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1713,9 +1713,14 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
}
ast::expr_vec(args, mutbl) => {
let tt = ast_expr_vstore_to_vstore(fcx, ev, args.len(), vst);
let mutability;
match vst {
ast::expr_vstore_mut_box => mutability = ast::m_mutbl,
_ => mutability = mutbl
}
let t: ty::t = fcx.infcx().next_ty_var();
for args.each |e| { bot |= check_expr_with(fcx, *e, t); }
ty::mk_evec(tcx, {ty: t, mutbl: mutbl}, tt)
ty::mk_evec(tcx, {ty: t, mutbl: mutability}, tt)
}
ast::expr_repeat(element, count_expr, mutbl) => {
let count = ty::eval_repeat_count(tcx, count_expr, expr.span);
Expand Down Expand Up @@ -2695,7 +2700,7 @@ fn ast_expr_vstore_to_vstore(fcx: @fn_ctxt, e: @ast::expr, n: uint,
ty::vstore_fixed(u)
}
ast::expr_vstore_uniq => ty::vstore_uniq,
ast::expr_vstore_box => ty::vstore_box,
ast::expr_vstore_box | ast::expr_vstore_mut_box => ty::vstore_box,
ast::expr_vstore_slice => {
let r = fcx.infcx().next_region_var(e.span, e.id);
ty::vstore_slice(r)
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ enum expr_vstore {
expr_vstore_fixed(Option<uint>), // [1,2,3,4]
expr_vstore_uniq, // ~[1,2,3,4]
expr_vstore_box, // @[1,2,3,4]
expr_vstore_mut_box, // @mut [1,2,3,4]
expr_vstore_slice // &[1,2,3,4]
}

Expand Down
10 changes: 7 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ use ast::{_mod, add, arg, arm, attribute,
expr_lit, expr_log, expr_loop, expr_loop_body, expr_mac,
expr_paren, expr_path, expr_rec, expr_repeat, expr_ret,
expr_swap, expr_struct, expr_tup, expr_unary, expr_unary_move,
expr_vec, expr_vstore, expr_while, extern_fn, field, fn_decl,
expr_vec, expr_vstore, expr_vstore_mut_box, expr_while,
extern_fn, field, fn_decl,
foreign_item, foreign_item_const, foreign_item_fn, foreign_mod,
ident, impure_fn, infer, inherited,
item, item_, item_class, item_const, item_enum, item_fn,
Expand Down Expand Up @@ -1450,8 +1451,11 @@ impl Parser {
hi = e.span.hi;
// HACK: turn @[...] into a @-evec
ex = match e.node {
expr_vec(*) | expr_lit(@{node: lit_str(_), span: _})
if m == m_imm => expr_vstore(e, expr_vstore_box),
expr_vec(*) if m == m_mutbl =>
expr_vstore(e, expr_vstore_mut_box),
expr_vec(*) if m == m_imm => expr_vstore(e, expr_vstore_box),
expr_lit(@{node: lit_str(_), span: _}) if m == m_imm =>
expr_vstore(e, expr_vstore_box),
_ => expr_unary(box(m), e)
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,10 @@ fn print_expr_vstore(s: ps, t: ast::expr_vstore) {
ast::expr_vstore_fixed(None) => word(s.s, ~"_"),
ast::expr_vstore_uniq => word(s.s, ~"~"),
ast::expr_vstore_box => word(s.s, ~"@"),
ast::expr_vstore_mut_box => {
word(s.s, ~"@");
word(s.s, ~"mut");
}
ast::expr_vstore_slice => word(s.s, ~"&"),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/auxiliary/trait_inheritance_overloading_xc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub trait MyNum : Add<self,self>, Sub<self,self>, Mul<self,self> {

pub impl int : MyNum {
pure fn add(other: &int) -> int { self + *other }
pure fn sub(other: &int) -> int { self - *other }
pure fn mul(other: &int) -> int { self * *other }
pure fn sub(&self, other: &int) -> int { *self - *other }
pure fn mul(&self, other: &int) -> int { *self * *other }
}

2 changes: 1 addition & 1 deletion src/test/bench/shootout-mandelbrot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct cmplx {
}

impl cmplx : ops::Mul<cmplx,cmplx> {
pure fn mul(x: &cmplx) -> cmplx {
pure fn mul(&self, x: &cmplx) -> cmplx {
cmplx {
re: self.re*(*x).re - self.im*(*x).im,
im: self.re*(*x).im + self.im*(*x).re
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-pass/new-vstore-mut-box-syntax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let x: @mut [int] = @mut [ 1, 2, 3 ];

}

6 changes: 3 additions & 3 deletions src/test/run-pass/operator-overloading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ impl Point : ops::Add<Point,Point> {
}

impl Point : ops::Sub<Point,Point> {
pure fn sub(other: &Point) -> Point {
pure fn sub(&self, other: &Point) -> Point {
Point {x: self.x - (*other).x, y: self.y - (*other).y}
}
}

impl Point : ops::Neg<Point> {
pure fn neg() -> Point {
pure fn neg(&self) -> Point {
Point {x: -self.x, y: -self.y}
}
}
Expand All @@ -40,7 +40,7 @@ impl Point : cmp::Eq {
fn main() {
let mut p = Point {x: 10, y: 20};
p += Point {x: 101, y: 102};
p -= Point {x: 100, y: 100};
p = p - Point {x: 100, y: 100};
assert p + Point {x: 5, y: 5} == Point {x: 16, y: 27};
assert -p == Point {x: -11, y: -22};
assert p[true] == 11;
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/trait-inheritance-overloading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ trait MyNum : Add<self,self>, Sub<self,self>, Mul<self,self> {

impl int : MyNum {
pure fn add(other: &int) -> int { self + *other }
pure fn sub(other: &int) -> int { self - *other }
pure fn mul(other: &int) -> int { self * *other }
pure fn sub(&self, other: &int) -> int { *self - *other }
pure fn mul(&self, other: &int) -> int { *self * *other }
}

fn f<T:Copy MyNum>(x: T, y: T) -> (T, T, T) {
Expand Down