11637: minor: Simplify r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2022-03-06 16:56:29 +00:00 committed by GitHub
commit b9538122f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 83 deletions

View File

@ -102,7 +102,7 @@ use smallvec::SmallVec;
use stdx::impl_from; use stdx::impl_from;
use syntax::{ use syntax::{
ast::{self, HasName}, ast::{self, HasName},
match_ast, AstNode, SyntaxNode, AstNode, SyntaxNode,
}; };
use crate::{db::HirDatabase, InFile}; use crate::{db::HirDatabase, InFile};
@ -132,13 +132,10 @@ impl SourceToDefCtx<'_, '_> {
pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> { pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
let _p = profile::span("module_to_def"); let _p = profile::span("module_to_def");
let parent_declaration = let parent_declaration = src
src.syntax().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1).find_map( .syntax()
|it| { .ancestors_with_macros_skip_attr_item(self.db.upcast())
let m = ast::Module::cast(it.value.clone())?; .find_map(|it| it.map(ast::Module::cast).transpose());
Some(it.with_value(m))
},
);
let parent_module = match parent_declaration { let parent_module = match parent_declaration {
Some(parent_declaration) => self.module_to_def(parent_declaration), Some(parent_declaration) => self.module_to_def(parent_declaration),
@ -150,7 +147,7 @@ impl SourceToDefCtx<'_, '_> {
let child_name = src.value.name()?.as_name(); let child_name = src.value.name()?.as_name();
let def_map = parent_module.def_map(self.db.upcast()); let def_map = parent_module.def_map(self.db.upcast());
let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; let &child_id = def_map[parent_module.local_id].children.get(&child_name)?;
Some(def_map.module_id(child_id)) Some(def_map.module_id(child_id))
} }
@ -337,7 +334,7 @@ impl SourceToDefCtx<'_, '_> {
} }
pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> { pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
for container in src.ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1) { for container in src.ancestors_with_macros_skip_attr_item(self.db.upcast()) {
if let Some(res) = self.container_to_def(container) { if let Some(res) = self.container_to_def(container) {
return Some(res); return Some(res);
} }
@ -348,70 +345,62 @@ impl SourceToDefCtx<'_, '_> {
} }
fn container_to_def(&mut self, container: InFile<SyntaxNode>) -> Option<ChildContainer> { fn container_to_def(&mut self, container: InFile<SyntaxNode>) -> Option<ChildContainer> {
let cont = match_ast! { let cont = if let Some(item) = ast::Item::cast(container.value.clone()) {
match (container.value) { match item {
ast::Module(it) => { ast::Item::Module(it) => self.module_to_def(container.with_value(it))?.into(),
let def = self.module_to_def(container.with_value(it))?; ast::Item::Trait(it) => self.trait_to_def(container.with_value(it))?.into(),
def.into() ast::Item::Impl(it) => self.impl_to_def(container.with_value(it))?.into(),
}, ast::Item::Enum(it) => self.enum_to_def(container.with_value(it))?.into(),
ast::Trait(it) => { ast::Item::TypeAlias(it) => {
let def = self.trait_to_def(container.with_value(it))?; self.type_alias_to_def(container.with_value(it))?.into()
def.into() }
}, ast::Item::Struct(it) => {
ast::Impl(it) => {
let def = self.impl_to_def(container.with_value(it))?;
def.into()
},
ast::Fn(it) => {
let def = self.fn_to_def(container.with_value(it))?;
DefWithBodyId::from(def).into()
},
ast::Struct(it) => {
let def = self.struct_to_def(container.with_value(it))?; let def = self.struct_to_def(container.with_value(it))?;
VariantId::from(def).into() VariantId::from(def).into()
}, }
ast::Enum(it) => { ast::Item::Union(it) => {
let def = self.enum_to_def(container.with_value(it))?;
def.into()
},
ast::Union(it) => {
let def = self.union_to_def(container.with_value(it))?; let def = self.union_to_def(container.with_value(it))?;
VariantId::from(def).into() VariantId::from(def).into()
}, }
ast::Static(it) => { ast::Item::Fn(it) => {
let def = self.fn_to_def(container.with_value(it))?;
DefWithBodyId::from(def).into()
}
ast::Item::Static(it) => {
let def = self.static_to_def(container.with_value(it))?; let def = self.static_to_def(container.with_value(it))?;
DefWithBodyId::from(def).into() DefWithBodyId::from(def).into()
}, }
ast::Const(it) => { ast::Item::Const(it) => {
let def = self.const_to_def(container.with_value(it))?; let def = self.const_to_def(container.with_value(it))?;
DefWithBodyId::from(def).into() DefWithBodyId::from(def).into()
}, }
ast::TypeAlias(it) => {
let def = self.type_alias_to_def(container.with_value(it))?;
def.into()
},
ast::Variant(it) => {
let def = self.enum_variant_to_def(container.with_value(it))?;
VariantId::from(def).into()
},
_ => return None, _ => return None,
} }
} else {
let it = ast::Variant::cast(container.value)?;
let def = self.enum_variant_to_def(InFile::new(container.file_id, it))?;
VariantId::from(def).into()
}; };
Some(cont) Some(cont)
} }
fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> { fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
for container in src.ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1) { let ancestors = src.ancestors_with_macros_skip_attr_item(self.db.upcast());
let res: GenericDefId = match_ast! { for InFile { file_id, value } in ancestors {
match (container.value) { let item = match ast::Item::cast(value) {
ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), Some(it) => it,
ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(), None => continue,
ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(), };
ast::Trait(it) => self.trait_to_def(container.with_value(it))?.into(), let res: GenericDefId = match item {
ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(), ast::Item::Fn(it) => self.fn_to_def(InFile::new(file_id, it))?.into(),
ast::Impl(it) => self.impl_to_def(container.with_value(it))?.into(), ast::Item::Struct(it) => self.struct_to_def(InFile::new(file_id, it))?.into(),
_ => continue, ast::Item::Enum(it) => self.enum_to_def(InFile::new(file_id, it))?.into(),
ast::Item::Trait(it) => self.trait_to_def(InFile::new(file_id, it))?.into(),
ast::Item::TypeAlias(it) => {
self.type_alias_to_def(InFile::new(file_id, it))?.into()
} }
ast::Item::Impl(it) => self.impl_to_def(InFile::new(file_id, it))?.into(),
_ => continue,
}; };
return Some(res); return Some(res);
} }
@ -419,14 +408,17 @@ impl SourceToDefCtx<'_, '_> {
} }
fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> { fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
for container in src.ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1) { let ancestors = src.ancestors_with_macros_skip_attr_item(self.db.upcast());
let res: DefWithBodyId = match_ast! { for InFile { file_id, value } in ancestors {
match (container.value) { let item = match ast::Item::cast(value) {
ast::Const(it) => self.const_to_def(container.with_value(it))?.into(), Some(it) => it,
ast::Static(it) => self.static_to_def(container.with_value(it))?.into(), None => continue,
ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), };
_ => continue, let res: DefWithBodyId = match item {
} ast::Item::Const(it) => self.const_to_def(InFile::new(file_id, it))?.into(),
ast::Item::Static(it) => self.static_to_def(InFile::new(file_id, it))?.into(),
ast::Item::Fn(it) => self.fn_to_def(InFile::new(file_id, it))?.into(),
_ => continue,
}; };
return Some(res); return Some(res);
} }

