Skip to content

Commit f0da940

Browse files
bors[bot]Veykril
andauthored
Merge #10803
10803: minor: Simplify r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents cfa26c3 + 966cae3 commit f0da940

File tree

3 files changed

+92
-92
lines changed

3 files changed

+92
-92
lines changed

crates/hir_def/src/attr.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -723,34 +723,32 @@ impl Attr {
723723
/// to derive macros.
724724
///
725725
/// Returns `None` when the attribute does not have a well-formed `#[derive]` attribute input.
726-
pub(crate) fn parse_derive(&self) -> Option<impl Iterator<Item = ModPath>> {
727-
if let Some(AttrInput::TokenTree(args, _)) = self.input.as_deref() {
728-
if args.delimiter_kind() != Some(DelimiterKind::Parenthesis) {
729-
return None;
730-
}
731-
let mut counter = 0;
732-
let paths = args
733-
.token_trees
734-
.iter()
735-
.group_by(move |tt| {
736-
if let tt::TokenTree::Leaf(tt::Leaf::Punct(Punct { char: ',', .. })) = tt {
737-
counter += 1;
738-
}
739-
counter
740-
})
741-
.into_iter()
742-
.map(|(_, tts)| {
743-
let segments = tts.filter_map(|tt| match tt {
744-
tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => Some(id.as_name()),
745-
_ => None,
746-
});
747-
ModPath::from_segments(PathKind::Plain, segments)
748-
})
749-
.collect::<Vec<_>>();
750-
751-
return Some(paths.into_iter());
726+
pub(crate) fn parse_derive(&self) -> Option<impl Iterator<Item = ModPath> + '_> {
727+
let args = match self.input.as_deref() {
728+
Some(AttrInput::TokenTree(args, _)) => args,
729+
_ => return None,
730+
};
731+
732+
if args.delimiter_kind() != Some(DelimiterKind::Parenthesis) {
733+
return None;
752734
}
753-
None
735+
let paths = args
736+
.token_trees
737+
.iter()
738+
.group_by(|tt| {
739+
matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Punct(Punct { char: ',', .. })))
740+
})
741+
.into_iter()
742+
.map(|(_, tts)| {
743+
let segments = tts.filter_map(|tt| match tt {
744+
tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => Some(id.as_name()),
745+
_ => None,
746+
});
747+
ModPath::from_segments(PathKind::Plain, segments)
748+
})
749+
.collect::<Vec<_>>();
750+
751+
return Some(paths.into_iter());
754752
}
755753

756754
pub fn string_value(&self) -> Option<&SmolStr> {

crates/hir_def/src/nameres/collector.rs

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,6 @@ enum MacroDirectiveKind {
226226
Attr { ast_id: AstIdWithPath<ast::Item>, attr: Attr, mod_item: ModItem },
227227
}
228228

229-
struct DefData<'a> {
230-
id: ModuleDefId,
231-
name: &'a Name,
232-
visibility: &'a RawVisibility,
233-
has_constructor: bool,
234-
}
235-
236229
/// Walks the tree of module recursively
237230
struct DefCollector<'a> {
238231
db: &'a dyn DefDatabase,
@@ -1353,19 +1346,18 @@ impl DefCollector<'_> {
13531346
}
13541347

