Skip to content

Commit a9fe312

Browse files
committed
Auto merge of #52592 - eddyb:or-default, r=Mark-Simulacrum
Use the new Entry::or_default method where possible.
2 parents 33b923f + 14aed81 commit a9fe312

File tree

36 files changed

+130
-146
lines changed

36 files changed

+130
-146
lines changed

src/bootstrap/sanity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub fn check(build: &mut Build) {
176176
if target.contains("-none-") {
177177
if build.no_std(*target).is_none() {
178178
let target = build.config.target_config.entry(target.clone())
179-
.or_insert(Default::default());
179+
.or_default();
180180

181181
target.no_std = true;
182182
}
@@ -192,7 +192,7 @@ pub fn check(build: &mut Build) {
192192
// fall back to the system toolchain in /usr before giving up
193193
if build.musl_root(*target).is_none() && build.config.build == *target {
194194
let target = build.config.target_config.entry(target.clone())
195-
.or_insert(Default::default());
195+
.or_default();
196196
target.musl_root = Some("/usr".into());
197197
}
198198
match build.musl_root(*target) {

src/bootstrap/tool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Step for ToolBuild {
183183
let mut artifacts = builder.tool_artifacts.borrow_mut();
184184
let prev_artifacts = artifacts
185185
.entry(target)
186-
.or_insert_with(Default::default);
186+
.or_default();
187187
if let Some(prev) = prev_artifacts.get(&*id) {
188188
if prev.1 != val.1 {
189189
duplicates.push((

src/librustc/hir/lowering.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,7 @@ impl<'a> LoweringContext<'a> {
23342334
// FIXME: This could probably be done with less rightward drift. Also looks like two control
23352335
// paths where report_error is called are also the only paths that advance to after
23362336
// the match statement, so the error reporting could probably just be moved there.
2337-
let mut add_bounds = NodeMap();
2337+
let mut add_bounds: NodeMap<Vec<_>> = NodeMap();
23382338
for pred in &generics.where_clause.predicates {
23392339
if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {
23402340
'next_bound: for bound in &bound_pred.bounds {
@@ -2364,7 +2364,7 @@ impl<'a> LoweringContext<'a> {
23642364
GenericParamKind::Type { .. } => {
23652365
if node_id == param.id {
23662366
add_bounds.entry(param.id)
2367-
.or_insert(Vec::new())
2367+
.or_default()
23682368
.push(bound.clone());
23692369
continue 'next_bound;
23702370
}
@@ -2730,7 +2730,7 @@ impl<'a> LoweringContext<'a> {
27302730

27312731
if let Some(ref trait_ref) = trait_ref {
27322732
if let Def::Trait(def_id) = trait_ref.path.def {
2733-
this.trait_impls.entry(def_id).or_insert(vec![]).push(id);
2733+
this.trait_impls.entry(def_id).or_default().push(id);
27342734
}
27352735
}
27362736

src/librustc/lint/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl LintBuffer {
512512
msg: msg.to_string(),
513513
diagnostic
514514
};
515-
let arr = self.map.entry(id).or_insert(Vec::new());
515+
let arr = self.map.entry(id).or_default();
516516
if !arr.contains(&early_lint) {
517517
arr.push(early_lint);
518518
}

src/librustc/middle/resolve_lifetime.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -391,37 +391,33 @@ fn resolve_lifetimes<'tcx>(
391391

392392
let named_region_map = krate(tcx);
393393

394-
let mut defs = FxHashMap();
394+
let mut rl = ResolveLifetimes {
395+
defs: FxHashMap(),
396+
late_bound: FxHashMap(),
397+
object_lifetime_defaults: FxHashMap(),
398+
};
399+
395400
for (k, v) in named_region_map.defs {
396401
let hir_id = tcx.hir.node_to_hir_id(k);
397-
let map = defs.entry(hir_id.owner_local_def_id())
398-
.or_insert_with(|| Lrc::new(FxHashMap()));
402+
let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default();
399403
Lrc::get_mut(map).unwrap().insert(hir_id.local_id, v);
400404
}
401-
let mut late_bound = FxHashMap();
402405
for k in named_region_map.late_bound {
403406
let hir_id = tcx.hir.node_to_hir_id(k);
404-
let map = late_bound
405-
.entry(hir_id.owner_local_def_id())
406-
.or_insert_with(|| Lrc::new(FxHashSet()));
407+
let map = rl.late_bound.entry(hir_id.owner_local_def_id()).or_default();
407408
Lrc::get_mut(map).unwrap().insert(hir_id.local_id);
408409
}
409-
let mut object_lifetime_defaults = FxHashMap();
410410
for (k, v) in named_region_map.object_lifetime_defaults {
411411
let hir_id = tcx.hir.node_to_hir_id(k);
412-
let map = object_lifetime_defaults
412+
let map = rl.object_lifetime_defaults
413413
.entry(hir_id.owner_local_def_id())
414-
.or_insert_with(|| Lrc::new(FxHashMap()));
414+
.or_default();
415415
Lrc::get_mut(map)
416416
.unwrap()
417417
.insert(hir_id.local_id, Lrc::new(v));
418418
}
419419

420-
Lrc::new(ResolveLifetimes {
421-
defs,
422-
late_bound,
423-
object_lifetime_defaults,
424-
})
420+
Lrc::new(rl)
425421
}
426422

427423
fn krate<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> NamedRegionMap {

src/librustc/session/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2174,7 +2174,7 @@ pub fn build_session_options_and_crate_config(
21742174
);
21752175
}
21762176

2177-
let mut externs = BTreeMap::new();
2177+
let mut externs: BTreeMap<_, BTreeSet<_>> = BTreeMap::new();
21782178
for arg in &matches.opt_strs("extern") {
21792179
let mut parts = arg.splitn(2, '=');
21802180
let name = match parts.next() {
@@ -2191,7 +2191,7 @@ pub fn build_session_options_and_crate_config(
21912191

21922192
externs
21932193
.entry(name.to_string())
2194-
.or_insert_with(BTreeSet::new)
2194+
.or_default()
21952195
.insert(location.to_string());
21962196
}
21972197

src/librustc/traits/auto_trait.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -513,26 +513,26 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
513513
{
514514
let deps1 = vid_map
515515
.entry(RegionTarget::RegionVid(r1))
516-
.or_insert_with(|| Default::default());
516+
.or_default();
517517
deps1.larger.insert(RegionTarget::RegionVid(r2));
518518
}
519519

520520
let deps2 = vid_map
521521
.entry(RegionTarget::RegionVid(r2))
522-
.or_insert_with(|| Default::default());
522+
.or_default();
523523
deps2.smaller.insert(RegionTarget::RegionVid(r1));
524524
}
525525
&Constraint::RegSubVar(region, vid) => {
526526
{
527527
let deps1 = vid_map
528528
.entry(RegionTarget::Region(region))
529-
.or_insert_with(|| Default::default());
529+
.or_default();
530530
deps1.larger.insert(RegionTarget::RegionVid(vid));
531531
}
532532

533533
let deps2 = vid_map
534534
.entry(RegionTarget::RegionVid(vid))
535-
.or_insert_with(|| Default::default());
535+
.or_default();
536536
deps2.smaller.insert(RegionTarget::Region(region));
537537
}
538538
&Constraint::VarSubReg(vid, region) => {
@@ -542,13 +542,13 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
542542
{
543543
let deps1 = vid_map
544544
.entry(RegionTarget::Region(r1))
545-
.or_insert_with(|| Default::default());
545+
.or_default();
546546
deps1.larger.insert(RegionTarget::Region(r2));
547547
}
548548

549549
let deps2 = vid_map
550550
.entry(RegionTarget::Region(r2))
551-
.or_insert_with(|| Default::default());
551+
.or_default();
552552
deps2.smaller.insert(RegionTarget::Region(r1));
553553
}
554554
}

src/librustc/traits/error_reporting.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
5757
index: Option<usize>, // None if this is an old error
5858
}
5959

60-
let mut error_map : FxHashMap<_, _> =
60+
let mut error_map : FxHashMap<_, Vec<_>> =
6161
self.reported_trait_errors.borrow().iter().map(|(&span, predicates)| {
6262
(span, predicates.iter().map(|predicate| ErrorDescriptor {
6363
predicate: predicate.clone(),
@@ -66,14 +66,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
6666
}).collect();
6767

6868
for (index, error) in errors.iter().enumerate() {
69-
error_map.entry(error.obligation.cause.span).or_insert(Vec::new()).push(
69+
error_map.entry(error.obligation.cause.span).or_default().push(
7070
ErrorDescriptor {
7171
predicate: error.obligation.predicate.clone(),
7272
index: Some(index)
7373
});
7474

7575
self.reported_trait_errors.borrow_mut()
76-
.entry(error.obligation.cause.span).or_insert(Vec::new())
76+
.entry(error.obligation.cause.span).or_default()
7777
.push(error.obligation.predicate.clone());
7878
}
7979

src/librustc/traits/specialize/specialization_graph.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub struct Graph {
4949

5050
/// Children of a given impl, grouped into blanket/non-blanket varieties as is
5151
/// done in `TraitDef`.
52-
#[derive(RustcEncodable, RustcDecodable)]
52+
#[derive(Default, RustcEncodable, RustcDecodable)]
5353
struct Children {
5454
// Impls of a trait (or specializations of a given impl). To allow for
5555
// quicker lookup, the impls are indexed by a simplified version of their
@@ -81,21 +81,14 @@ enum Inserted {
8181
}
8282

8383
impl<'a, 'gcx, 'tcx> Children {
84-
fn new() -> Children {
85-
Children {
86-
nonblanket_impls: FxHashMap(),
87-
blanket_impls: vec![],
88-
}
89-
}
90-
9184
/// Insert an impl into this set of children without comparing to any existing impls
9285
fn insert_blindly(&mut self,
9386
tcx: TyCtxt<'a, 'gcx, 'tcx>,
9487
impl_def_id: DefId) {
9588
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
9689
if let Some(sty) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false) {
9790
debug!("insert_blindly: impl_def_id={:?} sty={:?}", impl_def_id, sty);
98-
self.nonblanket_impls.entry(sty).or_insert(vec![]).push(impl_def_id)
91+
self.nonblanket_impls.entry(sty).or_default().push(impl_def_id)
9992
} else {
10093
debug!("insert_blindly: impl_def_id={:?} sty=None", impl_def_id);
10194
self.blanket_impls.push(impl_def_id)
@@ -230,7 +223,7 @@ impl<'a, 'gcx, 'tcx> Children {
230223
}
231224

232225
fn filtered(&mut self, sty: SimplifiedType) -> Box<dyn Iterator<Item = DefId> + '_> {
233-
let nonblanket = self.nonblanket_impls.entry(sty).or_insert(vec![]).iter();
226+
let nonblanket = self.nonblanket_impls.entry(sty).or_default().iter();
234227
Box::new(self.blanket_impls.iter().chain(nonblanket).cloned())
235228
}
236229
}
@@ -268,7 +261,7 @@ impl<'a, 'gcx, 'tcx> Graph {
268261
trait_ref, impl_def_id, trait_def_id);
269262

270263
self.parent.insert(impl_def_id, trait_def_id);
271-
self.children.entry(trait_def_id).or_insert(Children::new())
264+
self.children.entry(trait_def_id).or_default()
272265
.insert_blindly(tcx, impl_def_id);
273266
return Ok(None);
274267
}
@@ -281,7 +274,7 @@ impl<'a, 'gcx, 'tcx> Graph {
281274
loop {
282275
use self::Inserted::*;
283276

284-
let insert_result = self.children.entry(parent).or_insert(Children::new())
277+
let insert_result = self.children.entry(parent).or_default()
285278
.insert(tcx, impl_def_id, simplified)?;
286279

287280
match insert_result {
@@ -318,9 +311,8 @@ impl<'a, 'gcx, 'tcx> Graph {
318311
self.parent.insert(impl_def_id, parent);
319312

320313
// Add G as N's child.
321-
let mut grand_children = Children::new();
322-
grand_children.insert_blindly(tcx, grand_child_to_be);
323-
self.children.insert(impl_def_id, grand_children);
314+
self.children.entry(impl_def_id).or_default()
315+
.insert_blindly(tcx, grand_child_to_be);
324316
break;
325317
}
326318
ShouldRecurseOn(new_parent) => {
@@ -343,7 +335,7 @@ impl<'a, 'gcx, 'tcx> Graph {
343335
was already present.");
344336
}
345337

346-
self.children.entry(parent).or_insert(Children::new()).insert_blindly(tcx, child);
338+
self.children.entry(parent).or_default().insert_blindly(tcx, child);
347339
}
348340

349341
/// The parent of a given impl, which is the def id of the trait when the

src/librustc/ty/context.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1132,11 +1132,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11321132
None
11331133
};
11341134

1135-
let mut trait_map = FxHashMap();
1135+
let mut trait_map: FxHashMap<_, Lrc<FxHashMap<_, _>>> = FxHashMap();
11361136
for (k, v) in resolutions.trait_map {
11371137
let hir_id = hir.node_to_hir_id(k);
1138-
let map = trait_map.entry(hir_id.owner)
1139-
.or_insert_with(|| Lrc::new(FxHashMap()));
1138+
let map = trait_map.entry(hir_id.owner).or_default();
11401139
Lrc::get_mut(map).unwrap()
11411140
.insert(hir_id.local_id,
11421141
Lrc::new(StableVec::new(v)));

src/librustc/ty/inhabitedness/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
228228
match self.sty {
229229
TyAdt(def, substs) => {
230230
{
231-
let substs_set = visited.entry(def.did).or_insert(FxHashSet::default());
231+
let substs_set = visited.entry(def.did).or_default();
232232
if !substs_set.insert(substs) {
233233
// We are already calculating the inhabitedness of this type.
234234
// The type must contain a reference to itself. Break the

src/librustc/ty/trait_def.rs

+31-34
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct TraitDef {
4141
pub def_path_hash: DefPathHash,
4242
}
4343

44+
#[derive(Default)]
4445
pub struct TraitImpls {
4546
blanket_impls: Vec<DefId>,
4647
/// Impls indexed by their simplified self-type, for fast lookup.
@@ -143,47 +144,43 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
143144
pub(super) fn trait_impls_of_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
144145
trait_id: DefId)
145146
-> Lrc<TraitImpls> {
146-
let mut remote_impls = Vec::new();
147-
148-
// Traits defined in the current crate can't have impls in upstream
149-
// crates, so we don't bother querying the cstore.
150-
if !trait_id.is_local() {
151-
for &cnum in tcx.crates().iter() {
152-
let impls = tcx.implementations_of_trait((cnum, trait_id));
153-
remote_impls.extend(impls.iter().cloned());
154-
}
155-
}
156-
157-
let mut blanket_impls = Vec::new();
158-
let mut non_blanket_impls = FxHashMap();
147+
let mut impls = TraitImpls::default();
159148

160-
let local_impls = tcx.hir
161-
.trait_impls(trait_id)
162-
.into_iter()
163-
.map(|&node_id| tcx.hir.local_def_id(node_id));
149+
{
150+
let mut add_impl = |impl_def_id| {
151+
let impl_self_ty = tcx.type_of(impl_def_id);
152+
if impl_def_id.is_local() && impl_self_ty.references_error() {
153+
return;
154+
}
164155

165-
for impl_def_id in local_impls.chain(remote_impls.into_iter()) {
166-
let impl_self_ty = tcx.type_of(impl_def_id);
167-
if impl_def_id.is_local() && impl_self_ty.references_error() {
168-
continue
156+
if let Some(simplified_self_ty) =
157+
fast_reject::simplify_type(tcx, impl_self_ty, false)
158+
{
159+
impls.non_blanket_impls
160+
.entry(simplified_self_ty)
161+
.or_default()
162+
.push(impl_def_id);
163+
} else {
164+
impls.blanket_impls.push(impl_def_id);
165+
}
166+
};
167+
168+
// Traits defined in the current crate can't have impls in upstream
169+
// crates, so we don't bother querying the cstore.
170+
if !trait_id.is_local() {
171+
for &cnum in tcx.crates().iter() {
172+
for &def_id in tcx.implementations_of_trait((cnum, trait_id)).iter() {
173+
add_impl(def_id);
174+
}
175+
}
169176
}
170177

171-
if let Some(simplified_self_ty) =
172-
fast_reject::simplify_type(tcx, impl_self_ty, false)
173-
{
174-
non_blanket_impls
175-
.entry(simplified_self_ty)
176-
.or_insert(vec![])
177-
.push(impl_def_id);
178-
} else {
179-
blanket_impls.push(impl_def_id);
178+
for &node_id in tcx.hir.trait_impls(trait_id) {
179+
add_impl(tcx.hir.local_def_id(node_id));
180180
}
181181
}
182182

183-
Lrc::new(TraitImpls {
184-
blanket_impls: blanket_impls,
185-
non_blanket_impls: non_blanket_impls,
186-
})
183+
Lrc::new(impls)
187184
}
188185

189186
impl<'a> HashStable<StableHashingContext<'a>> for TraitImpls {

0 commit comments

Comments
 (0)