Skip to content
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
22 changes: 21 additions & 1 deletion crates/hir-def/src/builtin_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use intern::{Symbol, sym};
use tt::TextRange;

use crate::{
AdtId, BuiltinDeriveImplId, BuiltinDeriveImplLoc, FunctionId, HasModule, db::DefDatabase,
AdtId, BuiltinDeriveImplId, BuiltinDeriveImplLoc, FunctionId, HasModule, MacroId,
db::DefDatabase, lang_item::LangItems,
};

macro_rules! declare_enum {
Expand Down Expand Up @@ -86,6 +87,25 @@ declare_enum!(
DispatchFromDyn => [],
);

impl BuiltinDeriveImplTrait {
pub fn derive_macro(self, lang_items: &LangItems) -> Option<MacroId> {
match self {
BuiltinDeriveImplTrait::Copy => lang_items.CopyDerive,
BuiltinDeriveImplTrait::Clone => lang_items.CloneDerive,
BuiltinDeriveImplTrait::Default => lang_items.DefaultDerive,
BuiltinDeriveImplTrait::Debug => lang_items.DebugDerive,
BuiltinDeriveImplTrait::Hash => lang_items.HashDerive,
BuiltinDeriveImplTrait::Ord => lang_items.OrdDerive,
BuiltinDeriveImplTrait::PartialOrd => lang_items.PartialOrdDerive,
BuiltinDeriveImplTrait::Eq => lang_items.EqDerive,
BuiltinDeriveImplTrait::PartialEq => lang_items.PartialEqDerive,
BuiltinDeriveImplTrait::CoerceUnsized | BuiltinDeriveImplTrait::DispatchFromDyn => {
lang_items.CoercePointeeDerive
}
}
}
}

impl BuiltinDeriveImplMethod {
pub fn trait_method(
self,
Expand Down
10 changes: 6 additions & 4 deletions crates/hir-def/src/dyn_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
pub mod keys {
use std::marker::PhantomData;

use either::Either;
use hir_expand::{MacroCallId, attrs::AttrId};
use rustc_hash::FxHashMap;
use syntax::{AstNode, AstPtr, ast};

use crate::{
BlockId, ConstId, EnumId, EnumVariantId, ExternBlockId, ExternCrateId, FieldId, FunctionId,
ImplId, LifetimeParamId, Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId, TraitId,
TypeAliasId, TypeOrConstParamId, UnionId, UseId,
BlockId, BuiltinDeriveImplId, ConstId, EnumId, EnumVariantId, ExternBlockId, ExternCrateId,
FieldId, FunctionId, ImplId, LifetimeParamId, Macro2Id, MacroRulesId, ProcMacroId,
StaticId, StructId, TraitId, TypeAliasId, TypeOrConstParamId, UnionId, UseId,
dyn_map::{DynMap, Policy},
};

Expand Down Expand Up @@ -71,7 +72,8 @@ pub mod keys {
(
AttrId,
/* derive() */ MacroCallId,
/* actual derive macros */ Box<[Option<MacroCallId>]>,
/* actual derive macros */
Box<[Option<Either<MacroCallId, BuiltinDeriveImplId>>]>,
),
> = Key::new();

Expand Down
19 changes: 14 additions & 5 deletions crates/hir-def/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::{fmt, sync::LazyLock};

use base_db::Crate;
use either::Either;
use hir_expand::{AstId, MacroCallId, attrs::AttrId, name::Name};
use indexmap::map::Entry;
use itertools::Itertools;
Expand Down Expand Up @@ -199,7 +200,7 @@ struct DeriveMacroInvocation {
attr_id: AttrId,
/// The `#[derive]` call
attr_call_id: MacroCallId,
derive_call_ids: SmallVec<[Option<MacroCallId>; 4]>,
derive_call_ids: SmallVec<[Option<Either<MacroCallId, BuiltinDeriveImplId>>; 4]>,
}

pub(crate) static BUILTIN_SCOPE: LazyLock<FxIndexMap<Name, PerNs>> = LazyLock::new(|| {
Expand Down Expand Up @@ -345,7 +346,9 @@ impl ItemScope {
pub fn all_macro_calls(&self) -> impl Iterator<Item = MacroCallId> + '_ {
self.macro_invocations.values().copied().chain(self.attr_macros.values().copied()).chain(
self.derive_macros.values().flat_map(|it| {
it.iter().flat_map(|it| it.derive_call_ids.iter().copied().flatten())
it.iter().flat_map(|it| {
it.derive_call_ids.iter().copied().flatten().flat_map(|it| it.left())
})
}),
)
}
Expand Down Expand Up @@ -379,6 +382,10 @@ impl ItemScope {
self.types.get(name).map(|item| (item.def, item.vis))
}

pub(crate) fn makro(&self, name: &Name) -> Option<MacroId> {
self.macros.get(name).map(|item| item.def)
}

/// XXX: this is O(N) rather than O(1), try to not introduce new usages.
pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility, /*declared*/ bool)> {
match item {
Expand Down Expand Up @@ -519,7 +526,7 @@ impl ItemScope {
pub(crate) fn set_derive_macro_invoc(
&mut self,
adt: AstId<ast::Adt>,
call: MacroCallId,
call: Either<MacroCallId, BuiltinDeriveImplId>,
id: AttrId,
idx: usize,
) {
Expand All @@ -539,7 +546,7 @@ impl ItemScope {
adt: AstId<ast::Adt>,
attr_id: AttrId,
attr_call_id: MacroCallId,
mut derive_call_ids: SmallVec<[Option<MacroCallId>; 4]>,
mut derive_call_ids: SmallVec<[Option<Either<MacroCallId, BuiltinDeriveImplId>>; 4]>,
) {
derive_call_ids.shrink_to_fit();
self.derive_macros.entry(adt).or_default().push(DeriveMacroInvocation {
Expand All @@ -554,7 +561,9 @@ impl ItemScope {
) -> impl Iterator<
Item = (
AstId<ast::Adt>,
impl Iterator<Item = (AttrId, MacroCallId, &[Option<MacroCallId>])>,
impl Iterator<
Item = (AttrId, MacroCallId, &[Option<Either<MacroCallId, BuiltinDeriveImplId>>]),
>,
),
> + '_ {
self.derive_macros.iter().map(|(k, v)| {
Expand Down
54 changes: 48 additions & 6 deletions crates/hir-def/src/lang_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use intern::{Symbol, sym};
use stdx::impl_from;

use crate::{
AdtId, AssocItemId, AttrDefId, Crate, EnumId, EnumVariantId, FunctionId, ImplId, ModuleDefId,
StaticId, StructId, TraitId, TypeAliasId, UnionId,
AdtId, AssocItemId, AttrDefId, Crate, EnumId, EnumVariantId, FunctionId, ImplId, MacroId,
ModuleDefId, StaticId, StructId, TraitId, TypeAliasId, UnionId,
attrs::AttrFlags,
db::DefDatabase,
nameres::{DefMap, assoc::TraitItems, crate_def_map, crate_local_def_map},
Expand Down Expand Up @@ -99,7 +99,7 @@ pub fn crate_lang_items(db: &dyn DefDatabase, krate: Crate) -> Option<Box<LangIt
}

if matches!(krate.data(db).origin, base_db::CrateOrigin::Lang(base_db::LangCrateOrigin::Core)) {
lang_items.fill_non_lang_core_traits(db, crate_def_map);
lang_items.fill_non_lang_core_items(db, crate_def_map);
}

if lang_items.is_empty() { None } else { Some(Box::new(lang_items)) }
Expand Down Expand Up @@ -169,6 +169,27 @@ fn resolve_core_trait(
Some(trait_)
}

fn resolve_core_macro(
db: &dyn DefDatabase,
core_def_map: &DefMap,
modules: &[Symbol],
name: Symbol,
) -> Option<MacroId> {
let mut current = &core_def_map[core_def_map.root];
for module in modules {
let Some((ModuleDefId::ModuleId(cur), _)) =
current.scope.type_(&Name::new_symbol_root(module.clone()))
else {
return None;
};
if cur.krate(db) != core_def_map.krate() || cur.block(db) != core_def_map.block_id() {
return None;
}
current = &core_def_map[cur];
}
current.scope.makro(&Name::new_symbol_root(name))
}

#[salsa::tracked(returns(as_deref))]
pub(crate) fn crate_notable_traits(db: &dyn DefDatabase, krate: Crate) -> Option<Box<[TraitId]>> {
let mut traits = Vec::new();
Expand All @@ -195,7 +216,11 @@ macro_rules! language_item_table {

@non_lang_core_traits:

$( core::$($non_lang_module:ident)::*, $non_lang_trait:ident; )*
$( core::$($non_lang_trait_module:ident)::*, $non_lang_trait:ident; )*

@non_lang_core_macros:

$( core::$($non_lang_macro_module:ident)::*, $non_lang_macro:ident, $non_lang_macro_field:ident; )*
) => {
#[allow(non_snake_case)] // FIXME: Should we remove this?
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
Expand All @@ -207,6 +232,9 @@ macro_rules! language_item_table {
$(
pub $non_lang_trait: Option<TraitId>,
)*
$(
pub $non_lang_macro_field: Option<MacroId>,
)*
}

impl LangItems {
Expand All @@ -218,6 +246,7 @@ macro_rules! language_item_table {
fn merge_prefer_self(&mut self, other: &Self) {
$( self.$lang_item = self.$lang_item.or(other.$lang_item); )*
$( self.$non_lang_trait = self.$non_lang_trait.or(other.$non_lang_trait); )*
$( self.$non_lang_macro_field = self.$non_lang_macro_field.or(other.$non_lang_macro_field); )*
}

fn assign_lang_item(&mut self, name: Symbol, target: LangItemTarget) {
Expand All @@ -233,8 +262,9 @@ macro_rules! language_item_table {
}
}

fn fill_non_lang_core_traits(&mut self, db: &dyn DefDatabase, core_def_map: &DefMap) {
$( self.$non_lang_trait = resolve_core_trait(db, core_def_map, &[ $(sym::$non_lang_module),* ], sym::$non_lang_trait); )*
fn fill_non_lang_core_items(&mut self, db: &dyn DefDatabase, core_def_map: &DefMap) {
$( self.$non_lang_trait = resolve_core_trait(db, core_def_map, &[ $(sym::$non_lang_trait_module),* ], sym::$non_lang_trait); )*
$( self.$non_lang_macro_field = resolve_core_macro(db, core_def_map, &[ $(sym::$non_lang_macro_module),* ], sym::$non_lang_macro); )*
}
}

Expand Down Expand Up @@ -479,4 +509,16 @@ language_item_table! { LangItems =>
core::hash, Hash;
core::cmp, Ord;
core::cmp, Eq;

@non_lang_core_macros:
core::default, Default, DefaultDerive;
core::fmt, Debug, DebugDerive;
core::hash, Hash, HashDerive;
core::cmp, PartialOrd, PartialOrdDerive;
core::cmp, Ord, OrdDerive;
core::cmp, PartialEq, PartialEqDerive;
core::cmp, Eq, EqDerive;
core::marker, CoercePointee, CoercePointeeDerive;
core::marker, Copy, CopyDerive;
core::clone, Clone, CloneDerive;
}
10 changes: 6 additions & 4 deletions crates/hir-def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mod tests;
use std::ops::{Deref, DerefMut, Index, IndexMut};

use base_db::Crate;
use either::Either;
use hir_expand::{
EditionedFileId, ErasedAstId, HirFileId, InFile, MacroCallId, mod_path::ModPath, name::Name,
proc_macro::ProcMacroKind,
Expand All @@ -75,8 +76,8 @@ use triomphe::Arc;
use tt::TextRange;

use crate::{
AstId, BlockId, BlockLoc, ExternCrateId, FunctionId, FxIndexMap, Lookup, MacroCallStyles,
MacroExpander, MacroId, ModuleId, ModuleIdLt, ProcMacroId, UseId,
AstId, BlockId, BlockLoc, BuiltinDeriveImplId, ExternCrateId, FunctionId, FxIndexMap, Lookup,
MacroCallStyles, MacroExpander, MacroId, ModuleId, ModuleIdLt, ProcMacroId, UseId,
db::DefDatabase,
item_scope::{BuiltinShadowMode, ItemScope},
item_tree::TreeId,
Expand Down Expand Up @@ -192,7 +193,8 @@ pub struct DefMap {
/// Tracks which custom derives are in scope for an item, to allow resolution of derive helper
/// attributes.
// FIXME: Figure out a better way for the IDE layer to resolve these?
derive_helpers_in_scope: FxHashMap<AstId<ast::Item>, Vec<(Name, MacroId, MacroCallId)>>,
derive_helpers_in_scope:
FxHashMap<AstId<ast::Item>, Vec<(Name, MacroId, Either<MacroCallId, BuiltinDeriveImplId>)>>,
/// A mapping from [`hir_expand::MacroDefId`] to [`crate::MacroId`].
pub macro_def_to_macro_id: FxHashMap<ErasedAstId, MacroId>,

Expand Down Expand Up @@ -540,7 +542,7 @@ impl DefMap {
pub fn derive_helpers_in_scope(
&self,
id: AstId<ast::Adt>,
) -> Option<&[(Name, MacroId, MacroCallId)]> {
) -> Option<&[(Name, MacroId, Either<MacroCallId, BuiltinDeriveImplId>)]> {
self.derive_helpers_in_scope.get(&id.map(|it| it.upcast())).map(Deref::deref)
}

Expand Down
Loading