Skip to content

Commit ddf105b

Browse files
committed
Auto merge of rust-lang#16527 - Veykril:salsa-no-self-ref, r=Veykril
internal: Remove SELF_REF hack for self referential SyntaxContexts This should reduce the amount of SyntaxContexts we allocate
2 parents 1c32387 + 5136705 commit ddf105b

File tree

21 files changed

+245
-129
lines changed

21 files changed

+245
-129
lines changed

crates/hir-def/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ use std::{
7070
panic::{RefUnwindSafe, UnwindSafe},
7171
};
7272

73-
use base_db::{impl_intern_key, salsa, CrateId, Edition};
73+
use base_db::{
74+
impl_intern_key,
75+
salsa::{self, impl_intern_value_trivial},
76+
CrateId, Edition,
77+
};
7478
use hir_expand::{
7579
ast_id_map::{AstIdNode, FileAstId},
7680
builtin_attr_macro::BuiltinAttrExpander,
@@ -171,6 +175,7 @@ pub trait ItemTreeLoc {
171175
macro_rules! impl_intern {
172176
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
173177
impl_intern_key!($id);
178+
impl_intern_value_trivial!($loc);
174179
impl_intern_lookup!(DefDatabase, $id, $loc, $intern, $lookup);
175180
};
176181
}
@@ -490,6 +495,7 @@ pub struct TypeOrConstParamId {
490495
pub parent: GenericDefId,
491496
pub local_id: LocalTypeOrConstParamId,
492497
}
498+
impl_intern_value_trivial!(TypeOrConstParamId);
493499

494500
/// A TypeOrConstParamId with an invariant that it actually belongs to a type
495501
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -551,6 +557,7 @@ pub struct LifetimeParamId {
551557
pub local_id: LocalLifetimeParamId,
552558
}
553559
pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
560+
impl_intern_value_trivial!(LifetimeParamId);
554561

555562
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
556563
pub enum ItemContainerId {

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ macro_rules! f {
3535
};
3636
}
3737
38-
struct#0:[email protected]#2# MyTraitMap2#0:[email protected]#0# {#0:[email protected]#2#
39-
40-
38+
struct#0:[email protected]#1# MyTraitMap2#0:[email protected]#0# {#0:[email protected]#1#
39+
40+
4141
"#]],
4242
);
4343
}
@@ -171,7 +171,7 @@ fn main(foo: ()) {
171171
}
172172
173173
fn main(foo: ()) {
174-
/* error: unresolved macro unresolved */"helloworld!"#0:[email protected]#6#;
174+
/* error: unresolved macro unresolved */"helloworld!"#0:[email protected]#2#;
175175
}
176176
}
177177
@@ -197,7 +197,7 @@ macro_rules! mk_struct {
197197
#[macro_use]
198198
mod foo;
199199
200-
200+
201201
"#]],
202202
);
203203
}

crates/hir-expand/src/hygiene.rs

+35-36
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
use std::iter;
99

10+
use base_db::salsa::{self, InternValue};
1011
use span::{MacroCallId, Span, SyntaxContextId};
1112

12-
use crate::db::ExpandDatabase;
13+
use crate::db::{ExpandDatabase, InternSyntaxContextQuery};
1314

