From: Lukas Wirth Date: Wed, 23 Feb 2022 14:55:06 +0000 (+0100) Subject: Simplify X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;ds=sidebyside;h=ffeec9dec9a70fc2b7342b20eabe5b5da23f3f00;p=rust.git Simplify --- diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 41e4bf8e8bf..9176c90ae9c 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -1701,8 +1701,9 @@ fn collect_module(&mut self, module: &Mod, attrs: &Attrs) { { Ok((file_id, is_mod_rs, mod_dir)) => { let item_tree = db.file_item_tree(file_id.into()); + let krate = self.def_collector.def_map.krate; let is_enabled = item_tree - .top_level_attrs(db, self.def_collector.def_map.krate) + .top_level_attrs(db, krate) .cfg() .map_or(true, |cfg| self.is_cfg_enabled(&cfg)); if is_enabled { @@ -1713,7 +1714,7 @@ fn collect_module(&mut self, module: &Mod, attrs: &Attrs) { &self.item_tree[module.visibility], ); ModCollector { - def_collector: &mut *self.def_collector, + def_collector: self.def_collector, macro_depth: self.macro_depth, module_id, tree_id: TreeId::new(file_id.into(), None), @@ -1723,7 +1724,7 @@ fn collect_module(&mut self, module: &Mod, attrs: &Attrs) { .collect_in_top_module(item_tree.top_level_items()); let is_macro_use = is_macro_use || item_tree - .top_level_attrs(db, self.def_collector.def_map.krate) + .top_level_attrs(db, krate) .by_key("macro_use") .exists(); if is_macro_use { @@ -1748,12 +1749,11 @@ fn push_child_module( definition: Option<(FileId, bool)>, visibility: &crate::visibility::RawVisibility, ) -> LocalModuleId { - let vis = self - .def_collector - .def_map + let def_map = &mut self.def_collector.def_map; + let vis = def_map .resolve_visibility(self.def_collector.db, self.module_id, visibility) .unwrap_or(Visibility::Public); - let modules = &mut self.def_collector.def_map.modules; + let modules = &mut def_map.modules; let origin = match definition { None => ModuleOrigin::Inline { definition: declaration }, Some((definition, is_mod_rs)) => { @@ -1768,10 +1768,10 @@ fn push_child_module( } modules[self.module_id].children.insert(name.clone(), res); - let module = self.def_collector.def_map.module_id(res); + let module = def_map.module_id(res); let def = ModuleDefId::from(module); - self.def_collector.def_map.modules[self.module_id].scope.declare(def); + def_map.modules[self.module_id].scope.declare(def); self.def_collector.update( self.module_id, &[(Some(name), PerNs::from_def(def, vis, false))], diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs index 80009010c52..f76034a3e22 100644 --- a/crates/hir_def/src/visibility.rs +++ b/crates/hir_def/src/visibility.rs @@ -1,6 +1,6 @@ //! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`). -use std::sync::Arc; +use std::{iter, sync::Arc}; use hir_expand::{hygiene::Hygiene, InFile}; use la_arena::ArenaMap; @@ -25,7 +25,7 @@ pub enum RawVisibility { } impl RawVisibility { - pub(crate) fn private() -> RawVisibility { + pub(crate) const fn private() -> RawVisibility { RawVisibility::Module(ModPath::from_kind(PathKind::Super(0))) } @@ -113,10 +113,7 @@ pub fn is_visible_from(self, db: &dyn DefDatabase, from_module: ModuleId) -> boo } pub(crate) fn is_visible_from_other_crate(self) -> bool { - match self { - Visibility::Module(_) => false, - Visibility::Public => true, - } + matches!(self, Visibility::Public) } pub(crate) fn is_visible_from_def_map( @@ -145,10 +142,7 @@ pub(crate) fn is_visible_from_def_map( arc = to_module.def_map(db); &arc }; - let is_block_root = match to_module.block { - Some(_) => to_module_def_map[to_module.local_id].parent.is_none(), - None => false, - }; + let is_block_root = matches!(to_module.block, Some(_) if to_module_def_map[to_module.local_id].parent.is_none()); if is_block_root { to_module = to_module_def_map.containing_module(to_module.local_id).unwrap(); } @@ -161,9 +155,7 @@ pub(crate) fn is_visible_from_def_map( return true; } match def_map[from_module].parent { - Some(parent) => { - from_module = parent; - } + Some(parent) => from_module = parent, None => { match def_map.parent() { Some(module) => { @@ -171,10 +163,8 @@ pub(crate) fn is_visible_from_def_map( def_map = &*parent_arc; from_module = module.local_id; } - None => { - // Reached the root module, nothing left to check. - return false; - } + // Reached the root module, nothing left to check. + None => return false, } } } @@ -194,12 +184,12 @@ pub(crate) fn max(self, other: Visibility, def_map: &DefMap) -> Option