mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-05 05:04:24 +00:00
Merge #1268
1268: simplify r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
b22614f0b5
@ -10,7 +10,7 @@ use ra_syntax::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId,
|
||||
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, MacroCallLoc,
|
||||
name::AsName,
|
||||
type_ref::{Mutability, TypeRef},
|
||||
};
|
||||
@ -828,7 +828,8 @@ where
|
||||
.ast_id(e)
|
||||
.with_file_id(self.current_file_id);
|
||||
|
||||
if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) {
|
||||
if let Some(def) = self.resolver.resolve_macro_call(path) {
|
||||
let call_id = MacroCallLoc { def, ast_id }.id(self.db);
|
||||
if let Some(tt) = self.db.macro_expand(call_id).ok() {
|
||||
if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() {
|
||||
log::debug!("macro expansion {}", expr.syntax().debug_dump());
|
||||
|
@ -272,8 +272,8 @@ impl CrateDefMap {
|
||||
(res.resolved_def, res.segment_index)
|
||||
}
|
||||
|
||||
pub(crate) fn find_macro(&self, name: &Name) -> Option<&MacroDefId> {
|
||||
self.public_macros.get(name).or(self.local_macros.get(name))
|
||||
pub(crate) fn find_macro(&self, name: &Name) -> Option<MacroDefId> {
|
||||
self.public_macros.get(name).or(self.local_macros.get(name)).map(|it| *it)
|
||||
}
|
||||
|
||||
// Returns Yes if we are sure that additions to `ItemMap` wouldn't change
|
||||
|
@ -1,16 +1,12 @@
|
||||
//! Name resolution.
|
||||
use std::sync::Arc;
|
||||
|
||||
use ra_syntax::ast;
|
||||
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
|
||||
use crate::{
|
||||
ModuleDef, Trait,
|
||||
code_model_api::Crate,
|
||||
MacroCallId,
|
||||
MacroCallLoc,
|
||||
AstId,
|
||||
MacroDefId,
|
||||
db::HirDatabase,
|
||||
name::{Name, KnownName},
|
||||
nameres::{PerNs, CrateDefMap, CrateModuleId},
|
||||
@ -134,16 +130,9 @@ impl Resolver {
|
||||
resolution
|
||||
}
|
||||
|
||||
pub fn resolve_macro_call(
|
||||
&self,
|
||||
db: &impl HirDatabase,
|
||||
path: Option<Path>,
|
||||
ast_id: AstId<ast::MacroCall>,
|
||||
) -> Option<MacroCallId> {
|
||||
pub(crate) fn resolve_macro_call(&self, path: Option<Path>) -> Option<MacroDefId> {
|
||||
let name = path.and_then(|path| path.expand_macro_expr()).unwrap_or_else(Name::missing);
|
||||
let def_id = self.module().and_then(|(module, _)| module.find_macro(&name))?;
|
||||
let call_loc = MacroCallLoc { def: *def_id, ast_id }.id(db);
|
||||
Some(call_loc)
|
||||
self.module()?.0.find_macro(&name)
|
||||
}
|
||||
|
||||
/// Returns the resolved path segments
|
||||
|
@ -20,7 +20,7 @@ use crate::{
|
||||
HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name,
|
||||
AsName, Module, HirFileId, Crate, Trait, Resolver, Ty,Path,
|
||||
expr::{BodySourceMap, scope::{ScopeId, ExprScopes}},
|
||||
ids::{LocationCtx,MacroCallId},
|
||||
ids::{LocationCtx, MacroDefId},
|
||||
docs::{docs_from_ast,Documentation},
|
||||
expr, AstId,
|
||||
};
|
||||
@ -191,13 +191,12 @@ pub enum PathResolution {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct MacroByExampleDef {
|
||||
pub(crate) id: MacroCallId,
|
||||
pub(crate) id: MacroDefId,
|
||||
}
|
||||
|
||||
impl MacroByExampleDef {
|
||||
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::MacroCall>) {
|
||||
let loc = self.id.loc(db);
|
||||
(self.id.into(), loc.def.0.to_node(db))
|
||||
(self.id.0.file_id(), self.id.0.to_node(db))
|
||||
}
|
||||
}
|
||||
|
||||
@ -284,21 +283,9 @@ impl SourceAnalyzer {
|
||||
self.infer.as_ref()?.field_resolution(expr_id)
|
||||
}
|
||||
|
||||
pub fn resolve_macro_call(
|
||||
&self,
|
||||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
macro_call: &ast::MacroCall,
|
||||
) -> Option<MacroByExampleDef> {
|
||||
let hir_id = file_id.into();
|
||||
let ast_id = db.ast_id_map(hir_id).ast_id(macro_call).with_file_id(hir_id);
|
||||
let call_id = self.resolver.resolve_macro_call(
|
||||
db,
|
||||
macro_call.path().and_then(Path::from_ast),
|
||||
ast_id,
|
||||
);
|
||||
|
||||
call_id.map(|id| MacroByExampleDef { id })
|
||||
pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroByExampleDef> {
|
||||
let id = self.resolver.resolve_macro_call(macro_call.path().and_then(Path::from_ast))?;
|
||||
Some(MacroByExampleDef { id })
|
||||
}
|
||||
|
||||
pub fn resolve_hir_path(
|
||||
|
@ -69,7 +69,7 @@ pub(crate) fn reference_definition(
|
||||
.and_then(ast::MacroCall::cast)
|
||||
{
|
||||
tested_by!(goto_definition_works_for_macros);
|
||||
if let Some(macro_call) = analyzer.resolve_macro_call(db, file_id, macro_call) {
|
||||
if let Some(macro_call) = analyzer.resolve_macro_call(macro_call) {
|
||||
return Exact(NavigationTarget::from_macro_def(db, macro_call));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user