Skip to content

minor: Simplify #10803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions crates/hir_def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,34 +723,32 @@ impl Attr {
/// to derive macros.
///
/// Returns `None` when the attribute does not have a well-formed `#[derive]` attribute input.
pub(crate) fn parse_derive(&self) -> Option<impl Iterator<Item = ModPath>> {
if let Some(AttrInput::TokenTree(args, _)) = self.input.as_deref() {
if args.delimiter_kind() != Some(DelimiterKind::Parenthesis) {
return None;
}
let mut counter = 0;
let paths = args
.token_trees
.iter()
.group_by(move |tt| {
if let tt::TokenTree::Leaf(tt::Leaf::Punct(Punct { char: ',', .. })) = tt {
counter += 1;
}
counter
})
.into_iter()
.map(|(_, tts)| {
let segments = tts.filter_map(|tt| match tt {
tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => Some(id.as_name()),
_ => None,
});
ModPath::from_segments(PathKind::Plain, segments)
})
.collect::<Vec<_>>();

return Some(paths.into_iter());
pub(crate) fn parse_derive(&self) -> Option<impl Iterator<Item = ModPath> + '_> {
let args = match self.input.as_deref() {
Some(AttrInput::TokenTree(args, _)) => args,
_ => return None,
};

if args.delimiter_kind() != Some(DelimiterKind::Parenthesis) {
return None;
}
None
let paths = args
.token_trees
.iter()
.group_by(|tt| {
matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Punct(Punct { char: ',', .. })))
})
.into_iter()
.map(|(_, tts)| {
let segments = tts.filter_map(|tt| match tt {
tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => Some(id.as_name()),
_ => None,
});
ModPath::from_segments(PathKind::Plain, segments)
})
.collect::<Vec<_>>();

return Some(paths.into_iter());
}

pub fn string_value(&self) -> Option<&SmolStr> {
Expand Down
111 changes: 56 additions & 55 deletions crates/hir_def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,6 @@ enum MacroDirectiveKind {
Attr { ast_id: AstIdWithPath<ast::Item>, attr: Attr, mod_item: ModItem },
}

struct DefData<'a> {
id: ModuleDefId,
name: &'a Name,
visibility: &'a RawVisibility,
has_constructor: bool,
}

/// Walks the tree of module recursively
struct DefCollector<'a> {
db: &'a dyn DefDatabase,
Expand Down Expand Up @@ -1353,19 +1346,18 @@ impl DefCollector<'_> {
}

