11663: Internal: Add hir_def::MacroId, add Macro{Id} to ModuleDef{Id} r=Veykril a=Veykril

With this we can now handle macros like we handle ModuleDefs making them work more like other definitions and allowing us to remove a bunch of special cases. This also enables us to track the modules these macros are defined in, instead of only recording the crate they come from.

Introduces a new class of `MacroId`s (for each of the 3 macro kinds) into `hir_def`. We can't reuse `MacroDefId` as that is defined in `hir_expand` which doesn't know of modules, so now we have two different macro ids, this unfortunately requires some back and forth mapping between the two via database accesses which I hope won't be too expensive.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2022-03-09 10:26:34 +00:00 committed by GitHub
commit d70ea759b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 1108 additions and 796 deletions

View file

@ -1,12 +1,8 @@
//! Builtin attributes.
use itertools::Itertools;
use syntax::ast;
use crate::{
db::AstDatabase, name, AstId, CrateId, ExpandResult, MacroCallId, MacroCallKind, MacroDefId,
MacroDefKind,
};
use crate::{db::AstDatabase, name, ExpandResult, MacroCallId, MacroCallKind};
macro_rules! register_builtin {
( $(($name:ident, $variant:ident) => $expand:ident),* ) => {
@ -61,17 +57,8 @@ register_builtin! {
(test_case, TestCase) => dummy_attr_expand
}
pub fn find_builtin_attr(
ident: &name::Name,
krate: CrateId,
ast_id: AstId<ast::Macro>,
) -> Option<MacroDefId> {
let expander = BuiltinAttrExpander::find_by_name(ident)?;
Some(MacroDefId {
krate,
kind: MacroDefKind::BuiltInAttr(expander, ast_id),
local_inner: false,
})
pub fn find_builtin_attr(ident: &name::Name) -> Option<BuiltinAttrExpander> {
BuiltinAttrExpander::find_by_name(ident)
}
fn dummy_attr_expand(

View file

@ -8,10 +8,7 @@ use syntax::{
};
use tt::TokenId;
use crate::{
db::AstDatabase, name, quote, AstId, CrateId, ExpandError, ExpandResult, MacroCallId,
MacroDefId, MacroDefKind,
};
use crate::{db::AstDatabase, name, quote, ExpandError, ExpandResult, MacroCallId};
macro_rules! register_builtin {
( $($trait:ident => $expand:ident),* ) => {
@ -56,17 +53,8 @@ register_builtin! {
PartialEq => partial_eq_expand
}
pub fn find_builtin_derive(
ident: &name::Name,
krate: CrateId,
ast_id: AstId<ast::Macro>,
) -> Option<MacroDefId> {
let expander = BuiltinDeriveExpander::find_by_name(ident)?;
Some(MacroDefId {
krate,
kind: MacroDefKind::BuiltInDerive(expander, ast_id),
local_inner: false,
})
pub fn find_builtin_derive(ident: &name::Name) -> Option<BuiltinDeriveExpander> {
BuiltinDeriveExpander::find_by_name(ident)
}
struct BasicAdtInfo {

View file

@ -9,10 +9,7 @@ use syntax::{
SmolStr,
};
use crate::{
db::AstDatabase, name, quote, AstId, CrateId, ExpandError, ExpandResult, MacroCallId,
MacroCallLoc, MacroDefId, MacroDefKind,
};
use crate::{db::AstDatabase, name, quote, ExpandError, ExpandResult, MacroCallId, MacroCallLoc};
macro_rules! register_builtin {
( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
@ -79,23 +76,8 @@ impl ExpandedEager {
pub fn find_builtin_macro(
ident: &name::Name,
krate: CrateId,
ast_id: AstId<ast::Macro>,
) -> Option<MacroDefId> {
let kind = find_by_name(ident)?;
match kind {
Either::Left(kind) => Some(MacroDefId {
krate,
kind: MacroDefKind::BuiltIn(kind, ast_id),
local_inner: false,
}),
Either::Right(kind) => Some(MacroDefId {
krate,
kind: MacroDefKind::BuiltInEager(kind, ast_id),
local_inner: false,
}),
}
) -> Option<Either<BuiltinFnLikeExpander, EagerExpander>> {
find_by_name(ident)
}
register_builtin! {