1415
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
1516
pub struct SyntaxContextData {
@@ -22,6 +23,14 @@ pub struct SyntaxContextData {
2223
pub opaque_and_semitransparent: SyntaxContextId,
2324
}
2425

26+
impl InternValue for SyntaxContextData {
27+
type Key = (SyntaxContextId, Option<MacroCallId>, Transparency);
28+
29+
fn into_key(&self) -> Self::Key {
30+
(self.parent, self.outer_expn, self.outer_transparency)
31+
}
32+
}
33+
2534
impl std::fmt::Debug for SyntaxContextData {
2635
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2736
f.debug_struct("SyntaxContextData")
@@ -149,38 +158,36 @@ fn apply_mark_internal(
149158
transparency: Transparency,
150159
) -> SyntaxContextId {
151160
let syntax_context_data = db.lookup_intern_syntax_context(ctxt);
152-
let mut opaque = handle_self_ref(ctxt, syntax_context_data.opaque);
153-
let mut opaque_and_semitransparent =
154-
handle_self_ref(ctxt, syntax_context_data.opaque_and_semitransparent);
161+
let mut opaque = syntax_context_data.opaque;
162+
let mut opaque_and_semitransparent = syntax_context_data.opaque_and_semitransparent;
155163

156164
if transparency >= Transparency::Opaque {
157165
let parent = opaque;
158-
// Unlike rustc, with salsa we can't prefetch the to be allocated ID to create cycles with
159-
// salsa when interning, so we use a sentinel value that effectively means the current
160-
// syntax context.
161-
let new_opaque = SyntaxContextId::SELF_REF;
162-
opaque = db.intern_syntax_context(SyntaxContextData {
163-
outer_expn: call_id,
164-
outer_transparency: transparency,
165-
parent,
166-
opaque: new_opaque,
167-
opaque_and_semitransparent: new_opaque,
168-
});
166+
opaque = salsa::plumbing::get_query_table::<InternSyntaxContextQuery>(db).get_or_insert(
167+
(parent, call_id, transparency),
168+
|new_opaque| SyntaxContextData {
169+
outer_expn: call_id,
170+
outer_transparency: transparency,
171+
parent,
172+
opaque: new_opaque,
173+
opaque_and_semitransparent: new_opaque,
174+
},
175+
);
169176
}
170177

171178
if transparency >= Transparency::SemiTransparent {
172179
let parent = opaque_and_semitransparent;
173-
// Unlike rustc, with salsa we can't prefetch the to be allocated ID to create cycles with
174-
// salsa when interning, so we use a sentinel value that effectively means the current
175-
// syntax context.
176-
let new_opaque_and_semitransparent = SyntaxContextId::SELF_REF;
177-
opaque_and_semitransparent = db.intern_syntax_context(SyntaxContextData {
178-
outer_expn: call_id,
179-
outer_transparency: transparency,
180-
parent,
181-
opaque,
182-
opaque_and_semitransparent: new_opaque_and_semitransparent,
183-
});
180+
opaque_and_semitransparent =
181+
salsa::plumbing::get_query_table::<InternSyntaxContextQuery>(db).get_or_insert(
182+
(parent, call_id, transparency),
183+
|new_opaque_and_semitransparent| SyntaxContextData {
184+
outer_expn: call_id,
185+
outer_transparency: transparency,
186+
parent,
187+
opaque,
188+
opaque_and_semitransparent: new_opaque_and_semitransparent,
189+
},
190+
);
184191
}
185192

186193
let parent = ctxt;
@@ -201,20 +208,12 @@ pub trait SyntaxContextExt {
201208
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)>;
202209
}
203210

204-
#[inline(always)]
205-
fn handle_self_ref(p: SyntaxContextId, n: SyntaxContextId) -> SyntaxContextId {
206-
match n {
207-
SyntaxContextId::SELF_REF => p,
208-
_ => n,
209-
}
210-
}
211-
212211
impl SyntaxContextExt for SyntaxContextId {
213212
fn normalize_to_macro_rules(self, db: &dyn ExpandDatabase) -> Self {
214-
handle_self_ref(self, db.lookup_intern_syntax_context(self).opaque_and_semitransparent)
213+
db.lookup_intern_syntax_context(self).opaque_and_semitransparent
215214
}
216215
fn normalize_to_macros_2_0(self, db: &dyn ExpandDatabase) -> Self {
217-
handle_self_ref(self, db.lookup_intern_syntax_context(self).opaque)
216+
db.lookup_intern_syntax_context(self).opaque
218217
}
219218
fn parent_ctxt(self, db: &dyn ExpandDatabase) -> Self {
220219
db.lookup_intern_syntax_context(self).parent

crates/hir-expand/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use triomphe::Arc;
3030

3131
use std::{fmt, hash::Hash};
3232

33-
use base_db::{CrateId, Edition, FileId};
33+
use base_db::{salsa::impl_intern_value_trivial, CrateId, Edition, FileId};
3434
use either::Either;
3535
use span::{FileRange, HirFileIdRepr, Span, SyntaxContextId};
3636
use syntax::{
@@ -176,6 +176,7 @@ pub struct MacroCallLoc {
176176
pub kind: MacroCallKind,
177177
pub call_site: Span,
178178
}
179+
impl_intern_value_trivial!(MacroCallLoc);
179180

180181
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
181182
pub struct MacroDefId {

crates/hir-ty/src/chalk_db.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use hir_def::{
1717
use hir_expand::name::name;
1818

1919
use crate::{
20-
db::HirDatabase,
20+
db::{HirDatabase, InternedCoroutine},
2121
display::HirDisplay,
2222
from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, make_binders,
2323
make_single_type_binders,
@@ -428,7 +428,7 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
428428
&self,
429429
id: chalk_ir::CoroutineId<Interner>,
430430
) -> Arc<chalk_solve::rust_ir::CoroutineDatum<Interner>> {
431-
let (parent, expr) = self.db.lookup_intern_coroutine(id.into());
431+
let InternedCoroutine(parent, expr) = self.db.lookup_intern_coroutine(id.into());
432432

433433
// We fill substitution with unknown type, because we only need to know whether the generic
434434
// params are types or consts to build `Binders` and those being filled up are for
@@ -473,7 +473,7 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
473473
let inner_types =
474474
rust_ir::CoroutineWitnessExistential { types: wrap_empty_binders(vec![]) };
475475

476-
let (parent, _) = self.db.lookup_intern_coroutine(id.into());
476+
let InternedCoroutine(parent, _) = self.db.lookup_intern_coroutine(id.into());
477477
// See the comment in `coroutine_datum()` for unknown types.
478478
let subst = TyBuilder::subst_for_coroutine(self.db, parent).fill_with_unknown().build();
479479
let it = subst

crates/hir-ty/src/db.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
44
use std::sync;
55

6-
use base_db::{impl_intern_key, salsa, CrateId, Upcast};
6+
use base_db::{
7+
impl_intern_key,
8+
salsa::{self, impl_intern_value_trivial},
9+
CrateId, Upcast,
10+
};
711
use hir_def::{
812
db::DefDatabase, hir::ExprId, layout::TargetDataLayout, AdtId, BlockId, ConstParamId,
913
DefWithBodyId, EnumVariantId, FunctionId, GeneralConstId, GenericDefId, ImplId,
@@ -199,9 +203,9 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
199203
#[salsa::interned]
200204
fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId;
201205
#[salsa::interned]
202-
fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> InternedClosureId;
206+
fn intern_closure(&self, id: InternedClosure) -> InternedClosureId;
203207
#[salsa::interned]
204-
fn intern_coroutine(&self, id: (DefWithBodyId, ExprId)) -> InternedCoroutineId;
208+
fn intern_coroutine(&self, id: InternedCoroutine) -> InternedCoroutineId;
205209

206210
#[salsa::invoke(chalk_db::associated_ty_data_query)]
207211
fn associated_ty_data(
@@ -337,10 +341,18 @@ impl_intern_key!(InternedOpaqueTyId);
337341
pub struct InternedClosureId(salsa::InternId);
338342
impl_intern_key!(InternedClosureId);
339343

344+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
345+
pub struct InternedClosure(pub DefWithBodyId, pub ExprId);
346+
impl_intern_value_trivial!(InternedClosure);
347+
340348
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
341349
pub struct InternedCoroutineId(salsa::InternId);
342350
impl_intern_key!(InternedCoroutineId);
343351

352+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
353+
pub struct InternedCoroutine(pub DefWithBodyId, pub ExprId);
354+
impl_intern_value_trivial!(InternedCoroutine);
355+
344356
/// This exists just for Chalk, because Chalk just has a single `FnDefId` where
345357
/// we have different IDs for struct and enum variant constructors.
346358
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]

crates/hir-ty/src/display.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use triomphe::Arc;
3232

3333
use crate::{
3434
consteval::try_const_usize,
35-
db::HirDatabase,
35+
db::{HirDatabase, InternedClosure},
3636
from_assoc_type_id, from_foreign_def_id, from_placeholder_idx,
3737
layout::Layout,
3838
lt_from_placeholder_idx,
@@ -1085,7 +1085,7 @@ impl HirDisplay for Ty {
10851085
}
10861086
let sig = ClosureSubst(substs).sig_ty().callable_sig(db);
10871087
if let Some(sig) = sig {
1088-
let (def, _) = db.lookup_intern_closure((*id).into());
1088+
let InternedClosure(def, _) = db.lookup_intern_closure((*id).into());
10891089
let infer = db.infer(def);
10901090
let (_, kind) = infer.closure_info(id);
10911091
match f.closure_style {

crates/hir-ty/src/infer/closure.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use smallvec::SmallVec;
2121
use stdx::never;
2222

2323
use crate::{
24-
db::HirDatabase,
24+
db::{HirDatabase, InternedClosure},
2525
from_placeholder_idx, make_binders,
2626
mir::{BorrowKind, MirSpan, ProjectionElem},
2727
static_lifetime, to_chalk_trait_id,
@@ -716,7 +716,7 @@ impl InferenceContext<'_> {
716716

717717
fn is_upvar(&self, place: &HirPlace) -> bool {
718718
if let Some(c) = self.current_closure {
719-
let (_, root) = self.db.lookup_intern_closure(c.into());
719+
let InternedClosure(_, root) = self.db.lookup_intern_closure(c.into());
720720
return self.body.is_binding_upvar(place.local, root);
721721
}
722722
false
@@ -938,7 +938,7 @@ impl InferenceContext<'_> {
938938
}
939939

940940
fn analyze_closure(&mut self, closure: ClosureId) -> FnTrait {
941-
let (_, root) = self.db.lookup_intern_closure(closure.into());
941+
let InternedClosure(_, root) = self.db.lookup_intern_closure(closure.into());
942942
self.current_closure = Some(closure);
943943
let Expr::Closure { body, capture_by, .. } = &self.body[root] else {
944944
unreachable!("Closure expression id is always closure");

crates/hir-ty/src/infer/expr.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use syntax::ast::RangeOp;
2323
use crate::{
2424
autoderef::{builtin_deref, deref_by_trait, Autoderef},
2525
consteval,
26+
db::{InternedClosure, InternedCoroutine},
2627
infer::{
2728
coerce::{CoerceMany, CoercionCause},
2829
find_continuable,
@@ -253,13 +254,17 @@ impl InferenceContext<'_> {
253254
.push(ret_ty.clone())
254255
.build();
255256

256-
let coroutine_id = self.db.intern_coroutine((self.owner, tgt_expr)).into();
257+
let coroutine_id = self
258+
.db
259+
.intern_coroutine(InternedCoroutine(self.owner, tgt_expr))
260+
.into();
257261
let coroutine_ty = TyKind::Coroutine(coroutine_id, subst).intern(Interner);
258262

259263
(None, coroutine_ty, Some((resume_ty, yield_ty)))
260264
}
261265
ClosureKind::Closure | ClosureKind::Async => {
262-
let closure_id = self.db.intern_closure((self.owner, tgt_expr)).into();
266+
let closure_id =
267+
self.db.intern_closure(InternedClosure(self.owner, tgt_expr)).into();
263268
let closure_ty = TyKind::Closure(
264269
closure_id,
265270
TyBuilder::subst_for_closure(self.db, self.owner, sig_ty.clone()),

crates/hir-ty/src/layout.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ use stdx::never;
1919
use triomphe::Arc;
2020

2121
use crate::{
22-
consteval::try_const_usize, db::HirDatabase, infer::normalize, layout::adt::struct_variant_idx,
23-
utils::ClosureSubst, Interner, ProjectionTy, Substitution, TraitEnvironment, Ty,
22+
consteval::try_const_usize,
23+
db::{HirDatabase, InternedClosure},
24+
infer::normalize,
25+
layout::adt::struct_variant_idx,
26+
utils::ClosureSubst,
27+
Interner, ProjectionTy, Substitution, TraitEnvironment, Ty,
2428
};
2529

2630
pub use self::{
@@ -391,7 +395,7 @@ pub fn layout_of_ty_query(
391395
}
392396
}
393397
TyKind::Closure(c, subst) => {
394-
let (def, _) = db.lookup_intern_closure((*c).into());
398+
let InternedClosure(def, _) = db.lookup_intern_closure((*c).into());
395399
let infer = db.infer(def);
396400
let (captures, _) = infer.closure_info(c);
397401
let fields = captures

crates/hir-ty/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use std::{
5151
hash::{BuildHasherDefault, Hash},
5252
};
5353

54+
use base_db::salsa::impl_intern_value_trivial;
5455
use chalk_ir::{
5556
fold::{Shift, TypeFoldable},
5657
interner::HasInterner,
@@ -586,6 +587,7 @@ pub enum ImplTraitId {
586587
ReturnTypeImplTrait(hir_def::FunctionId, RpitId),
587588
AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
588589
}
590+
impl_intern_value_trivial!(ImplTraitId);
589591

590592
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
591593
pub struct ReturnTypeImplTraits {

0 commit comments

Comments
 (0)