Skip to content

Commit ff94f86

Browse files
committed
auto merge of #15234 : pcwalton/rust/integer-fallback-and-casts, r=alexcrichton
This will break code that looks like: let mut x = 0; while ... { x += 1; } println!("{}", x); Change that code to: let mut x = 0i; while ... { x += 1; } println!("{}", x); Closes #15201. [breaking-change] r? @alexcrichton
2 parents cc5663a + a5bb0a3 commit ff94f86

File tree

338 files changed

+1148
-1146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

338 files changed

+1148
-1146
lines changed

src/doc/guide-unsafe.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ impl<T: Send> Drop for Unique<T> {
267267
// A comparison between the built-in `Box` and this reimplementation
268268
fn main() {
269269
{
270-
let mut x = box 5;
270+
let mut x = box 5i;
271271
*x = 10;
272272
} // `x` is freed here
273273
274274
{
275-
let mut y = Unique::new(5);
275+
let mut y = Unique::new(5i);
276276
*y.borrow_mut() = 10;
277277
} // `y` is freed here
278278
}
@@ -678,7 +678,7 @@ unsafe fn deallocate(ptr: *mut u8, _size: uint, _align: uint) {
678678
679679
#[start]
680680
fn main(argc: int, argv: *const *const u8) -> int {
681-
let x = box 1;
681+
let x = box 1i;
682682
683683
0
684684
}

src/doc/intro.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Check it out:
133133
```
134134
135135
fn dangling() -> Box<int> {
136-
let i = box 1234;
136+
let i = box 1234i;
137137
return i;
138138
}
139139
@@ -143,16 +143,16 @@ fn add_one() -> int {
143143
}
144144
```
145145

146-
Now instead of a stack allocated `1234`,
147-
we have a heap allocated `box 1234`.
146+
Now instead of a stack allocated `1234i`,
147+
we have a heap allocated `box 1234i`.
148148
Whereas `&` borrows a pointer to existing memory,
149149
creating an owned box allocates memory on the heap and places a value in it,
150150
giving you the sole pointer to that memory.
151151
You can roughly compare these two lines:
152152

153153
```
154154
// Rust
155-
let i = box 1234;
155+
let i = box 1234i;
156156
```
157157

158158
```cpp

src/doc/rust.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -442,17 +442,14 @@ of integer literal suffix:
442442
The type of an _unsuffixed_ integer literal is determined by type inference.
443443
If an integer type can be _uniquely_ determined from the surrounding program
444444
context, the unsuffixed integer literal has that type. If the program context
445-
underconstrains the type, the unsuffixed integer literal's type is `int`; if
446-
the program context overconstrains the type, it is considered a static type
447-
error.
445+
underconstrains the type, it is considered a static type error;
446+
if the program context overconstrains the type,
447+
it is also considered a static type error.
448448

449449
Examples of integer literals of various forms:
450450

451451
~~~~
452-
123; 0xff00; // type determined by program context
453-
// defaults to int in absence of type
454-
// information
455-
452+
123i; // type int
456453
123u; // type uint
457454
123_u; // type uint
458455
0xff_u8; // type u8
@@ -469,17 +466,19 @@ A _floating-point literal_ has one of two forms:
469466
second decimal literal.
470467
* A single _decimal literal_ followed by an _exponent_.
471468

472-
By default, a floating-point literal has a generic type, but will fall back to
473-
`f64`. A floating-point literal may be followed (immediately, without any
469+
By default, a floating-point literal has a generic type,
470+
and, like integer literals, the type must be uniquely determined
471+
from the context.
472+
A floating-point literal may be followed (immediately, without any
474473
spaces) by a _floating-point suffix_, which changes the type of the literal.
475474
There are two floating-point suffixes: `f32`, and `f64` (the 32-bit and 64-bit
476475
floating point types).
477476

478477
Examples of floating-point literals of various forms:
479478

480479
~~~~
481-
123.0; // type f64
482-
0.1; // type f64
480+
123.0f64; // type f64
481+
0.1f64; // type f64
483482
0.1f32; // type f32
484483
12E+99_f64; // type f64
485484
~~~~
@@ -2700,9 +2699,9 @@ must be a constant expression that can be evaluated at compile time, such
27002699
as a [literal](#literals) or a [static item](#static-items).
27012700

27022701
~~~~
2703-
[1, 2, 3, 4];
2702+
[1i, 2, 3, 4];
27042703
["a", "b", "c", "d"];
2705-
[0, ..128]; // vector with 128 zeros
2704+
[0i, ..128]; // vector with 128 zeros
27062705
[0u8, 0u8, 0u8, 0u8];
27072706
~~~~
27082707

@@ -2881,7 +2880,7 @@ equals sign (`=`) and an [rvalue](#lvalues-rvalues-and-temporaries) expression.
28812880
Evaluating an assignment expression [either copies or moves](#moved-and-copied-types) its right-hand operand to its left-hand operand.
28822881

28832882
~~~~
2884-
# let mut x = 0;
2883+
# let mut x = 0i;
28852884
# let y = 0;
28862885
28872886
x = y;
@@ -2932,7 +2931,7 @@ paren_expr : '(' expr ')' ;
29322931
An example of a parenthesized expression:
29332932

29342933
~~~~
2935-
let x = (2 + 3) * 4;
2934+
let x: int = (2 + 3) * 4;
29362935
~~~~
29372936

29382937

@@ -3016,7 +3015,7 @@ conditional expression evaluates to `false`, the `while` expression completes.
30163015
An example:
30173016

30183017
~~~~
3019-
let mut i = 0;
3018+
let mut i = 0u;
30203019
30213020
while i < 10 {
30223021
println!("hello");
@@ -3262,7 +3261,7 @@ Patterns can also dereference pointers by using the `&`,
32623261
on `x: &int` are equivalent:
32633262

32643263
~~~~
3265-
# let x = &3;
3264+
# let x = &3i;
32663265
let y = match *x { 0 => "zero", _ => "some" };
32673266
let z = match x { &0 => "zero", _ => "some" };
32683267
@@ -3285,7 +3284,7 @@ A range of values may be specified with `..`.
32853284
For example:
32863285

32873286
~~~~
3288-
# let x = 2;
3287+
# let x = 2i;
32893288
32903289
let message = match x {
32913290
0 | 1 => "not many",

src/doc/tutorial.md

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ write function, variable, and module names with lowercase letters, using
262262
underscores where they help readability, while writing types in camel case.
263263

264264
~~~
265-
let my_variable = 100;
265+
let my_variable = 100i;
266266
type MyType = int; // primitive types are _not_ camel case
267267
~~~
268268

@@ -276,7 +276,7 @@ write a piece of code like this:
276276

277277
~~~~
278278
# let item = "salad";
279-
let price;
279+
let price: f64;
280280
if item == "salad" {
281281
price = 3.50;
282282
} else if item == "muffin" {
@@ -290,7 +290,7 @@ But, in Rust, you don't have to repeat the name `price`:
290290

291291
~~~~
292292
# let item = "salad";
293-
let price =
293+
let price: f64 =
294294
if item == "salad" {
295295
3.50
296296
} else if item == "muffin" {
@@ -337,11 +337,10 @@ suffix that can be used to indicate the type of a literal: `i` for `int`,
337337
In the absence of an integer literal suffix, Rust will infer the
338338
integer type based on type annotations and function signatures in the
339339
surrounding program. In the absence of any type information at all,
340-
Rust will assume that an unsuffixed integer literal has type
341-
`int`.
340+
Rust will report an error and request that the type be specified explicitly.
342341

343342
~~~~
344-
let a = 1; // `a` is an `int`
343+
let a: int = 1; // `a` is an `int`
345344
let b = 10i; // `b` is an `int`, due to the `i` suffix
346345
let c = 100u; // `c` is a `uint`
347346
let d = 1000i32; // `d` is an `i32`
@@ -475,7 +474,7 @@ against each pattern in order until one matches. The matching pattern
475474
executes its corresponding arm.
476475

477476
~~~~
478-
let my_number = 1;
477+
let my_number = 1i;
479478
match my_number {
480479
0 => println!("zero"),
481480
1 | 2 => println!("one or two"),
@@ -501,7 +500,7 @@ matches any single value. (`..`) is a different wildcard that can match
501500
one or more fields in an `enum` variant.
502501

503502
~~~
504-
# let my_number = 1;
503+
# let my_number = 1i;
505504
match my_number {
506505
0 => { println!("zero") }
507506
_ => { println!("something else") }
@@ -584,7 +583,7 @@ keyword `break` aborts the loop, and `continue` aborts the current
584583
iteration and continues with the next.
585584

586585
~~~~
587-
let mut cake_amount = 8;
586+
let mut cake_amount = 8i;
588587
while cake_amount > 0 {
589588
cake_amount -= 1;
590589
}
@@ -944,7 +943,7 @@ The `box` operator performs memory allocation on the heap:
944943
~~~~
945944
{
946945
// an integer allocated on the heap
947-
let y = box 10;
946+
let y = box 10i;
948947
}
949948
// the destructor frees the heap memory as soon as `y` goes out of scope
950949
~~~~
@@ -1165,7 +1164,7 @@ let z = x;
11651164
The mutability of a value may be changed by moving it to a new owner:
11661165

11671166
~~~~
1168-
let r = box 13;
1167+
let r = box 13i;
11691168
let mut s = r; // box becomes mutable
11701169
*s += 1;
11711170
let t = s; // box becomes immutable
@@ -1285,9 +1284,9 @@ Using the generic `List<T>` works much like before, thanks to type inference:
12851284
# Cons(value, box xs)
12861285
# }
12871286
let mut xs = Nil; // Unknown type! This is a `List<T>`, but `T` can be anything.
1288-
xs = prepend(xs, 10); // Here the compiler infers `xs`'s type as `List<int>`.
1289-
xs = prepend(xs, 15);
1290-
xs = prepend(xs, 20);
1287+
xs = prepend(xs, 10i); // Here the compiler infers `xs`'s type as `List<int>`.
1288+
xs = prepend(xs, 15i);
1289+
xs = prepend(xs, 20i);
12911290
~~~
12921291

12931292
The code sample above demonstrates type inference making most type annotations optional. It is
@@ -1410,12 +1409,12 @@ Beyond the properties granted by the size, an owned box behaves as a regular
14101409
value by inheriting the mutability and lifetime of the owner:
14111410

14121411
~~~~
1413-
let x = 5; // immutable
1414-
let mut y = 5; // mutable
1412+
let x = 5i; // immutable
1413+
let mut y = 5i; // mutable
14151414
y += 2;
14161415
1417-
let x = box 5; // immutable
1418-
let mut y = box 5; // mutable
1416+
let x = box 5i; // immutable
1417+
let mut y = box 5i; // mutable
14191418
*y += 2; // the `*` operator is needed to access the contained value
14201419
~~~~
14211420

@@ -1507,7 +1506,7 @@ freezing enforced statically at compile-time. An example of a non-`Freeze` type
15071506
is [`RefCell<T>`][refcell].
15081507

15091508
~~~~
1510-
let mut x = 5;
1509+
let mut x = 5i;
15111510
{
15121511
let y = &x; // `x` is now frozen. It cannot be modified or re-assigned.
15131512
}
@@ -1523,8 +1522,8 @@ Rust uses the unary star operator (`*`) to access the contents of a
15231522
box or pointer, similarly to C.
15241523

15251524
~~~
1526-
let owned = box 10;
1527-
let borrowed = &20;
1525+
let owned = box 10i;
1526+
let borrowed = &20i;
15281527
15291528
let sum = *owned + *borrowed;
15301529
~~~
@@ -1534,9 +1533,9 @@ assignments. Such an assignment modifies the value that the pointer
15341533
points to.
15351534

15361535
~~~
1537-
let mut owned = box 10;
1536+
let mut owned = box 10i;
15381537
1539-
let mut value = 20;
1538+
let mut value = 20i;
15401539
let borrowed = &mut value;
15411540
15421541
*owned = *borrowed + 100;
@@ -1654,12 +1653,12 @@ Unicode code points, so they cannot be freely mutated without the ability to
16541653
alter the length.
16551654

16561655
~~~
1657-
let mut xs = [1, 2, 3];
1656+
let mut xs = [1i, 2i, 3i];
16581657
let view = xs.mut_slice(0, 2);
16591658
view[0] = 5;
16601659
16611660
// The type of a mutable slice is written as `&mut [T]`
1662-
let ys: &mut [int] = &mut [1, 2, 3];
1661+
let ys: &mut [int] = &mut [1i, 2i, 3i];
16631662
~~~
16641663

16651664
Square brackets denote indexing into a slice or fixed-size vector:

src/liballoc/arc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,14 @@ mod tests {
376376

377377
#[test]
378378
fn test_live() {
379-
let x = Arc::new(5);
379+
let x = Arc::new(5i);
380380
let y = x.downgrade();
381381
assert!(y.upgrade().is_some());
382382
}
383383

384384
#[test]
385385
fn test_dead() {
386-
let x = Arc::new(5);
386+
let x = Arc::new(5i);
387387
let y = x.downgrade();
388388
drop(x);
389389
assert!(y.upgrade().is_none());

src/liballoc/heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ mod bench {
329329
#[bench]
330330
fn alloc_owned_small(b: &mut Bencher) {
331331
b.iter(|| {
332-
box 10
332+
box 10i
333333
})
334334
}
335335
}

src/libcollections/hash/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,12 @@ mod tests {
342342
assert_eq!(hasher.hash(& &[1u8, 2u8, 3u8]), 9);
343343

344344
unsafe {
345-
let ptr: *const int = mem::transmute(5);
345+
let ptr: *const int = mem::transmute(5i);
346346
assert_eq!(hasher.hash(&ptr), 5);
347347
}
348348

349349
unsafe {
350-
let ptr: *mut int = mem::transmute(5);
350+
let ptr: *mut int = mem::transmute(5i);
351351
assert_eq!(hasher.hash(&ptr), 5);
352352
}
353353
}

src/libcollections/ringbuf.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,15 @@ mod tests {
572572
fn bench_push_back(b: &mut test::Bencher) {
573573
let mut deq = RingBuf::new();
574574
b.iter(|| {
575-
deq.push_back(0);
575+
deq.push_back(0i);
576576
})
577577
}
578578

579579
#[bench]
580580
fn bench_push_front(b: &mut test::Bencher) {
581581
let mut deq = RingBuf::new();
582582
b.iter(|| {
583-
deq.push_front(0);
583+
deq.push_front(0i);
584584
})
585585
}
586586

@@ -589,7 +589,7 @@ mod tests {
589589
let mut deq = RingBuf::new();
590590
b.iter(|| {
591591
for _ in range(0i, 65) {
592-
deq.push_front(1);
592+
deq.push_front(1i);
593593
}
594594
})
595595
}
@@ -651,10 +651,10 @@ mod tests {
651651
#[test]
652652
fn test_with_capacity() {
653653
let mut d = RingBuf::with_capacity(0);
654-
d.push_back(1);
654+
d.push_back(1i);
655655
assert_eq!(d.len(), 1);
656656
let mut d = RingBuf::with_capacity(50);
657-
d.push_back(1);
657+
d.push_back(1i);
658658
assert_eq!(d.len(), 1);
659659
}
660660

0 commit comments

Comments
 (0)