Skip to content

Commit 4030aaf

Browse files
committed
rustc: make integral type inference transactional, close #3211, close #4401, close #3398.
1 parent 592c2e1 commit 4030aaf

File tree

13 files changed

+64
-91
lines changed

13 files changed

+64
-91
lines changed

src/libcore/int-template.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use char;
1818
use cmp::{Eq, Ord};
1919
use cmp;
2020
use from_str::FromStr;
21-
use iter;
2221
use num;
2322
use num::Num::from_int;
2423
use prelude::*;
@@ -201,26 +200,6 @@ impl T: num::One {
201200
static pure fn one() -> T { 1 }
202201
}
203202

204-
impl T: iter::Times {
205-
#[inline(always)]
206-
#[doc = "A convenience form for basic iteration. Given a variable `x` \
207-
of any numeric type, the expression `for x.times { /* anything */ }` \
208-
will execute the given function exactly x times. If we assume that \
209-
`x` is an int, this is functionally equivalent to \
210-
`for int::range(0, x) |_i| { /* anything */ }`."]
211-
pure fn times(&self, it: fn() -> bool) {
212-
if is_negative(*self) {
213-
fail fmt!("The .times method expects a nonnegative number, \
214-
but found %?", self);
215-
}
216-
let mut i = *self;
217-
while i > 0 {
218-
if !it() { break }
219-
i -= 1;
220-
}
221-
}
222-
}
223-
224203
/**
225204
* Parse a buffer of bytes
226205
*
@@ -357,23 +336,6 @@ fn test_interfaces() {
357336
test(10 as T);
358337
}
359338

360-
#[test]
361-
fn test_times() {
362-
use iter::Times;
363-
let ten = 10 as T;
364-
let mut accum = 0;
365-
for ten.times { accum += 1; }
366-
assert (accum == 10);
367-
}
368-
369-
#[test]
370-
#[should_fail]
371-
#[ignore(cfg(windows))]
372-
fn test_times_negative() {
373-
use iter::Times;
374-
for (-10).times { log(error, ~"nope!"); }
375-
}
376-
377339
#[test]
378340
pub fn test_ranges() {
379341
let mut l = ~[];

src/libcore/to_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ impl<A: ToStr> ~A: ToStr {
146146
mod tests {
147147
#[test]
148148
fn test_simple_types() {
149-
assert 1.to_str() == ~"1";
150-
assert (-1).to_str() == ~"-1";
149+
assert 1i.to_str() == ~"1";
150+
assert (-1i).to_str() == ~"-1";
151151
assert 200u.to_str() == ~"200";
152152
assert 2u8.to_str() == ~"2";
153153
assert true.to_str() == ~"true";

src/libcore/uint-template.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use T_SIGNED = self::inst::T_SIGNED;
1818
use char;
1919
use cmp::{Eq, Ord};
2020
use from_str::FromStr;
21-
use iter;
2221
use num;
2322
use option::{None, Option, Some};
2423
use str;
@@ -161,22 +160,6 @@ impl T: num::One {
161160
static pure fn one() -> T { 1 }
162161
}
163162

164-
impl T: iter::Times {
165-
#[inline(always)]
166-
#[doc = "A convenience form for basic iteration. Given a variable `x` \
167-
of any numeric type, the expression `for x.times { /* anything */ }` \
168-
will execute the given function exactly x times. If we assume that \
169-
`x` is an int, this is functionally equivalent to \
170-
`for int::range(0, x) |_i| { /* anything */ }`."]
171-
pure fn times(&self, it: fn() -> bool) {
172-
let mut i = *self;
173-
while i > 0 {
174-
if !it() { break }
175-
i -= 1;
176-
}
177-
}
178-
}
179-
180163
/**
181164
* Parse a buffer of bytes
182165
*
@@ -349,14 +332,6 @@ pub fn to_str_radix17() {
349332
uint::to_str(100u, 17u);
350333
}
351334

352-
#[test]
353-
pub fn test_times() {
354-
use iter::Times;
355-
let ten = 10 as T;
356-
let mut accum = 0;
357-
for ten.times { accum += 1; }
358-
assert (accum == 10);
359-
}
360335
use io;
361336
#[test]
362337
pub fn test_ranges() {

src/libcore/uint-template/uint.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ pub use self::inst::{
1515
next_power_of_two
1616
};
1717

18-
mod inst {
18+
pub mod inst {
1919
use sys;
2020
use uint;
21+
use iter;
2122

2223
pub type T = uint;
2324
#[allow(non_camel_case_types)]
@@ -107,6 +108,27 @@ mod inst {
107108
return true;
108109
}
109110

111+
pub impl uint: iter::Times {
112+
#[inline(always)]
113+
/**
114+
* A convenience form for basic iteration. Given a uint `x`,
115+
* `for x.times { ... }` executes the given block x times.
116+
*
117+
* Equivalent to `for uint::range(0, x) |_| { ... }`.
118+
*
119+
* Not defined on all integer types to permit unambiguous
120+
* use with integer literals of inferred integer-type as
121+
* the self-value (eg. `for 100.times { ... }`).
122+
*/
123+
pure fn times(&self, it: fn() -> bool) {
124+
let mut i = *self;
125+
while i > 0 {
126+
if !it() { break }
127+
i -= 1;
128+
}
129+
}
130+
}
131+
110132
/// Returns the smallest power of 2 greater than or equal to `n`
111133
#[inline(always)]
112134
pub pure fn next_power_of_two(n: uint) -> uint {
@@ -174,4 +196,13 @@ mod inst {
174196
assert(uint::div_ceil(3u, 4u) == 1u);
175197
assert(uint::div_round(3u, 4u) == 1u);
176198
}
199+
200+
#[test]
201+
pub fn test_times() {
202+
use iter::Times;
203+
let ten = 10 as uint;
204+
let mut accum = 0;
205+
for ten.times { accum += 1; }
206+
assert (accum == 10);
207+
}
177208
}

src/librustc/middle/privacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn check_crate(tcx: ty::ctxt,
4141
let legacy_exports = has_legacy_export_attr(crate.node.attrs);
4242

4343
// Adds structs that are privileged to this scope.
44-
let add_privileged_items: @fn(&[@ast::item]) -> int = |items| {
44+
let add_privileged_items: @fn(&[@ast::item]) -> uint = |items| {
4545
let mut count = 0;
4646
for items.each |item| {
4747
match item.node {

src/librustc/middle/typeck/check/method.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ pub impl LookupContext {
257257
match ty::deref(self.tcx(), ty, false) {
258258
None => None,
259259
Some(t) => {
260-
//FIXME(#3211) -- probably want to force ivars
261260
Some(structurally_resolved_type(self.fcx,
262261
self.self_expr.span,
263262
t.ty))
@@ -907,8 +906,8 @@ pub impl LookupContext {
907906
let tcx = self.tcx();
908907
match ty::get(self_ty).sty {
909908
ty_box(*) | ty_uniq(*) | ty_rptr(*) |
910-
ty_infer(IntVar(_)) | // FIXME(#3211)---should be resolved
911-
ty_infer(FloatVar(_)) | // FIXME(#3211)---should be resolved
909+
ty_infer(IntVar(_)) |
910+
ty_infer(FloatVar(_)) |
912911
ty_self | ty_param(*) | ty_nil | ty_bot | ty_bool |
913912
ty_int(*) | ty_uint(*) |
914913
ty_float(*) | ty_enum(*) | ty_ptr(*) | ty_rec(*) |

src/librustc/middle/typeck/infer/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,10 @@ impl @InferCtxt {
562562
debug!("rollback!");
563563
rollback_to(&self.ty_var_bindings, snapshot.ty_var_bindings_len);
564564

565-
// FIXME(#3211) -- int_var and float_var not transactional
566-
//rollback_to(&self.int_var_bindings,
567-
// snapshot.int_var_bindings_len);
568-
//rollback_to(&self.float_var_bindings,
569-
// snapshot.float_var_bindings_len);
565+
rollback_to(&self.int_var_bindings,
566+
snapshot.int_var_bindings_len);
567+
rollback_to(&self.float_var_bindings,
568+
snapshot.float_var_bindings_len);
570569

571570
self.region_vars.rollback_to(snapshot.region_vars_snapshot);
572571
}

src/libstd/cmp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ impl f64: FuzzyEq {
5454

5555
#[test]
5656
fn test_fuzzy_equals() {
57-
assert (&1.0).fuzzy_eq(&1.0);
57+
assert (&1.0f).fuzzy_eq(&1.0);
5858
assert (&1.0f32).fuzzy_eq(&1.0f32);
5959
assert (&1.0f64).fuzzy_eq(&1.0f64);
6060
}
6161

6262
#[test]
6363
fn test_fuzzy_eq_eps() {
64-
assert (&1.2).fuzzy_eq_eps(&0.9, &0.5);
65-
assert !(&1.5).fuzzy_eq_eps(&0.9, &0.5);
64+
assert (&1.2f).fuzzy_eq_eps(&0.9, &0.5);
65+
assert !(&1.5f).fuzzy_eq_eps(&0.9, &0.5);
6666
}

src/test/run-pass/fn-pattern-expected-type-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
let v = [ (1, 2), (3, 4), (5, 6) ];
12+
let v : &[(int,int)] = &[ (1, 2), (3, 4), (5, 6) ];
1313
for v.each |&(x, y)| {
1414
io::println(y.to_str());
1515
io::println(x.to_str());
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
struct S<T>(T);
22

33
fn main() {
4-
let s = S(2);
4+
let s = S(2i);
55
io::println(s.to_str());
66
}
77

src/test/run-pass/issue-3211.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let mut x = 0;
3+
for 4096.times {
4+
x += 1;
5+
}
6+
assert x == 4096;
7+
io::println(fmt!("x = %u", x));
8+
}

src/test/run-pass/issue-4401.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let mut count = 0;
3+
for 999_999.times() {
4+
count += 1;
5+
}
6+
assert count == 999_999;
7+
io::println(fmt!("%u", count));
8+
}

src/test/run-pass/numeric-method-autoexport.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,11 @@
1515
fn main() {
1616
// ints
1717
// num
18-
assert 15.add(&6) == 21;
18+
assert 15i.add(&6) == 21;
1919
assert 15i8.add(&6i8) == 21i8;
2020
assert 15i16.add(&6i16) == 21i16;
2121
assert 15i32.add(&6i32) == 21i32;
2222
assert 15i64.add(&6i64) == 21i64;
23-
// times
24-
15.times(|| false);
25-
15i8.times(|| false);
26-
15i16.times(|| false);
27-
15i32.times(|| false);
28-
15i64.times(|| false);
2923

3024
// uints
3125
// num
@@ -34,12 +28,9 @@ fn main() {
3428
assert 15u16.add(&6u16) == 21u16;
3529
assert 15u32.add(&6u32) == 21u32;
3630
assert 15u64.add(&6u64) == 21u64;
31+
3732
// times
3833
15u.times(|| false);
39-
15u8.times(|| false);
40-
15u16.times(|| false);
41-
15u32.times(|| false);
42-
15u64.times(|| false);
4334

4435
// floats
4536
// num

0 commit comments

Comments
 (0)