Skip to content

Commit c291d7b

Browse files
committed
add lifetime to BuiltinDeriveMacroId
1 parent 34cb163 commit c291d7b

108 files changed

Lines changed: 1120 additions & 1039 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/hir-def/src/builtin_derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl BuiltinDeriveImplMethod {
111111
pub fn trait_method(
112112
self,
113113
db: &dyn SourceDatabase,
114-
impl_: BuiltinDeriveImplId,
114+
impl_: BuiltinDeriveImplId<'_>,
115115
) -> Option<FunctionId> {
116116
let loc = impl_.loc(db);
117117
let lang_items = crate::lang_item::lang_items(db, loc.krate(db));

crates/hir-def/src/dyn_map.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ pub mod keys {
8686
pub const ATTR_MACRO_CALL<ast::Item, MacroCallId>;
8787
pub const DERIVE_MACRO_CALL<
8888
ast::Meta,
89-
(
89+
for<'db> (
9090
AttrId,
9191
/* derive() */ MacroCallId,
9292
/* actual derive macros */
93-
Box<[Option<Either<MacroCallId, BuiltinDeriveImplId>>]>,
93+
Box<[Option<Either<MacroCallId, BuiltinDeriveImplId<'db>>>]>,
9494
),
9595
>;
9696
}
@@ -210,35 +210,3 @@ where
210210
unsafe { std::mem::transmute::<&mut DynMap<'db>, &mut KeyMap<'db, Key<K, V>>>(self) }
211211
}
212212
}
213-
214-
#[cfg(test)]
215-
mod tests {
216-
use test_fixture::WithFixture;
217-
218-
use syntax::{
219-
AstPtr,
220-
ast::{self, make},
221-
};
222-
223-
use crate::{ModuleIdLt, test_db::TestDB};
224-
225-
use super::{DynMap, Key, ValueTrait};
226-
227-
const MODULE: Key<ast::Module, dyn for<'db> ValueTrait<'db, Output = ModuleIdLt<'db>>> =
228-
Key::new();
229-
230-
#[test]
231-
fn lifetime_key_returns_database_bound_id() {
232-
let (db, file_id) = TestDB::with_single_file("");
233-
let module = make::mod_(make::name("foo"), None);
234-
let module = AstPtr::new(&module);
235-
let module_id = db.module_for_file(file_id.file_id(&db));
236-
let module_id = unsafe { module_id.to_db(&db) };
237-
let mut map = DynMap::default();
238-
239-
map[MODULE].insert(module, module_id);
240-
241-
let stored = map[MODULE].get(&module).unwrap();
242-
assert_eq!(*stored, module_id);
243-
}
244-
}

crates/hir-def/src/expr_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ impl ExpressionStore {
587587
pub fn blocks<'a>(
588588
&'a self,
589589
db: &'a dyn SourceDatabase,
590-
) -> impl Iterator<Item = (BlockId, &'a DefMap)> {
590+
) -> impl Iterator<Item = (BlockId, &'a DefMap<'a>)> {
591591
self.expr_only
592592
.as_ref()
593593
.map(|it| &*it.block_scopes)

crates/hir-def/src/expr_store/expander.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'db> Expander<'db> {
3333
pub(super) fn new(
3434
db: &'db dyn SourceDatabase,
3535
current_file_id: HirFileId,
36-
def_map: &DefMap,
36+
def_map: &DefMap<'_>,
3737
) -> Expander<'db> {
3838
let recursion_limit = def_map.recursion_limit();
3939
let recursion_limit = if cfg!(test) {

crates/hir-def/src/expr_store/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub struct ExprCollector<'db> {
451451
db: &'db dyn SourceDatabase,
452452
cfg_options: &'db CfgOptions,
453453
expander: Expander<'db>,
454-
def_map: &'db DefMap,
454+
def_map: &'db DefMap<'db>,
455455
local_def_map: &'db LocalDefMap,
456456
module: ModuleId,
457457
lowering_mode: LoweringMode,

crates/hir-def/src/find_path.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct FindPathCtx<'db> {
124124
from: ModuleIdLt<'db>,
125125
from_crate: Crate,
126126
crate_root: ModuleIdLt<'db>,
127-
from_def_map: &'db DefMap,
127+
from_def_map: &'db DefMap<'db>,
128128
fuel: Cell<usize>,
129129
}
130130

@@ -271,7 +271,7 @@ fn find_path_for_module<'db>(
271271

272272
fn find_in_scope<'db>(
273273
db: &'db dyn SourceDatabase,
274-
def_map: &DefMap,
274+
def_map: &DefMap<'db>,
275275
from: ModuleIdLt<'db>,
276276
item: ItemInNs,
277277
ignore_local_imports: bool,
@@ -286,11 +286,11 @@ fn find_in_scope<'db>(
286286

287287
/// Returns single-segment path (i.e. without any prefix) if `item` is found in prelude and its
288288
/// name doesn't clash in current scope.
289-
fn find_in_prelude(
290-
db: &dyn SourceDatabase,
291-
local_def_map: &DefMap,
289+
fn find_in_prelude<'db>(
290+
db: &'db dyn SourceDatabase,
291+
local_def_map: &DefMap<'db>,
292292
item: ItemInNs,
293-
from: ModuleIdLt<'_>,
293+
from: ModuleIdLt<'db>,
294294
) -> Option<Choice> {
295295
let (prelude_module, _) = local_def_map.prelude()?;
296296
let prelude_def_map = prelude_module.def_map(db);
@@ -321,7 +321,7 @@ fn find_in_prelude(
321321

322322
fn is_kw_kind_relative_to_from(
323323
db: &dyn SourceDatabase,
324-
def_map: &DefMap,
324+
def_map: &DefMap<'_>,
325325
item: ModuleIdLt<'_>,
326326
from: ModuleIdLt<'_>,
327327
) -> Option<PathKind> {

crates/hir-def/src/item_scope.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use indexmap::map::Entry;
1010
use itertools::Itertools;
1111
use la_arena::Idx;
1212
use rustc_hash::{FxHashMap, FxHashSet};
13+
use salsa::SalsaValue;
1314
use smallvec::SmallVec;
1415
use span::Edition;
1516
use stdx::{format_to, impl_from};
@@ -121,8 +122,8 @@ impl PerNsGlobImports {
121122
}
122123
}
123124

124-
#[derive(Debug, Default, PartialEq, Eq)]
125-
pub struct ItemScope {
125+
#[derive(Debug, Default, PartialEq, Eq, SalsaValue)]
126+
pub struct ItemScope<'db> {
126127
/// Defs visible in this scope. This includes `declarations`, but also
127128
/// imports. The imports belong to this module and can be resolved by using them on
128129
/// the `use_imports_*` fields.
@@ -136,7 +137,7 @@ pub struct ItemScope {
136137
declarations: ThinVec<ModuleDefId>,
137138

138139
impls: ThinVec<(ImplId, /* trait impl */ bool)>,
139-
builtin_derive_impls: ThinVec<BuiltinDeriveImplId>,
140+
builtin_derive_impls: ThinVec<BuiltinDeriveImplId<'db>>,
140141
extern_blocks: ThinVec<ExternBlockId>,
141142
unnamed_consts: ThinVec<ConstId>,
142143
/// Traits imported via `use Trait as _;`.
@@ -168,15 +169,15 @@ pub struct ItemScope {
168169
macro_invocations: FxHashMap<AstId<ast::MacroCall>, MacroCallId>,
169170
/// The derive macro invocations in this scope, keyed by the owner item over the actual derive attributes
170171
/// paired with the derive macro invocations for the specific attribute.
171-
derive_macros: FxHashMap<AstId<ast::Adt>, SmallVec<[DeriveMacroInvocation; 1]>>,
172+
derive_macros: FxHashMap<AstId<ast::Adt>, SmallVec<[DeriveMacroInvocation<'db>; 1]>>,
172173
}
173174

174-
#[derive(Debug, PartialEq, Eq)]
175-
struct DeriveMacroInvocation {
175+
#[derive(Debug, PartialEq, Eq, SalsaValue)]
176+
struct DeriveMacroInvocation<'db> {
176177
attr_id: AttrId,
177178
/// The `#[derive]` call
178179
attr_call_id: MacroCallId,
179-
derive_call_ids: SmallVec<[Option<Either<MacroCallId, BuiltinDeriveImplId>>; 4]>,
180+
derive_call_ids: SmallVec<[Option<Either<MacroCallId, BuiltinDeriveImplId<'db>>>; 4]>,
180181
}
181182

182183
pub(crate) static BUILTIN_SCOPE: LazyLock<FxIndexMap<Name, PerNs>> = LazyLock::new(|| {
@@ -197,7 +198,7 @@ pub(crate) enum BuiltinShadowMode {
197198

198199
/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
199200
/// Other methods will only resolve values, types and module scoped macros only.
200-
impl ItemScope {
201+
impl<'db> ItemScope<'db> {
201202
pub fn entries(&self) -> impl Iterator<Item = (&Name, PerNs)> {
202203
// FIXME: shadowing
203204
self.types
@@ -234,7 +235,7 @@ impl ItemScope {
234235
.dedup()
235236
}
236237

237-
pub fn fully_resolve_import(&self, db: &dyn SourceDatabase, mut import: ImportId) -> PerNs {
238+
pub fn fully_resolve_import(&self, db: &'db dyn SourceDatabase, mut import: ImportId) -> PerNs {
238239
let mut res = PerNs::none();
239240

240241
let mut scope = self;
@@ -313,7 +314,7 @@ impl ItemScope {
313314
self.impls.iter().filter(|&&(_, is_trait_impl)| !is_trait_impl).map(|&(id, _)| id)
314315
}
315316

316-
pub fn builtin_derive_impls(&self) -> impl ExactSizeIterator<Item = BuiltinDeriveImplId> {
317+
pub fn builtin_derive_impls(&self) -> impl ExactSizeIterator<Item = BuiltinDeriveImplId<'db>> {
317318
self.builtin_derive_impls.iter().copied()
318319
}
319320

@@ -452,7 +453,7 @@ impl ItemScope {
452453
}
453454
}
454455

455-
impl ItemScope {
456+
impl<'db> ItemScope<'db> {
456457
pub(crate) fn declare(&mut self, def: ModuleDefId) {
457458
self.declarations.push(def)
458459
}
@@ -473,7 +474,7 @@ impl ItemScope {
473474
self.impls.push((imp, is_trait_impl));
474475
}
475476

476-
pub(crate) fn define_builtin_derive_impl(&mut self, imp: BuiltinDeriveImplId) {
477+
pub(crate) fn define_builtin_derive_impl(&mut self, imp: BuiltinDeriveImplId<'db>) {
477478
self.builtin_derive_impls.push(imp);
478479
}
479480

@@ -508,7 +509,7 @@ impl ItemScope {
508509
pub(crate) fn set_derive_macro_invoc(
509510
&mut self,
510511
adt: AstId<ast::Adt>,
511-
call: Either<MacroCallId, BuiltinDeriveImplId>,
512+
call: Either<MacroCallId, BuiltinDeriveImplId<'db>>,
512513
id: AttrId,
513514
idx: usize,
514515
) {
@@ -528,7 +529,7 @@ impl ItemScope {
528529
adt: AstId<ast::Adt>,
529530
attr_id: AttrId,
530531
attr_call_id: MacroCallId,
531-
mut derive_call_ids: SmallVec<[Option<Either<MacroCallId, BuiltinDeriveImplId>>; 4]>,
532+
mut derive_call_ids: SmallVec<[Option<Either<MacroCallId, BuiltinDeriveImplId<'db>>>; 4]>,
532533
) {
533534
derive_call_ids.shrink_to_fit();
534535
self.derive_macros.entry(adt).or_default().push(DeriveMacroInvocation {
@@ -544,7 +545,11 @@ impl ItemScope {
544545
Item = (
545546
AstId<ast::Adt>,
546547
impl Iterator<
547-
Item = (AttrId, MacroCallId, &[Option<Either<MacroCallId, BuiltinDeriveImplId>>]),
548+
Item = (
549+
AttrId,
550+
MacroCallId,
551+
&[Option<Either<MacroCallId, BuiltinDeriveImplId<'db>>>],
552+
),
548553
>,
549554
),
550555
> + '_ {
@@ -863,7 +868,7 @@ impl ItemScope {
863868
}
864869

865870
// These methods are a temporary measure only meant to be used by `DefCollector::push_res_and_update_glob_vis()`.
866-
impl ItemScope {
871+
impl<'db> ItemScope<'db> {
867872
pub(crate) fn update_visibility_types(&mut self, name: &Name, vis: Visibility) {
868873
let res =
869874
self.types.get_mut(name).expect("tried to update visibility of non-existent type");

crates/hir-def/src/lang_item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl LangItems {
151151

152152
fn resolve_core_trait(
153153
db: &dyn SourceDatabase,
154-
core_def_map: &DefMap,
154+
core_def_map: &DefMap<'_>,
155155
modules: &[Symbol],
156156
name: Symbol,
157157
) -> Option<TraitId> {
@@ -176,7 +176,7 @@ fn resolve_core_trait(
176176

177177
fn resolve_core_macro(
178178
db: &dyn SourceDatabase,
179-
core_def_map: &DefMap,
179+
core_def_map: &DefMap<'_>,
180180
modules: &[Symbol],
181181
name: Symbol,
182182
) -> Option<MacroId> {
@@ -397,7 +397,7 @@ macro_rules! language_item_table {
397397
}
398398
}
399399

400-
fn fill_non_lang_core_items(&mut self, db: &dyn SourceDatabase, core_def_map: &DefMap) {
400+
fn fill_non_lang_core_items(&mut self, db: &dyn SourceDatabase, core_def_map: &DefMap<'_>) {
401401
$( self.$non_lang_trait = resolve_core_trait(db, core_def_map, &[ $(sym::$non_lang_trait_module),* ], sym::$non_lang_trait); )*
402402
$( self.$non_lang_macro_field = resolve_core_macro(db, core_def_map, &[ $(sym::$non_lang_macro_module),* ], sym::$non_lang_macro); )*
403403
}

crates/hir-def/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ pub struct BuiltinDeriveImplLoc {
354354
pub derive_index: u32,
355355
}
356356

357-
#[salsa::interned(debug, unsafe(no_lifetime), revisions = usize::MAX)]
357+
#[salsa::interned(debug, revisions = usize::MAX)]
358358
#[derive(PartialOrd, Ord)]
359359
pub struct BuiltinDeriveImplId {
360360
#[returns(ref)]
@@ -564,7 +564,7 @@ impl<'db> ModuleIdLt<'db> {
564564
unsafe { std::mem::transmute(self) }
565565
}
566566

567-
pub fn def_map(self, db: &'db dyn SourceDatabase) -> &'db DefMap {
567+
pub fn def_map(self, db: &'db dyn SourceDatabase) -> &'db DefMap<'db> {
568568
match self.block(db) {
569569
Some(block) => block_def_map(db, block),
570570
None => crate_def_map(db, self.krate(db)),
@@ -574,7 +574,7 @@ impl<'db> ModuleIdLt<'db> {
574574
pub(crate) fn local_def_map(
575575
self,
576576
db: &'db dyn SourceDatabase,
577-
) -> (&'db DefMap, &'db LocalDefMap) {
577+
) -> (&'db DefMap<'db>, &'db LocalDefMap) {
578578
match self.block(db) {
579579
Some(block) => (block_def_map(db, block), self.only_local_def_map(db)),
580580
None => {
@@ -588,7 +588,7 @@ impl<'db> ModuleIdLt<'db> {
588588
crate_local_def_map(db, self.krate(db)).local(db)
589589
}
590590

591-
pub fn crate_def_map(self, db: &'db dyn SourceDatabase) -> &'db DefMap {
591+
pub fn crate_def_map(self, db: &'db dyn SourceDatabase) -> &'db DefMap<'db> {
592592
crate_def_map(db, self.krate(db))
593593
}
594594

@@ -742,7 +742,9 @@ pub enum AdtId {
742742
impl_from!(StructId, UnionId, EnumId for AdtId);
743743

744744
/// A macro
745-
#[derive(Debug, PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash, salsa::Supertype)]
745+
#[derive(
746+
Debug, PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash, salsa::Supertype, salsa::SalsaValue,
747+
)]
746748
pub enum MacroId {
747749
Macro2Id(Macro2Id),
748750
MacroRulesId(MacroRulesId),
@@ -1241,7 +1243,7 @@ impl HasModule for BuiltinDeriveImplLoc {
12411243
}
12421244
}
12431245

1244-
impl HasModule for BuiltinDeriveImplId {
1246+
impl HasModule for BuiltinDeriveImplId<'_> {
12451247
#[inline]
12461248
fn module(&self, db: &dyn SourceDatabase) -> ModuleId {
12471249
self.loc(db).module(db)

crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
260260

261261
fn resolve_macro_call_id(
262262
db: &dyn SourceDatabase,
263-
def_map: &DefMap,
263+
def_map: &DefMap<'_>,
264264
ast_id: AstId<ast::MacroCall>,
265265
ast_ptr: InFile<AstPtr<ast::MacroCall>>,
266266
) -> Option<MacroCallId> {

0 commit comments

Comments
 (0)