View File

@ -4,7 +4,10 @@ use base_db::{AnchoredPath, Edition, FileId};
use cfg::CfgExpr; use cfg::CfgExpr;
use either::Either; use either::Either;
use mbe::{parse_exprs_with_sep, parse_to_token_tree}; use mbe::{parse_exprs_with_sep, parse_to_token_tree};
use syntax::ast::{self, AstToken}; use syntax::{
ast::{self, AstToken},
SmolStr,
};
use crate::{ use crate::{
db::AstDatabase, name, quote, AstId, CrateId, ExpandError, ExpandResult, MacroCallId, db::AstDatabase, name, quote, AstId, CrateId, ExpandError, ExpandResult, MacroCallId,
@ -130,6 +133,9 @@ register_builtin! {
(option_env, OptionEnv) => option_env_expand (option_env, OptionEnv) => option_env_expand
} }
const DOLLAR_CRATE: tt::Ident =
tt::Ident { text: SmolStr::new_inline("$crate"), id: tt::TokenId::unspecified() };
fn module_path_expand( fn module_path_expand(
_db: &dyn AstDatabase, _db: &dyn AstDatabase,
_id: MacroCallId, _id: MacroCallId,
@ -202,7 +208,6 @@ fn assert_expand(
_id: MacroCallId, _id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> { ) -> ExpandResult<tt::Subtree> {
let krate = tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() };
let args = parse_exprs_with_sep(tt, ','); let args = parse_exprs_with_sep(tt, ',');
let expanded = match &*args { let expanded = match &*args {
[cond, panic_args @ ..] => { [cond, panic_args @ ..] => {
@ -218,7 +223,7 @@ fn assert_expand(
let panic_args = itertools::Itertools::intersperse(panic_args.iter().cloned(), comma); let panic_args = itertools::Itertools::intersperse(panic_args.iter().cloned(), comma);
quote! {{ quote! {{
if !#cond { if !#cond {
#krate::panic!(##panic_args); #DOLLAR_CRATE::panic!(##panic_args);
} }
}} }}
} }
@ -293,15 +298,13 @@ fn asm_expand(
// We expand all assembly snippets to `format_args!` invocations to get format syntax // We expand all assembly snippets to `format_args!` invocations to get format syntax
// highlighting for them. // highlighting for them.
let krate = tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() };
let mut literals = Vec::new(); let mut literals = Vec::new();
for tt in tt.token_trees.chunks(2) { for tt in tt.token_trees.chunks(2) {
match tt { match tt {
[tt::TokenTree::Leaf(tt::Leaf::Literal(lit))] [tt::TokenTree::Leaf(tt::Leaf::Literal(lit))]
| [tt::TokenTree::Leaf(tt::Leaf::Literal(lit)), tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: ',', id: _, spacing: _ }))] => | [tt::TokenTree::Leaf(tt::Leaf::Literal(lit)), tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: ',', id: _, spacing: _ }))] =>
{ {
let krate = krate.clone(); let krate = DOLLAR_CRATE.clone();
literals.push(quote!(#krate::format_args!(#lit);)); literals.push(quote!(#krate::format_args!(#lit);));
} }
_ => break, _ => break,
@ -343,11 +346,10 @@ fn panic_expand(
) -> ExpandResult<tt::Subtree> { ) -> ExpandResult<tt::Subtree> {
let loc: MacroCallLoc = db.lookup_intern_macro_call(id); let loc: MacroCallLoc = db.lookup_intern_macro_call(id);
// Expand to a macro call `$crate::panic::panic_{edition}` // Expand to a macro call `$crate::panic::panic_{edition}`
let krate = tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() };
let mut call = if db.crate_graph()[loc.krate].edition >= Edition::Edition2021 { let mut call = if db.crate_graph()[loc.krate].edition >= Edition::Edition2021 {
quote!(#krate::panic::panic_2021!) quote!(#DOLLAR_CRATE::panic::panic_2021!)
} else { } else {
quote!(#krate::panic::panic_2015!) quote!(#DOLLAR_CRATE::panic::panic_2015!)
}; };
// Pass the original arguments // Pass the original arguments
@ -362,11 +364,10 @@ fn unreachable_expand(
) -> ExpandResult<tt::Subtree> { ) -> ExpandResult<tt::Subtree> {
let loc: MacroCallLoc = db.lookup_intern_macro_call(id); let loc: MacroCallLoc = db.lookup_intern_macro_call(id);
// Expand to a macro call `$crate::panic::unreachable_{edition}` // Expand to a macro call `$crate::panic::unreachable_{edition}`
let krate = tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() };
let mut call = if db.crate_graph()[loc.krate].edition >= Edition::Edition2021 { let mut call = if db.crate_graph()[loc.krate].edition >= Edition::Edition2021 {
quote!(#krate::panic::unreachable_2021!) quote!(#DOLLAR_CRATE::panic::unreachable_2021!)
} else { } else {
quote!(#krate::panic::unreachable_2015!) quote!(#DOLLAR_CRATE::panic::unreachable_2015!)
}; };
// Pass the original arguments // Pass the original arguments

View File

@ -712,7 +712,7 @@ impl<'a> InFile<&'a SyntaxNode> {
self, self,
db: &dyn db::AstDatabase, db: &dyn db::AstDatabase,
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
iter::successors(Some(self.cloned()), move |node| match node.value.parent() { let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() {
Some(parent) => Some(node.with_value(parent)), Some(parent) => Some(node.with_value(parent)),
None => { None => {
let parent_node = node.file_id.call_node(db)?; let parent_node = node.file_id.call_node(db)?;
@ -724,7 +724,8 @@ impl<'a> InFile<&'a SyntaxNode> {
Some(parent_node) Some(parent_node)
} }
} }
}) };
iter::successors(succ(&self.cloned()), succ)
} }
/// Falls back to the macro call range if the node cannot be mapped up fully. /// Falls back to the macro call range if the node cannot be mapped up fully.

View File

@ -150,9 +150,9 @@ impl IdentClass {
sema: &Semantics<RootDatabase>, sema: &Semantics<RootDatabase>,
lifetime: &ast::Lifetime, lifetime: &ast::Lifetime,
) -> Option<IdentClass> { ) -> Option<IdentClass> {
NameClass::classify_lifetime(sema, &lifetime).map(IdentClass::NameClass).or_else(|| { NameRefClass::classify_lifetime(sema, &lifetime)
NameRefClass::classify_lifetime(sema, &lifetime).map(IdentClass::NameRefClass) .map(IdentClass::NameRefClass)
}) .or_else(|| NameClass::classify_lifetime(sema, &lifetime).map(IdentClass::NameClass))
} }
pub fn definitions(self) -> ArrayVec<Definition, 2> { pub fn definitions(self) -> ArrayVec<Definition, 2> {