Skip to content

Commit 5af3dfe

Browse files
committed
---
yaml --- r: 65505 b: refs/heads/master c: b8391cc h: refs/heads/master i: 65503: c416e1b v: v3
1 parent ee10eac commit 5af3dfe

File tree

17 files changed

+421
-1008
lines changed

17 files changed

+421
-1008
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 6e075b6bb6c15e8fa358e08670687e3c94f4565b
2+
refs/heads/master: b8391ccea0b2e2718a4d4ef999e9f03583c7ddea
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/src/librustc/middle/lint.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub enum lint {
9696
unnecessary_allocation,
9797

9898
missing_doc,
99+
unreachable_code,
99100
}
100101

101102
pub fn level_to_str(lv: level) -> &'static str {
@@ -273,6 +274,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
273274
desc: "detects missing documentation for public members",
274275
default: allow
275276
}),
277+
278+
("unreachable_code",
279+
LintSpec {
280+
lint: unreachable_code,
281+
desc: "detects unreachable code",
282+
default: warn
283+
}),
276284
];
277285

278286
/*

trunk/src/librustc/middle/typeck/check/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ use core::prelude::*;
8181
use middle::const_eval;
8282
use middle::pat_util::pat_id_map;
8383
use middle::pat_util;
84+
use middle::lint::unreachable_code;
8485
use middle::ty::{FnSig, VariantInfo_};
8586
use middle::ty::{ty_param_bounds_and_ty, ty_param_substs_and_ty};
8687
use middle::ty::{substs, param_ty};
@@ -2937,7 +2938,8 @@ pub fn check_block_with_expected(fcx: @mut FnCtxt,
29372938
let mut any_err = false;
29382939
for blk.node.stmts.each |s| {
29392940
check_stmt(fcx, *s);
2940-
let s_ty = fcx.node_ty(ast_util::stmt_id(*s));
2941+
let s_id = ast_util::stmt_id(*s);
2942+
let s_ty = fcx.node_ty(s_id);
29412943
if last_was_bot && !warned && match s.node {
29422944
ast::stmt_decl(@codemap::spanned { node: ast::decl_local(_),
29432945
_}, _) |
@@ -2946,7 +2948,8 @@ pub fn check_block_with_expected(fcx: @mut FnCtxt,
29462948
}
29472949
_ => false
29482950
} {
2949-
fcx.ccx.tcx.sess.span_warn(s.span, "unreachable statement");
2951+
fcx.ccx.tcx.sess.add_lint(unreachable_code, s_id, s.span,
2952+
~"unreachable statement");
29502953
warned = true;
29512954
}
29522955
if ty::type_is_bot(s_ty) {

trunk/src/libstd/hashmap.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ pub fn linear_map_with_capacity<K:Eq + Hash,V>(
7272
fn linear_map_with_capacity_and_keys<K:Eq + Hash,V>(
7373
k0: u64, k1: u64,
7474
initial_capacity: uint) -> HashMap<K, V> {
75+
let cap = uint::max(INITIAL_CAPACITY, initial_capacity);
7576
HashMap {
7677
k0: k0, k1: k1,
77-
resize_at: resize_at(initial_capacity),
78+
resize_at: resize_at(cap),
7879
size: 0,
79-
buckets: vec::from_fn(initial_capacity, |_| None)
80+
buckets: vec::from_fn(cap, |_| None)
8081
}
8182
}
8283

@@ -480,7 +481,8 @@ pub impl<K: Hash + Eq, V> HashMap<K, V> {
480481
}
481482

482483
fn consume(&mut self, f: &fn(K, V)) {
483-
let buckets = replace(&mut self.buckets, ~[]);
484+
let buckets = replace(&mut self.buckets,
485+
vec::from_fn(INITIAL_CAPACITY, |_| None));
484486
self.size = 0;
485487

486488
do vec::consume(buckets) |_, bucket| {
@@ -664,6 +666,12 @@ mod test_map {
664666
use super::*;
665667
use uint;
666668

669+
#[test]
670+
fn test_create_capacity_zero() {
671+
let mut m = HashMap::with_capacity(0);
672+
assert!(m.insert(1, 1));
673+
}
674+
667675
#[test]
668676
fn test_insert() {
669677
let mut m = HashMap::new();
@@ -771,6 +779,14 @@ mod test_map {
771779
assert_eq!(m2.get(&2), &3);
772780
}
773781

782+
#[test]
783+
fn test_consume_still_usable() {
784+
let mut m = HashMap::new();
785+
assert!(m.insert(1, 2));
786+
do m.consume |_, _| {}
787+
assert!(m.insert(1, 2));
788+
}
789+
774790
#[test]
775791
fn test_iterate() {
776792
let mut m = linear_map_with_capacity(4);

trunk/src/libstd/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub use path::PosixPath;
4545
pub use path::WindowsPath;
4646
pub use ptr::Ptr;
4747
pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr};
48-
pub use str::{StrSlice, OwnedStr};
48+
pub use str::{StrSlice, OwnedStr, StrUtil};
4949
pub use from_str::{FromStr};
5050
pub use to_bytes::IterBytes;
5151
pub use to_str::{ToStr, ToStrConsume};

trunk/src/libstd/str.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,33 +2165,50 @@ pub fn as_bytes_slice<'a>(s: &'a str) -> &'a [u8] {
21652165
}
21662166

21672167
/**
2168-
* Work with the byte buffer of a string as a null-terminated C string.
2169-
*
2170-
* Allows for unsafe manipulation of strings, which is useful for foreign
2171-
* interop. This is similar to `str::as_buf`, but guarantees null-termination.
2172-
* If the given slice is not already null-terminated, this function will
2173-
* allocate a temporary, copy the slice, null terminate it, and pass
2174-
* that instead.
2175-
*
2176-
* # Example
2177-
*
2178-
* ~~~ {.rust}
2179-
* let s = str::as_c_str("PATH", { |path| libc::getenv(path) });
2180-
* ~~~
2168+
* A dummy trait to hold all the utility methods that we implement on strings.
21812169
*/
2182-
#[inline]
2183-
pub fn as_c_str<T>(s: &str, f: &fn(*libc::c_char) -> T) -> T {
2184-
do as_buf(s) |buf, len| {
2185-
// NB: len includes the trailing null.
2186-
assert!(len > 0);
2187-
if unsafe { *(ptr::offset(buf,len-1)) != 0 } {
2188-
as_c_str(to_owned(s), f)
2189-
} else {
2190-
f(buf as *libc::c_char)
2170+
pub trait StrUtil {
2171+
/**
2172+
* Work with the byte buffer of a string as a null-terminated C string.
2173+
*
2174+
* Allows for unsafe manipulation of strings, which is useful for foreign
2175+
* interop. This is similar to `str::as_buf`, but guarantees null-termination.
2176+
* If the given slice is not already null-terminated, this function will
2177+
* allocate a temporary, copy the slice, null terminate it, and pass
2178+
* that instead.
2179+
*
2180+
* # Example
2181+
*
2182+
* ~~~ {.rust}
2183+
* let s = "PATH".as_c_str(|path| libc::getenv(path));
2184+
* ~~~
2185+
*/
2186+
fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T;
2187+
}
2188+
2189+
impl<'self> StrUtil for &'self str {
2190+
#[inline]
2191+
fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T {
2192+
do as_buf(self) |buf, len| {
2193+
// NB: len includes the trailing null.
2194+
assert!(len > 0);
2195+
if unsafe { *(ptr::offset(buf,len-1)) != 0 } {
2196+
to_owned(self).as_c_str(f)
2197+
} else {
2198+
f(buf as *libc::c_char)
2199+
}
21912200
}
21922201
}
21932202
}
21942203

2204+
/**
2205+
* Deprecated. Use the `as_c_str` method on strings instead.
2206+
*/
2207+
#[inline(always)]
2208+
pub fn as_c_str<T>(s: &str, f: &fn(*libc::c_char) -> T) -> T {
2209+
s.as_c_str(f)
2210+
}
2211+
21952212
/**
21962213
* Work with the byte buffer and length of a slice.
21972214
*

trunk/src/libstd/vec.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> ~[T] {
149149
do as_mut_buf(v) |p, _len| {
150150
let mut i: uint = 0u;
151151
while i < n_elts {
152-
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)),
153-
op(i));
152+
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), op(i));
154153
i += 1u;
155154
}
156155
}
@@ -166,7 +165,20 @@ pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> ~[T] {
166165
* to the value `t`.
167166
*/
168167
pub fn from_elem<T:Copy>(n_elts: uint, t: T) -> ~[T] {
169-
from_fn(n_elts, |_i| copy t)
168+
// hack: manually inline from_fn for 2x plus speedup (sadly very important, from_elem is a
169+
// bottleneck in borrowck!)
170+
unsafe {
171+
let mut v = with_capacity(n_elts);
172+
do as_mut_buf(v) |p, _len| {
173+
let mut i = 0u;
174+
while i < n_elts {
175+
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), copy t);
176+
i += 1u;
177+
}
178+
}
179+
raw::set_len(&mut v, n_elts);
180+
v
181+
}
170182
}
171183

172184
/// Creates a new unique vector with the same contents as the slice

0 commit comments

Comments
 (0)