Skip to content

Commit 7274fcf

Browse files
committed
---
yaml --- r: 148184 b: refs/heads/try2 c: a34727f h: refs/heads/master v: v3
1 parent bc76dc1 commit 7274fcf

File tree

6 files changed

+142
-31
lines changed

6 files changed

+142
-31
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 4fc0452acef1355ba566a30c5bd04ccd3b9acef2
8+
refs/heads/try2: a34727f27657cbed71fb2326dc6f1766fee95a4c
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/librustc/middle/typeck/check/mod.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,13 @@ impl PurityState {
213213
}
214214
}
215215

216-
/// Whether `check_binop` allows overloaded operators to be invoked.
216+
/// Whether `check_binop` is part of an assignment or not.
217+
/// Used to know wether we allow user overloads and to print
218+
/// better messages on error.
217219
#[deriving(Eq)]
218-
enum AllowOverloadedOperatorsFlag {
219-
AllowOverloadedOperators,
220-
DontAllowOverloadedOperators,
220+
enum IsBinopAssignment{
221+
SimpleBinop,
222+
BinopAssignment,
221223
}
222224

223225
#[deriving(Clone)]
@@ -2086,7 +2088,7 @@ pub fn check_expr_with_unifier(fcx: @FnCtxt,
20862088
rhs: @ast::Expr,
20872089
// Used only in the error case
20882090
expected_result: Option<ty::t>,
2089-
allow_overloaded_operators: AllowOverloadedOperatorsFlag
2091+
is_binop_assignment: IsBinopAssignment
20902092
) {
20912093
let tcx = fcx.ccx.tcx;
20922094

@@ -2136,9 +2138,9 @@ pub fn check_expr_with_unifier(fcx: @FnCtxt,
21362138

21372139
}
21382140

2139-
// Check for overloaded operators if allowed.
2141+
// Check for overloaded operators if not an assignment.
21402142
let result_t;
2141-
if allow_overloaded_operators == AllowOverloadedOperators {
2143+
if is_binop_assignment == SimpleBinop {
21422144
result_t = check_user_binop(fcx,
21432145
callee_id,
21442146
expr,
@@ -2150,13 +2152,14 @@ pub fn check_expr_with_unifier(fcx: @FnCtxt,
21502152
} else {
21512153
fcx.type_error_message(expr.span,
21522154
|actual| {
2153-
format!("binary operation {} cannot be \
2154-
applied to type `{}`",
2155-
ast_util::binop_to_str(op),
2156-
actual)
2155+
format!("binary assignment operation \
2156+
{}= cannot be applied to type `{}`",
2157+
ast_util::binop_to_str(op),
2158+
actual)
21572159
},
21582160
lhs_t,
21592161
None);
2162+
check_expr(fcx, rhs);
21602163
result_t = ty::mk_err();
21612164
}
21622165

@@ -2760,7 +2763,7 @@ pub fn check_expr_with_unifier(fcx: @FnCtxt,
27602763
lhs,
27612764
rhs,
27622765
expected,
2763-
AllowOverloadedOperators);
2766+
SimpleBinop);
27642767

27652768
let lhs_ty = fcx.expr_ty(lhs);
27662769
let rhs_ty = fcx.expr_ty(rhs);
@@ -2781,7 +2784,7 @@ pub fn check_expr_with_unifier(fcx: @FnCtxt,
27812784
lhs,
27822785
rhs,
27832786
expected,
2784-
DontAllowOverloadedOperators);
2787+
BinopAssignment);
27852788

27862789
let lhs_t = fcx.expr_ty(lhs);
27872790
let result_t = fcx.expr_ty(expr);

branches/try2/src/libstd/io/extensions.rs

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
// XXX: Not sure how this should be structured
1414
// XXX: Iteration should probably be considered separately
1515

16+
use container::Container;
1617
use iter::Iterator;
1718
use option::Option;
1819
use io::Reader;
19-
use vec::OwnedVector;
20+
use vec::{OwnedVector, ImmutableVector};
2021

2122
/// An iterator that reads a single byte on each iteration,
2223
/// until `.read_byte()` returns `None`.
@@ -117,16 +118,23 @@ pub fn u64_from_be_bytes(data: &[u8],
117118
start: uint,
118119
size: uint)
119120
-> u64 {
120-
let mut sz = size;
121-
assert!((sz <= 8u));
122-
let mut val = 0_u64;
123-
let mut pos = start;
124-
while sz > 0u {
125-
sz -= 1u;
126-
val += (data[pos] as u64) << ((sz * 8u) as u64);
127-
pos += 1u;
128-
}
129-
return val;
121+
use ptr::{copy_nonoverlapping_memory, offset, mut_offset};
122+
use unstable::intrinsics::from_be64;
123+
use vec::MutableVector;
124+
125+
assert!(size <= 8u);
126+
127+
if data.len() - start < size {
128+
fail!("index out of bounds");
129+
}
130+
131+
let mut buf = [0u8, ..8];
132+
unsafe {
133+
let ptr = offset(data.as_ptr(), start as int);
134+
let out = buf.as_mut_ptr();
135+
copy_nonoverlapping_memory(mut_offset(out, (8 - size) as int), ptr, size);
136+
from_be64(*(out as *i64)) as u64
137+
}
130138
}
131139

132140
#[cfg(test)]
@@ -465,4 +473,86 @@ mod test {
465473
assert!(reader.read_le_f32() == 8.1250);
466474
}
467475

476+
#[test]
477+
fn test_u64_from_be_bytes() {
478+
use super::u64_from_be_bytes;
479+
480+
let buf = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
481+
482+
// Aligned access
483+
assert_eq!(u64_from_be_bytes(buf, 0, 0), 0);
484+
assert_eq!(u64_from_be_bytes(buf, 0, 1), 0x01);
485+
assert_eq!(u64_from_be_bytes(buf, 0, 2), 0x0102);
486+
assert_eq!(u64_from_be_bytes(buf, 0, 3), 0x010203);
487+
assert_eq!(u64_from_be_bytes(buf, 0, 4), 0x01020304);
488+
assert_eq!(u64_from_be_bytes(buf, 0, 5), 0x0102030405);
489+
assert_eq!(u64_from_be_bytes(buf, 0, 6), 0x010203040506);
490+
assert_eq!(u64_from_be_bytes(buf, 0, 7), 0x01020304050607);
491+
assert_eq!(u64_from_be_bytes(buf, 0, 8), 0x0102030405060708);
492+
493+
// Unaligned access
494+
assert_eq!(u64_from_be_bytes(buf, 1, 0), 0);
495+
assert_eq!(u64_from_be_bytes(buf, 1, 1), 0x02);
496+
assert_eq!(u64_from_be_bytes(buf, 1, 2), 0x0203);
497+
assert_eq!(u64_from_be_bytes(buf, 1, 3), 0x020304);
498+
assert_eq!(u64_from_be_bytes(buf, 1, 4), 0x02030405);
499+
assert_eq!(u64_from_be_bytes(buf, 1, 5), 0x0203040506);
500+
assert_eq!(u64_from_be_bytes(buf, 1, 6), 0x020304050607);
501+
assert_eq!(u64_from_be_bytes(buf, 1, 7), 0x02030405060708);
502+
assert_eq!(u64_from_be_bytes(buf, 1, 8), 0x0203040506070809);
503+
}
504+
}
505+
506+
#[cfg(test)]
507+
mod bench {
508+
use extra::test::BenchHarness;
509+
use container::Container;
510+
511+
macro_rules! u64_from_be_bytes_bench_impl(
512+
($size:expr, $stride:expr, $start_index:expr) =>
513+
({
514+
use vec;
515+
use super::u64_from_be_bytes;
516+
517+
let data = vec::from_fn($stride*100+$start_index, |i| i as u8);
518+
let mut sum = 0u64;
519+
bh.iter(|| {
520+
let mut i = $start_index;
521+
while (i < data.len()) {
522+
sum += u64_from_be_bytes(data, i, $size);
523+
i += $stride;
524+
}
525+
});
526+
})
527+
)
528+
529+
#[bench]
530+
fn u64_from_be_bytes_4_aligned(bh: &mut BenchHarness) {
531+
u64_from_be_bytes_bench_impl!(4, 4, 0);
532+
}
533+
534+
#[bench]
535+
fn u64_from_be_bytes_4_unaligned(bh: &mut BenchHarness) {
536+
u64_from_be_bytes_bench_impl!(4, 4, 1);
537+
}
538+
539+
#[bench]
540+
fn u64_from_be_bytes_7_aligned(bh: &mut BenchHarness) {
541+
u64_from_be_bytes_bench_impl!(7, 8, 0);
542+
}
543+
544+
#[bench]
545+
fn u64_from_be_bytes_7_unaligned(bh: &mut BenchHarness) {
546+
u64_from_be_bytes_bench_impl!(7, 8, 1);
547+
}
548+
549+
#[bench]
550+
fn u64_from_be_bytes_8_aligned(bh: &mut BenchHarness) {
551+
u64_from_be_bytes_bench_impl!(8, 8, 0);
552+
}
553+
554+
#[bench]
555+
fn u64_from_be_bytes_8_unaligned(bh: &mut BenchHarness) {
556+
u64_from_be_bytes_bench_impl!(8, 8, 1);
557+
}
468558
}

branches/try2/src/libsyntax/visit.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,13 @@ pub fn walk_item<E: Clone, V: Visitor<E>>(visitor: &mut V, item: &Item, env: E)
223223
walk_enum_def(visitor, enum_definition, type_parameters, env)
224224
}
225225
ItemImpl(ref type_parameters,
226-
ref trait_references,
227-
typ,
228-
ref methods) => {
226+
ref trait_reference,
227+
typ,
228+
ref methods) => {
229229
visitor.visit_generics(type_parameters, env.clone());
230-
for trait_reference in trait_references.iter() {
231-
walk_trait_ref(visitor, trait_reference, env.clone())
230+
match *trait_reference {
231+
Some(ref trait_reference) => walk_trait_ref(visitor, trait_reference, env.clone()),
232+
None => ()
232233
}
233234
visitor.visit_ty(typ, env.clone());
234235
for method in methods.iter() {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo;
12+
13+
fn main() {
14+
let mut a = Foo;
15+
let ref b = Foo;
16+
a += *b; //~ Error: binary assignment operation += cannot be applied to type `Foo`
17+
}

branches/try2/src/test/compile-fail/issue-5239-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
// Regression test for issue #5239
1212

1313
fn main() {
14-
let x: |int| -> int = |ref x| { x += 1; }; //~ ERROR binary operation + cannot be applied to type `&int`
14+
let x: |int| -> int = |ref x| { x += 1; }; //~ ERROR binary assignment operation += cannot be applied to type `&int`
1515
}

0 commit comments

Comments
 (0)