Skip to content

Commit 7683180

Browse files
committed
rustc: implement and use Default on more types.
1 parent da622a3 commit 7683180

File tree

16 files changed

+61
-73
lines changed

16 files changed

+61
-73
lines changed

src/Cargo.lock

+10-1
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,14 @@ dependencies = [
659659
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
660660
]
661661

662+
[[package]]
663+
name = "ena"
664+
version = "0.10.1"
665+
source = "registry+https://github.com/rust-lang/crates.io-index"
666+
dependencies = [
667+
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
668+
]
669+
662670
[[package]]
663671
name = "env_logger"
664672
version = "0.5.12"
@@ -2196,7 +2204,7 @@ name = "rustc_data_structures"
21962204
version = "0.0.0"
21972205
dependencies = [
21982206
"cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
2199-
"ena 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
2207+
"ena 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
22002208
"graphviz 0.0.0",
22012209
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
22022210
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3261,6 +3269,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
32613269
"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
32623270
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
32633271
"checksum elasticlunr-rs 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4837d77a1e157489a3933b743fd774ae75074e0e390b2b7f071530048a0d87ee"
3272+
"checksum ena 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "25b4e5febb25f08c49f1b07dc33a182729a6b21edfb562b5aef95f78e0dbe5bb"
32643273
"checksum ena 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "88dc8393b3c7352f94092497f6b52019643e493b6b890eb417cdb7c46117e621"
32653274
"checksum env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)" = "f4d7e69c283751083d53d01eac767407343b8b69c4bd70058e08adc2637cb257"
32663275
"checksum env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "afb070faf94c85d17d50ca44f6ad076bce18ae92f0037d350947240a36e9d42e"

src/librustc/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ impl DepGraph {
101101
DepGraph {
102102
data: Some(Lrc::new(DepGraphData {
103103
previous_work_products: prev_work_products,
104-
dep_node_debug: Lock::new(Default::default()),
104+
dep_node_debug: Default::default(),
105105
current: Lock::new(CurrentDepGraph::new()),
106106
previous: prev_graph,
107107
colors: Lock::new(DepNodeColorMap::new(prev_graph_node_count)),
108-
loaded_from_cache: Lock::new(Default::default()),
108+
loaded_from_cache: Default::default(),
109109
})),
110110
fingerprints: Lrc::new(Lock::new(fingerprints)),
111111
}

src/librustc/hir/map/definitions.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use util::nodemap::NodeMap;
3636
/// Internally the DefPathTable holds a tree of DefKeys, where each DefKey
3737
/// stores the DefIndex of its parent.
3838
/// There is one DefPathTable for each crate.
39+
#[derive(Default)]
3940
pub struct DefPathTable {
4041
index_to_key: [Vec<DefKey>; 2],
4142
def_path_hashes: [Vec<DefPathHash>; 2],
@@ -153,7 +154,7 @@ impl Decodable for DefPathTable {
153154
/// The definition table containing node definitions.
154155
/// It holds the DefPathTable for local DefIds/DefPaths and it also stores a
155156
/// mapping from NodeIds to local DefIds.
156-
#[derive(Clone)]
157+
#[derive(Clone, Default)]
157158
pub struct Definitions {
158159
table: DefPathTable,
159160
node_to_def_index: NodeMap<DefIndex>,
@@ -412,20 +413,8 @@ impl Definitions {
412413
/// ascending order.
413414
///
414415
/// FIXME: there is probably a better place to put this comment.
415-
pub fn new() -> Definitions {
416-
Definitions {
417-
table: DefPathTable {
418-
index_to_key: [vec![], vec![]],
419-
def_path_hashes: [vec![], vec![]],
420-
},
421-
node_to_def_index: Default::default(),
422-
def_index_to_node: [vec![], vec![]],
423-
node_to_hir_id: IndexVec::new(),
424-
parent_modules_of_macro_defs: Default::default(),
425-
expansions_that_defined: Default::default(),
426-
next_disambiguator: Default::default(),
427-
def_index_to_span: Default::default(),
428-
}
416+
pub fn new() -> Self {
417+
Self::default()
429418
}
430419

431420
pub fn def_path_table(&self) -> &DefPathTable {

src/librustc/ich/hcx.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for Span {
369369
// times, we cache a stable hash of it and hash that instead of
370370
// recursing every time.
371371
thread_local! {
372-
static CACHE: RefCell<FxHashMap<hygiene::Mark, u64>> =
373-
RefCell::new(Default::default());
372+
static CACHE: RefCell<FxHashMap<hygiene::Mark, u64>> = Default::default();
374373
}
375374

376375
let sub_hash: u64 = CACHE.with(|cache| {

src/librustc/infer/region_constraints/mod.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::{cmp, fmt, mem, u32};
2929

3030
mod taint;
3131

32+
#[derive(Default)]
3233
pub struct RegionConstraintCollector<'tcx> {
3334
/// For each `RegionVid`, the corresponding `RegionVariableOrigin`.
3435
var_infos: IndexVec<RegionVid, RegionVariableInfo>,
@@ -341,17 +342,8 @@ impl TaintDirections {
341342
}
342343

343344
impl<'tcx> RegionConstraintCollector<'tcx> {
344-
pub fn new() -> RegionConstraintCollector<'tcx> {
345-
RegionConstraintCollector {
346-
var_infos: VarInfos::default(),
347-
data: RegionConstraintData::default(),
348-
lubs: Default::default(),
349-
glbs: Default::default(),
350-
bound_count: 0,
351-
undo_log: Vec::new(),
352-
unification_table: ut::UnificationTable::new(),
353-
any_unifications: false,
354-
}
345+
pub fn new() -> Self {
346+
Self::default()
355347
}
356348

357349
pub fn num_region_vars(&self) -> usize {

src/librustc/lint/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ pub fn check_ast_crate(
12331233
let (passes, buffered) = if pre_expansion {
12341234
(
12351235
sess.lint_store.borrow_mut().pre_expansion_passes.take(),
1236-
LintBuffer::new(),
1236+
LintBuffer::default(),
12371237
)
12381238
} else {
12391239
(

src/librustc/lint/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,12 @@ mod levels;
490490

491491
pub use self::levels::{LintLevelSets, LintLevelMap};
492492

493+
#[derive(Default)]
493494
pub struct LintBuffer {
494495
map: NodeMap<Vec<BufferedEarlyLint>>,
495496
}
496497

497498
impl LintBuffer {
498-
pub fn new() -> LintBuffer {
499-
LintBuffer { map: ::std::default::Default::default() }
500-
}
501-
502499
pub fn add_lint(&mut self,
503500
lint: &'static Lint,
504501
id: ast::NodeId,

src/librustc/middle/borrowck.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
1818
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
1919
pub enum SignalledError { SawSomeError, NoErrorsSeen }
2020

21+
impl Default for SignalledError {
22+
fn default() -> SignalledError {
23+
SignalledError::NoErrorsSeen
24+
}
25+
}
26+
2127
impl_stable_hash_for!(enum self::SignalledError { SawSomeError, NoErrorsSeen });
2228

23-
#[derive(Debug, RustcEncodable, RustcDecodable)]
29+
#[derive(Debug, Default, RustcEncodable, RustcDecodable)]
2430
pub struct BorrowCheckResult {
2531
pub used_mut_nodes: FxHashSet<HirId>,
2632
pub signalled_any_error: SignalledError,

src/librustc/middle/resolve_lifetime.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -2741,9 +2741,7 @@ fn insert_late_bound_lifetimes(
27412741
constrained_by_input.visit_ty(arg_ty);
27422742
}
27432743

2744-
let mut appears_in_output = AllCollector {
2745-
regions: Default::default(),
2746-
};
2744+
let mut appears_in_output = AllCollector::default();
27472745
intravisit::walk_fn_ret_ty(&mut appears_in_output, &decl.output);
27482746

27492747
debug!(
@@ -2755,9 +2753,7 @@ fn insert_late_bound_lifetimes(
27552753
//
27562754
// Subtle point: because we disallow nested bindings, we can just
27572755
// ignore binders here and scrape up all names we see.
2758-
let mut appears_in_where_clause = AllCollector {
2759-
regions: Default::default(),
2760-
};
2756+
let mut appears_in_where_clause = AllCollector::default();
27612757
appears_in_where_clause.visit_generics(generics);
27622758

27632759
for param in &generics.params {
@@ -2854,6 +2850,7 @@ fn insert_late_bound_lifetimes(
28542850
}
28552851
}
28562852

2853+
#[derive(Default)]
28572854
struct AllCollector {
28582855
regions: FxHashSet<hir::LifetimeName>,
28592856
}

src/librustc/session/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ pub fn build_session_(
11491149
local_crate_source_file,
11501150
working_dir,
11511151
lint_store: RwLock::new(lint::LintStore::new()),
1152-
buffered_lints: Lock::new(Some(lint::LintBuffer::new())),
1152+
buffered_lints: Lock::new(Some(Default::default())),
11531153
one_time_diagnostics: Default::default(),
11541154
plugin_llvm_passes: OneThread::new(RefCell::new(Vec::new())),
11551155
plugin_attributes: OneThread::new(RefCell::new(Vec::new())),

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3183,7 +3183,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
31833183
/// rather, you should request the vector for a specific type via
31843184
/// `tcx.inherent_impls(def_id)` so as to minimize your dependencies
31853185
/// (constructing this map requires touching the entire crate).
3186-
#[derive(Clone, Debug)]
3186+
#[derive(Clone, Debug, Default)]
31873187
pub struct CrateInherentImpls {
31883188
pub inherent_impls: DefIdMap<Lrc<Vec<DefId>>>,
31893189
}

src/librustc_codegen_ssa/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn reachable_non_generics_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
6464
assert_eq!(cnum, LOCAL_CRATE);
6565

6666
if !tcx.sess.opts.output_types.should_codegen() {
67-
return Lrc::new(Default::default())
67+
return Default::default();
6868
}
6969

7070
// Check to see if this crate is a "special runtime crate". These

src/librustc_data_structures/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ path = "lib.rs"
99
crate-type = ["dylib"]
1010

1111
[dependencies]
12-
ena = "0.9.3"
12+
ena = "0.10.1"
1313
log = "0.4"
1414
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
1515
serialize = { path = "../libserialize" }

src/librustc_data_structures/snapshot_map/mod.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ pub struct SnapshotMap<K, V>
2323
undo_log: Vec<UndoLog<K, V>>,
2424
}
2525

26+
// HACK(eddyb) manual impl avoids `Default` bounds on `K` and `V`.
27+
impl<K, V> Default for SnapshotMap<K, V>
28+
where K: Hash + Clone + Eq
29+
{
30+
fn default() -> Self {
31+
SnapshotMap {
32+
map: Default::default(),
33+
undo_log: Default::default(),
34+
}
35+
}
36+
}
37+
2638
pub struct Snapshot {
2739
len: usize,
2840
}
@@ -35,17 +47,6 @@ enum UndoLog<K, V> {
3547
Noop,
3648
}
3749

38-
impl<K, V> Default for SnapshotMap<K, V>
39-
where K: Hash + Clone + Eq
40-
{
41-
fn default() -> Self {
42-
SnapshotMap {
43-
map: FxHashMap::default(),
44-
undo_log: vec![],
45-
}
46-
}
47-
}
48-
4950
impl<K, V> SnapshotMap<K, V>
5051
where K: Hash + Clone + Eq
5152
{

src/librustc_data_structures/transitive_relation.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ pub struct TransitiveRelation<T: Clone + Debug + Eq + Hash> {
4242
closure: Lock<Option<BitMatrix<usize, usize>>>,
4343
}
4444

45+
// HACK(eddyb) manual impl avoids `Default` bound on `T`.
46+
impl<T: Clone + Debug + Eq + Hash> Default for TransitiveRelation<T> {
47+
fn default() -> Self {
48+
TransitiveRelation {
49+
elements: Default::default(),
50+
map: Default::default(),
51+
edges: Default::default(),
52+
closure: Default::default(),
53+
}
54+
}
55+
}
56+
4557
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, Debug)]
4658
struct Index(usize);
4759

@@ -51,17 +63,6 @@ struct Edge {
5163
target: Index,
5264
}
5365

54-
impl<T: Clone + Debug + Eq + Hash> Default for TransitiveRelation<T> {
55-
fn default() -> TransitiveRelation<T> {
56-
TransitiveRelation {
57-
elements: vec![],
58-
map: FxHashMap::default(),
59-
edges: vec![],
60-
closure: Lock::new(None),
61-
}
62-
}
63-
}
64-
6566
impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
6667
pub fn is_empty(&self) -> bool {
6768
self.edges.is_empty()

src/librustc_typeck/coherence/inherent_impls.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
2222
use rustc::hir;
2323
use rustc::hir::itemlikevisit::ItemLikeVisitor;
2424
use rustc::ty::{self, CrateInherentImpls, TyCtxt};
25-
use rustc::util::nodemap::DefIdMap;
2625

2726
use rustc_data_structures::sync::Lrc;
2827
use syntax::ast;
@@ -37,9 +36,7 @@ pub fn crate_inherent_impls<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
3736
let krate = tcx.hir.krate();
3837
let mut collect = InherentCollect {
3938
tcx,
40-
impls_map: CrateInherentImpls {
41-
inherent_impls: DefIdMap::default()
42-
}
39+
impls_map: Default::default(),
4340
};
4441
krate.visit_all_item_likes(&mut collect);
4542
Lrc::new(collect.impls_map)

0 commit comments

Comments
 (0)