13551348
for directive in &self.unresolved_imports {
1356-
if let ImportSource::Import { id: import, use_tree } = &directive.import.source {
1357-
if let (Some(krate), PathKind::Plain | PathKind::Abs) =
1358-
(directive.import.path.segments().first(), &directive.import.path.kind)
1359-
{
1360-
if diagnosed_extern_crates.contains(krate) {
1361-
continue;
1362-
}
1349+
if let ImportSource::Import { id: import, use_tree } = directive.import.source {
1350+
if matches!(
1351+
(directive.import.path.segments().first(), &directive.import.path.kind),
1352+
(Some(krate), PathKind::Plain | PathKind::Abs) if diagnosed_extern_crates.contains(krate)
1353+
) {
1354+
continue;
13631355
}
13641356

13651357
self.def_map.diagnostics.push(DefDiagnostic::unresolved_import(
13661358
directive.module_id,
1367-
*import,
1368-
*use_tree,
1359+
import,
1360+
use_tree,
13691361
));
13701362
}
13711363
}
@@ -1386,6 +1378,13 @@ struct ModCollector<'a, 'b> {
13861378

13871379
impl ModCollector<'_, '_> {
13881380
fn collect(&mut self, items: &[ModItem]) {
1381+
struct DefData<'a> {
1382+
id: ModuleDefId,
1383+
name: &'a Name,
1384+
visibility: &'a RawVisibility,
1385+
has_constructor: bool,
1386+
}
1387+
13891388
let krate = self.def_collector.def_map.krate;
13901389

13911390
// Note: don't assert that inserted value is fresh: it's simply not true
@@ -1403,18 +1402,18 @@ impl ModCollector<'_, '_> {
14031402
// This should be processed eagerly instead of deferred to resolving.
14041403
// `#[macro_use] extern crate` is hoisted to imports macros before collecting
14051404
// any other items.
1406-
for item in items {
1407-
let attrs = self.item_tree.attrs(self.def_collector.db, krate, (*item).into());
1405+
for &item in items {
1406+
let attrs = self.item_tree.attrs(self.def_collector.db, krate, item.into());
14081407
if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) {
14091408
if let ModItem::ExternCrate(id) = item {
1410-
let import = self.item_tree[*id].clone();
1409+
let import = &self.item_tree[id];
14111410
let attrs = self.item_tree.attrs(
14121411
self.def_collector.db,
14131412
krate,
1414-
ModItem::from(*id).into(),
1413+
ModItem::from(id).into(),
14151414
);
14161415
if attrs.by_key("macro_use").exists() {
1417-
self.def_collector.import_macros_from_extern_crate(self.module_id, &import);
1416+
self.def_collector.import_macros_from_extern_crate(self.module_id, import);
14181417
}
14191418
}
14201419
}
@@ -1656,9 +1655,7 @@ impl ModCollector<'_, '_> {
16561655
let is_enabled = item_tree
16571656
.top_level_attrs(db, self.def_collector.def_map.krate)
16581657
.cfg()
1659-
.map_or(true, |cfg| {
1660-
self.def_collector.cfg_options.check(&cfg) != Some(false)
1661-
});
1658+
.map_or(true, |cfg| self.is_cfg_enabled(&cfg));
16621659
if is_enabled {
16631660
let module_id = self.push_child_module(
16641661
module.name.clone(),
@@ -1675,12 +1672,12 @@ impl ModCollector<'_, '_> {
16751672
mod_dir,
16761673
}
16771674
.collect(item_tree.top_level_items());
1678-
if is_macro_use
1675+
let is_macro_use = is_macro_use
16791676
|| item_tree
16801677
.top_level_attrs(db, self.def_collector.def_map.krate)
16811678
.by_key("macro_use")
1682-
.exists()
1683-
{
1679+
.exists();
1680+
if is_macro_use {
16841681
self.import_all_legacy_macros(module_id);
16851682
}
16861683
}
@@ -1714,14 +1711,17 @@ impl ModCollector<'_, '_> {
17141711
ModuleOrigin::File { declaration, definition, is_mod_rs }
17151712
}
17161713
};
1714+
17171715
let res = modules.alloc(ModuleData::new(origin, vis));
17181716
modules[res].parent = Some(self.module_id);
17191717
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
17201718
modules[res].scope.define_legacy_macro(name, mac)
17211719
}
17221720
modules[self.module_id].children.insert(name.clone(), res);
1721+
17231722
let module = self.def_collector.def_map.module_id(res);
1724-
let def: ModuleDefId = module.into();
1723+
let def = ModuleDefId::from(module);
1724+
17251725
self.def_collector.def_map.modules[self.module_id].scope.declare(def);
17261726
self.def_collector.update(
17271727
self.module_id,
@@ -1786,30 +1786,29 @@ impl ModCollector<'_, '_> {
17861786
}
17871787

17881788
fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
1789-
if path.kind == PathKind::Plain {
1790-
if let Some(tool_module) = path.segments().first() {
1791-
let tool_module = tool_module.to_smol_str();
1792-
let is_tool = builtin_attr::TOOL_MODULES
1793-
.iter()
1794-
.copied()
1795-
.chain(self.def_collector.registered_tools.iter().map(SmolStr::as_str))
1796-
.any(|m| tool_module == *m);
1797-
if is_tool {
1798-
return true;
1799-
}
1789+
if path.kind != PathKind::Plain {
1790+
return false;
1791+
}
1792+
1793+
let segments = path.segments();
1794+
1795+
if let Some(name) = segments.first() {
1796+
let name = name.to_smol_str();
1797+
let pred = |n: &_| *n == name;
1798+
1799+
let registered = self.def_collector.registered_tools.iter().map(SmolStr::as_str);
1800+
let is_tool = builtin_attr::TOOL_MODULES.iter().copied().chain(registered).any(pred);
1801+
if is_tool {
1802+
return true;
18001803
}
18011804

1802-
if let Some(name) = path.as_ident() {
1803-
let name = name.to_smol_str();
1804-
let is_inert = builtin_attr::INERT_ATTRIBUTES
1805-
.iter()
1806-
.copied()
1807-
.chain(self.def_collector.registered_attrs.iter().map(SmolStr::as_str))
1808-
.any(|attr| name == *attr);
1805+
if segments.len() == 1 {
1806+
let registered = self.def_collector.registered_attrs.iter().map(SmolStr::as_str);
1807+
let is_inert =
1808+
builtin_attr::INERT_ATTRIBUTES.iter().copied().chain(registered).any(pred);
18091809
return is_inert;
18101810
}
18111811
}
1812-
18131812
false
18141813
}
18151814

@@ -1831,7 +1830,7 @@ impl ModCollector<'_, '_> {
18311830

18321831
let is_export = export_attr.exists();
18331832
let is_local_inner = if is_export {
1834-
export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it {
1833+
export_attr.tt_values().flat_map(|it| &it.token_trees).any(|it| match it {
18351834
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
18361835
ident.text.contains("local_inner_macros")
18371836
}
@@ -1852,12 +1851,14 @@ impl ModCollector<'_, '_> {
18521851
&name
18531852
}
18541853
None => {
1855-
match attrs.by_key("rustc_builtin_macro").tt_values().next().and_then(|tt| {
1856-
match tt.token_trees.first() {
1857-
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
1858-
_ => None,
1859-
}
1860-
}) {
1854+
let explicit_name =
1855+
attrs.by_key("rustc_builtin_macro").tt_values().next().and_then(|tt| {
1856+
match tt.token_trees.first() {
1857+
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
1858+
_ => None,
1859+
}
1860+
});
1861+
match explicit_name {
18611862
Some(ident) => {
18621863
name = ident.as_name();
18631864
&name
@@ -1947,7 +1948,7 @@ impl ModCollector<'_, '_> {
19471948
}
19481949

19491950
fn collect_macro_call(&mut self, mac: &MacroCall) {
1950-
let ast_id = AstIdWithPath::new(self.file_id(), mac.ast_id, (*mac.path).clone());
1951+
let ast_id = AstIdWithPath::new(self.file_id(), mac.ast_id, ModPath::clone(&mac.path));
19511952

19521953
// Case 1: try to resolve in legacy scope and expand macro_rules
19531954
let mut error = None;

crates/hir_def/src/nameres/mod_resolution.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,13 @@ impl ModDir {
2424
pub(super) fn root() -> ModDir {
2525
ModDir { dir_path: DirPath::empty(), root_non_dir_owner: false, depth: 0 }
2626
}
27-
fn child(&self, dir_path: DirPath, root_non_dir_owner: bool) -> Option<ModDir> {
28-
let depth = self.depth + 1;
29-
if MOD_DEPTH_LIMIT.check(depth as usize).is_err() {
30-
tracing::error!("MOD_DEPTH_LIMIT exceeded");
31-
cov_mark::hit!(circular_mods);
32-
return None;
33-
}
34-
Some(ModDir { dir_path, root_non_dir_owner, depth })
35-
}
3627

3728
pub(super) fn descend_into_definition(
3829
&self,
3930
name: &Name,
4031
attr_path: Option<&SmolStr>,
4132
) -> Option<ModDir> {
42-
let path = match attr_path.map(|it| it.as_str()) {
33+
let path = match attr_path.map(SmolStr::as_str) {
4334
None => {
4435
let mut path = self.dir_path.clone();
4536
path.push(&name.to_smol_str());
@@ -56,6 +47,16 @@ impl ModDir {
5647
self.child(path, false)
5748
}
5849

50+
fn child(&self, dir_path: DirPath, root_non_dir_owner: bool) -> Option<ModDir> {
51+
let depth = self.depth + 1;
52+
if MOD_DEPTH_LIMIT.check(depth as usize).is_err() {
53+
tracing::error!("MOD_DEPTH_LIMIT exceeded");
54+
cov_mark::hit!(circular_mods);
55+
return None;
56+
}
57+
Some(ModDir { dir_path, root_non_dir_owner, depth })
58+
}
59+
5960
pub(super) fn resolve_declaration(
6061
&self,
6162
db: &dyn DefDatabase,

0 commit comments

Comments
 (0)