Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use the new Entry::or_default method where possible.
  • Loading branch information
eddyb committed Aug 18, 2018
1 parent d5b6b95 commit 14aed81
Show file tree
Hide file tree
Showing 36 changed files with 130 additions and 146 deletions.
4 changes: 2 additions & 2 deletions src/bootstrap/sanity.rs
Expand Up @@ -176,7 +176,7 @@ pub fn check(build: &mut Build) {
if target.contains("-none-") {
if build.no_std(*target).is_none() {
let target = build.config.target_config.entry(target.clone())
.or_insert(Default::default());
.or_default();

target.no_std = true;
}
Expand All @@ -192,7 +192,7 @@ pub fn check(build: &mut Build) {
// fall back to the system toolchain in /usr before giving up
if build.musl_root(*target).is_none() && build.config.build == *target {
let target = build.config.target_config.entry(target.clone())
.or_insert(Default::default());
.or_default();
target.musl_root = Some("/usr".into());
}
match build.musl_root(*target) {
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/tool.rs
Expand Up @@ -183,7 +183,7 @@ impl Step for ToolBuild {
let mut artifacts = builder.tool_artifacts.borrow_mut();
let prev_artifacts = artifacts
.entry(target)
.or_insert_with(Default::default);
.or_default();
if let Some(prev) = prev_artifacts.get(&*id) {
if prev.1 != val.1 {
duplicates.push((
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/lowering.rs
Expand Up @@ -2334,7 +2334,7 @@ impl<'a> LoweringContext<'a> {
// FIXME: This could probably be done with less rightward drift. Also looks like two control
// paths where report_error is called are also the only paths that advance to after
// the match statement, so the error reporting could probably just be moved there.
let mut add_bounds = NodeMap();
let mut add_bounds: NodeMap<Vec<_>> = NodeMap();
for pred in &generics.where_clause.predicates {
if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {
'next_bound: for bound in &bound_pred.bounds {
Expand Down Expand Up @@ -2364,7 +2364,7 @@ impl<'a> LoweringContext<'a> {
GenericParamKind::Type { .. } => {
if node_id == param.id {
add_bounds.entry(param.id)
.or_insert(Vec::new())
.or_default()
.push(bound.clone());
continue 'next_bound;
}
Expand Down Expand Up @@ -2730,7 +2730,7 @@ impl<'a> LoweringContext<'a> {

if let Some(ref trait_ref) = trait_ref {
if let Def::Trait(def_id) = trait_ref.path.def {
this.trait_impls.entry(def_id).or_insert(vec![]).push(id);
this.trait_impls.entry(def_id).or_default().push(id);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/mod.rs
Expand Up @@ -512,7 +512,7 @@ impl LintBuffer {
msg: msg.to_string(),
diagnostic
};
let arr = self.map.entry(id).or_insert(Vec::new());
let arr = self.map.entry(id).or_default();
if !arr.contains(&early_lint) {
arr.push(early_lint);
}
Expand Down
26 changes: 11 additions & 15 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -391,37 +391,33 @@ fn resolve_lifetimes<'tcx>(

let named_region_map = krate(tcx);

let mut defs = FxHashMap();
let mut rl = ResolveLifetimes {
defs: FxHashMap(),
late_bound: FxHashMap(),
object_lifetime_defaults: FxHashMap(),
};

for (k, v) in named_region_map.defs {
let hir_id = tcx.hir.node_to_hir_id(k);
let map = defs.entry(hir_id.owner_local_def_id())
.or_insert_with(|| Lrc::new(FxHashMap()));
let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default();
Lrc::get_mut(map).unwrap().insert(hir_id.local_id, v);
}
let mut late_bound = FxHashMap();
for k in named_region_map.late_bound {
let hir_id = tcx.hir.node_to_hir_id(k);
let map = late_bound
.entry(hir_id.owner_local_def_id())
.or_insert_with(|| Lrc::new(FxHashSet()));
let map = rl.late_bound.entry(hir_id.owner_local_def_id()).or_default();
Lrc::get_mut(map).unwrap().insert(hir_id.local_id);
}
let mut object_lifetime_defaults = FxHashMap();
for (k, v) in named_region_map.object_lifetime_defaults {
let hir_id = tcx.hir.node_to_hir_id(k);
let map = object_lifetime_defaults
let map = rl.object_lifetime_defaults
.entry(hir_id.owner_local_def_id())
.or_insert_with(|| Lrc::new(FxHashMap()));
.or_default();
Lrc::get_mut(map)
.unwrap()
.insert(hir_id.local_id, Lrc::new(v));
}

Lrc::new(ResolveLifetimes {
defs,
late_bound,
object_lifetime_defaults,
})
Lrc::new(rl)
}

fn krate<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> NamedRegionMap {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/session/config.rs
Expand Up @@ -2174,7 +2174,7 @@ pub fn build_session_options_and_crate_config(
);
}

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

externs
.entry(name.to_string())
.or_insert_with(BTreeSet::new)
.or_default()
.insert(location.to_string());
}

Expand Down
12 changes: 6 additions & 6 deletions src/librustc/traits/auto_trait.rs
Expand Up @@ -513,26 +513,26 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
{
let deps1 = vid_map
.entry(RegionTarget::RegionVid(r1))
.or_insert_with(|| Default::default());
.or_default();
deps1.larger.insert(RegionTarget::RegionVid(r2));
}

let deps2 = vid_map
.entry(RegionTarget::RegionVid(r2))
.or_insert_with(|| Default::default());
.or_default();
deps2.smaller.insert(RegionTarget::RegionVid(r1));
}
&Constraint::RegSubVar(region, vid) => {
{
let deps1 = vid_map
.entry(RegionTarget::Region(region))
.or_insert_with(|| Default::default());
.or_default();
deps1.larger.insert(RegionTarget::RegionVid(vid));
}

let deps2 = vid_map
.entry(RegionTarget::RegionVid(vid))
.or_insert_with(|| Default::default());
.or_default();
deps2.smaller.insert(RegionTarget::Region(region));
}
&Constraint::VarSubReg(vid, region) => {
Expand All @@ -542,13 +542,13 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
{
let deps1 = vid_map
.entry(RegionTarget::Region(r1))
.or_insert_with(|| Default::default());
.or_default();
deps1.larger.insert(RegionTarget::Region(r2));
}

let deps2 = vid_map
.entry(RegionTarget::Region(r2))
.or_insert_with(|| Default::default());
.or_default();
deps2.smaller.insert(RegionTarget::Region(r1));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/traits/error_reporting.rs
Expand Up @@ -57,7 +57,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
index: Option<usize>, // None if this is an old error
}

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

for (index, error) in errors.iter().enumerate() {
error_map.entry(error.obligation.cause.span).or_insert(Vec::new()).push(
error_map.entry(error.obligation.cause.span).or_default().push(
ErrorDescriptor {
predicate: error.obligation.predicate.clone(),
index: Some(index)
});

self.reported_trait_errors.borrow_mut()
.entry(error.obligation.cause.span).or_insert(Vec::new())
.entry(error.obligation.cause.span).or_default()
.push(error.obligation.predicate.clone());
}

Expand Down
24 changes: 8 additions & 16 deletions src/librustc/traits/specialize/specialization_graph.rs
Expand Up @@ -49,7 +49,7 @@ pub struct Graph {

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

impl<'a, 'gcx, 'tcx> Children {
fn new() -> Children {
Children {
nonblanket_impls: FxHashMap(),
blanket_impls: vec![],
}
}

/// Insert an impl into this set of children without comparing to any existing impls
fn insert_blindly(&mut self,
tcx: TyCtxt<'a, 'gcx, 'tcx>,
impl_def_id: DefId) {
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
if let Some(sty) = fast_reject::simplify_type(tcx, trait_ref.self_ty(), false) {
debug!("insert_blindly: impl_def_id={:?} sty={:?}", impl_def_id, sty);
self.nonblanket_impls.entry(sty).or_insert(vec![]).push(impl_def_id)
self.nonblanket_impls.entry(sty).or_default().push(impl_def_id)
} else {
debug!("insert_blindly: impl_def_id={:?} sty=None", impl_def_id);
self.blanket_impls.push(impl_def_id)
Expand Down Expand Up @@ -230,7 +223,7 @@ impl<'a, 'gcx, 'tcx> Children {
}

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

self.parent.insert(impl_def_id, trait_def_id);
self.children.entry(trait_def_id).or_insert(Children::new())
self.children.entry(trait_def_id).or_default()
.insert_blindly(tcx, impl_def_id);
return Ok(None);
}
Expand All @@ -281,7 +274,7 @@ impl<'a, 'gcx, 'tcx> Graph {
loop {
use self::Inserted::*;

let insert_result = self.children.entry(parent).or_insert(Children::new())
let insert_result = self.children.entry(parent).or_default()
.insert(tcx, impl_def_id, simplified)?;

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

// Add G as N's child.
let mut grand_children = Children::new();
grand_children.insert_blindly(tcx, grand_child_to_be);
self.children.insert(impl_def_id, grand_children);
self.children.entry(impl_def_id).or_default()
.insert_blindly(tcx, grand_child_to_be);
break;
}
ShouldRecurseOn(new_parent) => {
Expand All @@ -343,7 +335,7 @@ impl<'a, 'gcx, 'tcx> Graph {
was already present.");
}

self.children.entry(parent).or_insert(Children::new()).insert_blindly(tcx, child);
self.children.entry(parent).or_default().insert_blindly(tcx, child);
}

/// The parent of a given impl, which is the def id of the trait when the
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/ty/context.rs
Expand Up @@ -1132,11 +1132,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
None
};

let mut trait_map = FxHashMap();
let mut trait_map: FxHashMap<_, Lrc<FxHashMap<_, _>>> = FxHashMap();
for (k, v) in resolutions.trait_map {
let hir_id = hir.node_to_hir_id(k);
let map = trait_map.entry(hir_id.owner)
.or_insert_with(|| Lrc::new(FxHashMap()));
let map = trait_map.entry(hir_id.owner).or_default();
Lrc::get_mut(map).unwrap()
.insert(hir_id.local_id,
Lrc::new(StableVec::new(v)));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/inhabitedness/mod.rs
Expand Up @@ -228,7 +228,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
match self.sty {
TyAdt(def, substs) => {
{
let substs_set = visited.entry(def.did).or_insert(FxHashSet::default());
let substs_set = visited.entry(def.did).or_default();
if !substs_set.insert(substs) {
// We are already calculating the inhabitedness of this type.
// The type must contain a reference to itself. Break the
Expand Down
65 changes: 31 additions & 34 deletions src/librustc/ty/trait_def.rs
Expand Up @@ -41,6 +41,7 @@ pub struct TraitDef {
pub def_path_hash: DefPathHash,
}

#[derive(Default)]
pub struct TraitImpls {
blanket_impls: Vec<DefId>,
/// Impls indexed by their simplified self-type, for fast lookup.
Expand Down Expand Up @@ -143,47 +144,43 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub(super) fn trait_impls_of_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
trait_id: DefId)
-> Lrc<TraitImpls> {
let mut remote_impls = Vec::new();

// Traits defined in the current crate can't have impls in upstream
// crates, so we don't bother querying the cstore.
if !trait_id.is_local() {
for &cnum in tcx.crates().iter() {
let impls = tcx.implementations_of_trait((cnum, trait_id));
remote_impls.extend(impls.iter().cloned());
}
}

let mut blanket_impls = Vec::new();
let mut non_blanket_impls = FxHashMap();
let mut impls = TraitImpls::default();

let local_impls = tcx.hir
.trait_impls(trait_id)
.into_iter()
.map(|&node_id| tcx.hir.local_def_id(node_id));
{
let mut add_impl = |impl_def_id| {
let impl_self_ty = tcx.type_of(impl_def_id);
if impl_def_id.is_local() && impl_self_ty.references_error() {
return;
}

for impl_def_id in local_impls.chain(remote_impls.into_iter()) {
let impl_self_ty = tcx.type_of(impl_def_id);
if impl_def_id.is_local() && impl_self_ty.references_error() {
continue
if let Some(simplified_self_ty) =
fast_reject::simplify_type(tcx, impl_self_ty, false)
{
impls.non_blanket_impls
.entry(simplified_self_ty)
.or_default()
.push(impl_def_id);
} else {
impls.blanket_impls.push(impl_def_id);
}
};

// Traits defined in the current crate can't have impls in upstream
// crates, so we don't bother querying the cstore.
if !trait_id.is_local() {
for &cnum in tcx.crates().iter() {
for &def_id in tcx.implementations_of_trait((cnum, trait_id)).iter() {
add_impl(def_id);
}
}
}

if let Some(simplified_self_ty) =
fast_reject::simplify_type(tcx, impl_self_ty, false)
{
non_blanket_impls
.entry(simplified_self_ty)
.or_insert(vec![])
.push(impl_def_id);
} else {
blanket_impls.push(impl_def_id);
for &node_id in tcx.hir.trait_impls(trait_id) {
add_impl(tcx.hir.local_def_id(node_id));
}
}

Lrc::new(TraitImpls {
blanket_impls: blanket_impls,
non_blanket_impls: non_blanket_impls,
})
Lrc::new(impls)
}

impl<'a> HashStable<StableHashingContext<'a>> for TraitImpls {
Expand Down

0 comments on commit 14aed81

Please sign in to comment.