Skip to content

Commit 2d5ce88

Browse files
committed
Auto merge of rust-lang#16366 - Veykril:transp-queries, r=Veykril
internal: Make data queries transparent over their diagnostics variant And a few other QoL things
2 parents c9afd41 + 35e05e0 commit 2d5ce88

File tree

17 files changed

+329
-157
lines changed

17 files changed

+329
-157
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ smallvec = { version = "1.10.0", features = [
122122
"union",
123123
"const_generics",
124124
] }
125-
smol_str = "0.2.0"
125+
smol_str = "0.2.1"
126126
text-size = "1.1.1"
127127
tracing = "0.1.40"
128128
tracing-tree = "0.3.0"

crates/hir-def/src/data.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ pub struct TraitData {
233233
}
234234

235235
impl TraitData {
236+
#[inline]
236237
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
237238
db.trait_data_with_diagnostics(tr).0
238239
}
@@ -241,12 +242,9 @@ impl TraitData {
241242
db: &dyn DefDatabase,
242243
tr: TraitId,
243244
) -> (Arc<TraitData>, DefDiagnostics) {
244-
let tr_loc @ ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
245+
let ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
245246
let item_tree = tree_id.item_tree(db);
246247
let tr_def = &item_tree[tree_id.value];
247-
let _cx = stdx::panic_context::enter(format!(
248-
"trait_data_query({tr:?} -> {tr_loc:?} -> {tr_def:?})"
249-
));
250248
let name = tr_def.name.clone();
251249
let is_auto = tr_def.is_auto;
252250
let is_unsafe = tr_def.is_unsafe;
@@ -333,6 +331,7 @@ pub struct ImplData {
333331
}
334332

335333
impl ImplData {
334+
#[inline]
336335
pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
337336
db.impl_data_with_diagnostics(id).0
338337
}

crates/hir-def/src/data/adt.rs

+3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ fn parse_repr_tt(tt: &Subtree) -> Option<ReprOptions> {
180180
}
181181

182182
impl StructData {
183+
#[inline]
183184
pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc<StructData> {
184185
db.struct_data_with_diagnostics(id).0
185186
}
@@ -236,6 +237,7 @@ impl StructData {
236237
)
237238
}
238239

240+
#[inline]
239241
pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc<StructData> {
240242
db.union_data_with_diagnostics(id).0
241243
}
@@ -322,6 +324,7 @@ impl EnumData {
322324
}
323325

324326
impl EnumVariantData {
327+
#[inline]
325328
pub(crate) fn enum_variant_data_query(
326329
db: &dyn DefDatabase,
327330
e: EnumVariantId,

crates/hir-def/src/db.rs

+5
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
117117

118118
// region:data
119119

120+
#[salsa::transparent]
120121
#[salsa::invoke(StructData::struct_data_query)]
121122
fn struct_data(&self, id: StructId) -> Arc<StructData>;
122123

123124
#[salsa::invoke(StructData::struct_data_with_diagnostics_query)]
124125
fn struct_data_with_diagnostics(&self, id: StructId) -> (Arc<StructData>, DefDiagnostics);
125126

127+
#[salsa::transparent]
126128
#[salsa::invoke(StructData::union_data_query)]
127129
fn union_data(&self, id: UnionId) -> Arc<StructData>;
128130

@@ -132,6 +134,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
132134
#[salsa::invoke(EnumData::enum_data_query)]
133135
fn enum_data(&self, e: EnumId) -> Arc<EnumData>;
134136

137+
#[salsa::transparent]
135138
#[salsa::invoke(EnumVariantData::enum_variant_data_query)]
136139
fn enum_variant_data(&self, id: EnumVariantId) -> Arc<EnumVariantData>;
137140

@@ -141,12 +144,14 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
141144
id: EnumVariantId,
142145
) -> (Arc<EnumVariantData>, DefDiagnostics);
143146

147+
#[salsa::transparent]
144148
#[salsa::invoke(ImplData::impl_data_query)]
145149
fn impl_data(&self, e: ImplId) -> Arc<ImplData>;
146150

147151
#[salsa::invoke(ImplData::impl_data_with_diagnostics_query)]
148152
fn impl_data_with_diagnostics(&self, e: ImplId) -> (Arc<ImplData>, DefDiagnostics);
149153

154+
#[salsa::transparent]
150155
#[salsa::invoke(TraitData::trait_data_query)]
151156
fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
152157

crates/hir-def/src/item_tree/pretty.rs

+65-29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
use std::fmt::{self, Write};
44

5+
use span::ErasedFileAstId;
6+
57
use crate::{
68
generics::{TypeOrConstParamData, WherePredicate, WherePredicateTypeTarget},
79
pretty::{print_path, print_type_bounds, print_type_ref},
@@ -118,7 +120,11 @@ impl Printer<'_> {
118120
w!(self, "{{");
119121
self.indented(|this| {
120122
for field in fields.clone() {
121-
let Field { visibility, name, type_ref, ast_id: _ } = &this.tree[field];
123+
let Field { visibility, name, type_ref, ast_id } = &this.tree[field];
124+
this.print_ast_id(match ast_id {
125+
FieldAstId::Record(it) => it.erase(),
126+
FieldAstId::Tuple(it) => it.erase(),
127+
});
122128
this.print_attrs_of(field, "\n");
123129
this.print_visibility(*visibility);
124130
w!(this, "{}: ", name.display(self.db.upcast()));
@@ -132,7 +138,11 @@ impl Printer<'_> {
132138
w!(self, "(");
133139
self.indented(|this| {
134140
for field in fields.clone() {
135-
let Field { visibility, name, type_ref, ast_id: _ } = &this.tree[field];
141+
let Field { visibility, name, type_ref, ast_id } = &this.tree[field];
142+
this.print_ast_id(match ast_id {
143+
FieldAstId::Record(it) => it.erase(),
144+
FieldAstId::Tuple(it) => it.erase(),
145+
});
136146
this.print_attrs_of(field, "\n");
137147
this.print_visibility(*visibility);
138148
w!(this, "{}: ", name.display(self.db.upcast()));
@@ -200,14 +210,16 @@ impl Printer<'_> {
200210

201211
match item {
202212
ModItem::Use(it) => {
203-
let Use { visibility, use_tree, ast_id: _ } = &self.tree[it];
213+
let Use { visibility, use_tree, ast_id } = &self.tree[it];
214+
self.print_ast_id(ast_id.erase());
204215
self.print_visibility(*visibility);
205216
w!(self, "use ");
206217
self.print_use_tree(use_tree);
207218
wln!(self, ";");
208219
}
209220
ModItem::ExternCrate(it) => {
210-
let ExternCrate { name, alias, visibility, ast_id: _ } = &self.tree[it];
221+
let ExternCrate { name, alias, visibility, ast_id } = &self.tree[it];
222+
self.print_ast_id(ast_id.erase());
211223
self.print_visibility(*visibility);
212224
w!(self, "extern crate {}", name.display(self.db.upcast()));
213225
if let Some(alias) = alias {
@@ -216,7 +228,8 @@ impl Printer<'_> {
216228
wln!(self, ";");
217229
}
218230
ModItem::ExternBlock(it) => {
219-
let ExternBlock { abi, ast_id: _, children } = &self.tree[it];
231+
let ExternBlock { abi, ast_id, children } = &self.tree[it];
232+
self.print_ast_id(ast_id.erase());
220233
w!(self, "extern ");
221234
if let Some(abi) = abi {
222235
w!(self, "\"{}\" ", abi);
@@ -237,9 +250,10 @@ impl Printer<'_> {
237250
abi,
238251
params,
239252
ret_type,
240-
ast_id: _,
253+
ast_id,
241254
flags,
242255
} = &self.tree[it];
256+
self.print_ast_id(ast_id.erase());
243257
self.print_visibility(*visibility);
244258
if flags.contains(FnFlags::HAS_DEFAULT_KW) {
245259
w!(self, "default ");
@@ -263,7 +277,12 @@ impl Printer<'_> {
263277
self.indented(|this| {
264278
for param in params.clone() {
265279
this.print_attrs_of(param, "\n");
266-
match &this.tree[param].type_ref {
280+
let Param { type_ref, ast_id } = &this.tree[param];
281+
this.print_ast_id(match ast_id {
282+
ParamAstId::Param(it) => it.erase(),
283+
ParamAstId::SelfParam(it) => it.erase(),
284+
});
285+
match type_ref {
267286
Some(ty) => {
268287
if flags.contains(FnFlags::HAS_SELF_PARAM) {
269288
w!(this, "self: ");
@@ -288,7 +307,8 @@ impl Printer<'_> {
288307
}
289308
}
290309
ModItem::Struct(it) => {
291-
let Struct { visibility, name, fields, generic_params, ast_id: _ } = &self.tree[it];
310+
let Struct { visibility, name, fields, generic_params, ast_id } = &self.tree[it];
311+
self.print_ast_id(ast_id.erase());
292312
self.print_visibility(*visibility);
293313
w!(self, "struct {}", name.display(self.db.upcast()));
294314
self.print_generic_params(generic_params);
@@ -300,7 +320,8 @@ impl Printer<'_> {
300320
}
301321
}
302322
ModItem::Union(it) => {
303-
let Union { name, visibility, fields, generic_params, ast_id: _ } = &self.tree[it];
323+
let Union { name, visibility, fields, generic_params, ast_id } = &self.tree[it];
324+
self.print_ast_id(ast_id.erase());
304325
self.print_visibility(*visibility);
305326
w!(self, "union {}", name.display(self.db.upcast()));
306327
self.print_generic_params(generic_params);
@@ -312,14 +333,16 @@ impl Printer<'_> {
312333
}
313334
}
314335
ModItem::Enum(it) => {
315-
let Enum { name, visibility, variants, generic_params, ast_id: _ } = &self.tree[it];
336+
let Enum { name, visibility, variants, generic_params, ast_id } = &self.tree[it];
337+
self.print_ast_id(ast_id.erase());
316338
self.print_visibility(*visibility);
317339
w!(self, "enum {}", name.display(self.db.upcast()));
318340
self.print_generic_params(generic_params);
319341
self.print_where_clause_and_opening_brace(generic_params);
320342
self.indented(|this| {
321343
for variant in FileItemTreeId::range_iter(variants.clone()) {
322-
let Variant { name, fields, ast_id: _ } = &this.tree[variant];
344+
let Variant { name, fields, ast_id } = &this.tree[variant];
345+
this.print_ast_id(ast_id.erase());
323346
this.print_attrs_of(variant, "\n");
324347
w!(this, "{}", name.display(self.db.upcast()));
325348
this.print_fields(fields);
@@ -329,7 +352,8 @@ impl Printer<'_> {
329352
wln!(self, "}}");
330353
}
331354
ModItem::Const(it) => {
332-
let Const { name, visibility, type_ref, ast_id: _ } = &self.tree[it];
355+
let Const { name, visibility, type_ref, ast_id } = &self.tree[it];
356+
self.print_ast_id(ast_id.erase());
333357
self.print_visibility(*visibility);
334358
w!(self, "const ");
335359
match name {
@@ -341,7 +365,8 @@ impl Printer<'_> {
341365
wln!(self, " = _;");
342366
}
343367
ModItem::Static(it) => {
344-
let Static { name, visibility, mutable, type_ref, ast_id: _ } = &self.tree[it];
368+
let Static { name, visibility, mutable, type_ref, ast_id } = &self.tree[it];
369+
self.print_ast_id(ast_id.erase());
345370
self.print_visibility(*visibility);
346371
w!(self, "static ");
347372
if *mutable {
@@ -353,15 +378,9 @@ impl Printer<'_> {
353378
wln!(self);
354379
}
355380
ModItem::Trait(it) => {
356-
let Trait {
357-
name,
358-
visibility,
359-
is_auto,
360-
is_unsafe,
361-
items,
362-
generic_params,
363-
ast_id: _,
364-
} = &self.tree[it];
381+
let Trait { name, visibility, is_auto, is_unsafe, items, generic_params, ast_id } =
382+
&self.tree[it];
383+
self.print_ast_id(ast_id.erase());
365384
self.print_visibility(*visibility);
366385
if *is_unsafe {
367386
w!(self, "unsafe ");
@@ -380,7 +399,8 @@ impl Printer<'_> {
380399
wln!(self, "}}");
381400
}
382401
ModItem::TraitAlias(it) => {
383-
let TraitAlias { name, visibility, generic_params, ast_id: _ } = &self.tree[it];
402+
let TraitAlias { name, visibility, generic_params, ast_id } = &self.tree[it];
403+
self.print_ast_id(ast_id.erase());
384404
self.print_visibility(*visibility);
385405
w!(self, "trait {}", name.display(self.db.upcast()));
386406
self.print_generic_params(generic_params);
@@ -397,8 +417,9 @@ impl Printer<'_> {
397417
is_unsafe,
398418
items,
399419
generic_params,
400-
ast_id: _,
420+
ast_id,
401421
} = &self.tree[it];
422+
self.print_ast_id(ast_id.erase());
402423
if *is_unsafe {
403424
w!(self, "unsafe");
404425
}
@@ -422,8 +443,9 @@ impl Printer<'_> {
422443
wln!(self, "}}");
423444
}
424445
ModItem::TypeAlias(it) => {
425-
let TypeAlias { name, visibility, bounds, type_ref, generic_params, ast_id: _ } =
446+
let TypeAlias { name, visibility, bounds, type_ref, generic_params, ast_id } =
426447
&self.tree[it];
448+
self.print_ast_id(ast_id.erase());
427449
self.print_visibility(*visibility);
428450
w!(self, "type {}", name.display(self.db.upcast()));
429451
self.print_generic_params(generic_params);
@@ -440,7 +462,8 @@ impl Printer<'_> {
440462
wln!(self);
441463
}
442464
ModItem::Mod(it) => {
443-
let Mod { name, visibility, kind, ast_id: _ } = &self.tree[it];
465+
let Mod { name, visibility, kind, ast_id } = &self.tree[it];
466+
self.print_ast_id(ast_id.erase());
444467
self.print_visibility(*visibility);
445468
w!(self, "mod {}", name.display(self.db.upcast()));
446469
match kind {
@@ -459,15 +482,24 @@ impl Printer<'_> {
459482
}
460483
}
461484
ModItem::MacroCall(it) => {
462-
let MacroCall { path, ast_id: _, expand_to: _, call_site: _ } = &self.tree[it];
485+
let MacroCall { path, ast_id, expand_to, call_site } = &self.tree[it];
486+
let _ = writeln!(
487+
self,
488+
"// AstId: {:?}, Span: {}, ExpandTo: {:?}",
489+
ast_id.erase().into_raw(),
490+
call_site,
491+
expand_to
492+
);
463493
wln!(self, "{}!(...);", path.display(self.db.upcast()));
464494
}
465495
ModItem::MacroRules(it) => {
466-
let MacroRules { name, ast_id: _ } = &self.tree[it];
496+
let MacroRules { name, ast_id } = &self.tree[it];
497+
self.print_ast_id(ast_id.erase());
467498
wln!(self, "macro_rules! {} {{ ... }}", name.display(self.db.upcast()));
468499
}
469500
ModItem::Macro2(it) => {
470-
let Macro2 { name, visibility, ast_id: _ } = &self.tree[it];
501+
let Macro2 { name, visibility, ast_id } = &self.tree[it];
502+
self.print_ast_id(ast_id.erase());
471503
self.print_visibility(*visibility);
472504
wln!(self, "macro {} {{ ... }}", name.display(self.db.upcast()));
473505
}
@@ -583,6 +615,10 @@ impl Printer<'_> {
583615
});
584616
true
585617
}
618+
619+
fn print_ast_id(&mut self, ast_id: ErasedFileAstId) {
620+
wln!(self, "// AstId: {:?}", ast_id.into_raw());
621+
}
586622
}
587623

588624
impl Write for Printer<'_> {

0 commit comments

Comments
 (0)