Skip to content

Commit 2bf053c

Browse files
committed
auto merge of #6851 : alexcrichton/rust/bugfixes, r=pcwalton
Closes #5090 by using the excellent new generic deriving code Promotes the unreachable code attribute to a lint attribute (instead of always being a warning) Fixes some edge cases when creating hashmaps/hashsets and also when consuming them. (fixes #5998)
2 parents c23843c + e694e5f commit 2bf053c

File tree

13 files changed

+365
-981
lines changed

13 files changed

+365
-981
lines changed

src/librustc/middle/lint.rs

+8
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
/*

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

+5-2
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) {

src/libstd/hashmap.rs

+19-3
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);

0 commit comments

Comments
 (0)