for directive in &self.unresolved_imports {
if let ImportSource::Import { id: import, use_tree } = &directive.import.source {
if let (Some(krate), PathKind::Plain | PathKind::Abs) =
(directive.import.path.segments().first(), &directive.import.path.kind)
{
if diagnosed_extern_crates.contains(krate) {
continue;
}
if let ImportSource::Import { id: import, use_tree } = directive.import.source {
if matches!(
(directive.import.path.segments().first(), &directive.import.path.kind),
(Some(krate), PathKind::Plain | PathKind::Abs) if diagnosed_extern_crates.contains(krate)
) {
continue;
}

self.def_map.diagnostics.push(DefDiagnostic::unresolved_import(
directive.module_id,
*import,
*use_tree,
import,
use_tree,
));
}
}
Expand All @@ -1386,6 +1378,13 @@ struct ModCollector<'a, 'b> {

impl ModCollector<'_, '_> {
fn collect(&mut self, items: &[ModItem]) {
struct DefData<'a> {
id: ModuleDefId,
name: &'a Name,
visibility: &'a RawVisibility,
has_constructor: bool,
}

let krate = self.def_collector.def_map.krate;

// Note: don't assert that inserted value is fresh: it's simply not true
Expand All @@ -1403,18 +1402,18 @@ impl ModCollector<'_, '_> {
// This should be processed eagerly instead of deferred to resolving.
// `#[macro_use] extern crate` is hoisted to imports macros before collecting
// any other items.
for item in items {
let attrs = self.item_tree.attrs(self.def_collector.db, krate, (*item).into());
for &item in items {
let attrs = self.item_tree.attrs(self.def_collector.db, krate, item.into());
if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) {
if let ModItem::ExternCrate(id) = item {
let import = self.item_tree[*id].clone();
let import = &self.item_tree[id];
let attrs = self.item_tree.attrs(
self.def_collector.db,
krate,
ModItem::from(*id).into(),
ModItem::from(id).into(),
);
if attrs.by_key("macro_use").exists() {
self.def_collector.import_macros_from_extern_crate(self.module_id, &import);
self.def_collector.import_macros_from_extern_crate(self.module_id, import);
}
}
}
Expand Down Expand Up @@ -1656,9 +1655,7 @@ impl ModCollector<'_, '_> {
let is_enabled = item_tree
.top_level_attrs(db, self.def_collector.def_map.krate)
.cfg()
.map_or(true, |cfg| {
self.def_collector.cfg_options.check(&cfg) != Some(false)
});
.map_or(true, |cfg| self.is_cfg_enabled(&cfg));
if is_enabled {
let module_id = self.push_child_module(
module.name.clone(),
Expand All @@ -1675,12 +1672,12 @@ impl ModCollector<'_, '_> {
mod_dir,
}
.collect(item_tree.top_level_items());
if is_macro_use
let is_macro_use = is_macro_use
|| item_tree
.top_level_attrs(db, self.def_collector.def_map.krate)
.by_key("macro_use")
.exists()
{
.exists();
if is_macro_use {
self.import_all_legacy_macros(module_id);
}
}
Expand Down Expand Up @@ -1714,14 +1711,17 @@ impl ModCollector<'_, '_> {
ModuleOrigin::File { declaration, definition, is_mod_rs }
}
};

let res = modules.alloc(ModuleData::new(origin, vis));
modules[res].parent = Some(self.module_id);
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
modules[res].scope.define_legacy_macro(name, mac)
}
modules[self.module_id].children.insert(name.clone(), res);

let module = self.def_collector.def_map.module_id(res);
let def: ModuleDefId = module.into();
let def = ModuleDefId::from(module);

self.def_collector.def_map.modules[self.module_id].scope.declare(def);
self.def_collector.update(
self.module_id,
Expand Down Expand Up @@ -1786,30 +1786,29 @@ impl ModCollector<'_, '_> {
}

fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
if path.kind == PathKind::Plain {
if let Some(tool_module) = path.segments().first() {
let tool_module = tool_module.to_smol_str();
let is_tool = builtin_attr::TOOL_MODULES
.iter()
.copied()
.chain(self.def_collector.registered_tools.iter().map(SmolStr::as_str))
.any(|m| tool_module == *m);
if is_tool {
return true;
}
if path.kind != PathKind::Plain {
return false;
}

let segments = path.segments();

if let Some(name) = segments.first() {
let name = name.to_smol_str();
let pred = |n: &_| *n == name;

let registered = self.def_collector.registered_tools.iter().map(SmolStr::as_str);
let is_tool = builtin_attr::TOOL_MODULES.iter().copied().chain(registered).any(pred);
if is_tool {
return true;
}

if let Some(name) = path.as_ident() {
let name = name.to_smol_str();
let is_inert = builtin_attr::INERT_ATTRIBUTES
.iter()
.copied()
.chain(self.def_collector.registered_attrs.iter().map(SmolStr::as_str))
.any(|attr| name == *attr);
if segments.len() == 1 {
let registered = self.def_collector.registered_attrs.iter().map(SmolStr::as_str);
let is_inert =
builtin_attr::INERT_ATTRIBUTES.iter().copied().chain(registered).any(pred);
return is_inert;
}
}

false
}

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

let is_export = export_attr.exists();
let is_local_inner = if is_export {
export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it {
export_attr.tt_values().flat_map(|it| &it.token_trees).any(|it| match it {
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
ident.text.contains("local_inner_macros")
}
Expand All @@ -1852,12 +1851,14 @@ impl ModCollector<'_, '_> {
&name
}
None => {
match attrs.by_key("rustc_builtin_macro").tt_values().next().and_then(|tt| {
match tt.token_trees.first() {
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
_ => None,
}
}) {
let explicit_name =
attrs.by_key("rustc_builtin_macro").tt_values().next().and_then(|tt| {
match tt.token_trees.first() {
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
_ => None,
}
});
match explicit_name {
Some(ident) => {
name = ident.as_name();
&name
Expand Down Expand Up @@ -1947,7 +1948,7 @@ impl ModCollector<'_, '_> {
}

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

// Case 1: try to resolve in legacy scope and expand macro_rules
let mut error = None;
Expand Down
21 changes: 11 additions & 10 deletions crates/hir_def/src/nameres/mod_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,13 @@ impl ModDir {
pub(super) fn root() -> ModDir {
ModDir { dir_path: DirPath::empty(), root_non_dir_owner: false, depth: 0 }
}
fn child(&self, dir_path: DirPath, root_non_dir_owner: bool) -> Option<ModDir> {
let depth = self.depth + 1;
if MOD_DEPTH_LIMIT.check(depth as usize).is_err() {
tracing::error!("MOD_DEPTH_LIMIT exceeded");
cov_mark::hit!(circular_mods);
return None;
}
Some(ModDir { dir_path, root_non_dir_owner, depth })
}

pub(super) fn descend_into_definition(
&self,
name: &Name,
attr_path: Option<&SmolStr>,
) -> Option<ModDir> {
let path = match attr_path.map(|it| it.as_str()) {
let path = match attr_path.map(SmolStr::as_str) {
None => {
let mut path = self.dir_path.clone();
path.push(&name.to_smol_str());
Expand All @@ -56,6 +47,16 @@ impl ModDir {
self.child(path, false)
}

fn child(&self, dir_path: DirPath, root_non_dir_owner: bool) -> Option<ModDir> {
let depth = self.depth + 1;
if MOD_DEPTH_LIMIT.check(depth as usize).is_err() {
tracing::error!("MOD_DEPTH_LIMIT exceeded");
cov_mark::hit!(circular_mods);
return None;
}
Some(ModDir { dir_path, root_non_dir_owner, depth })
}

pub(super) fn resolve_declaration(
&self,
db: &dyn DefDatabase,
Expand Down