Skip to content

Commit 893fadd

Browse files
committed
Auto merge of #76820 - jyn514:query-comments, r=davidtwco
Preserve doc-comments when generating queries Closes #76812
2 parents 3a4da87 + 2f1bfd6 commit 893fadd

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

compiler/rustc_macros/src/query.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use syn::parse::{Parse, ParseStream, Result};
55
use syn::punctuated::Punctuated;
66
use syn::spanned::Spanned;
77
use syn::{
8-
braced, parenthesized, parse_macro_input, Attribute, Block, Error, Expr, Ident, ReturnType,
9-
Token, Type,
8+
braced, parenthesized, parse_macro_input, AttrStyle, Attribute, Block, Error, Expr, Ident,
9+
ReturnType, Token, Type,
1010
};
1111

1212
#[allow(non_camel_case_types)]
@@ -128,17 +128,25 @@ impl Parse for QueryModifier {
128128
}
129129

130130
/// Ensures only doc comment attributes are used
131-
fn check_attributes(attrs: Vec<Attribute>) -> Result<()> {
132-
for attr in attrs {
131+
fn check_attributes(attrs: Vec<Attribute>) -> Result<Vec<Attribute>> {
132+
let inner = |attr: Attribute| {
133133
if !attr.path.is_ident("doc") {
134-
return Err(Error::new(attr.span(), "attributes not supported on queries"));
134+
Err(Error::new(attr.span(), "attributes not supported on queries"))
135+
} else if attr.style != AttrStyle::Outer {
136+
Err(Error::new(
137+
attr.span(),
138+
"attributes must be outer attributes (`///`), not inner attributes",
139+
))
140+
} else {
141+
Ok(attr)
135142
}
136-
}
137-
Ok(())
143+
};
144+
attrs.into_iter().map(inner).collect()
138145
}
139146

140147
/// A compiler query. `query ... { ... }`
141148
struct Query {
149+
doc_comments: Vec<Attribute>,
142150
modifiers: List<QueryModifier>,
143151
name: Ident,
144152
key: IdentOrWild,
@@ -148,7 +156,7 @@ struct Query {
148156

149157
impl Parse for Query {
150158
fn parse(input: ParseStream<'_>) -> Result<Self> {
151-
check_attributes(input.call(Attribute::parse_outer)?)?;
159+
let doc_comments = check_attributes(input.call(Attribute::parse_outer)?)?;
152160

153161
// Parse the query declaration. Like `query type_of(key: DefId) -> Ty<'tcx>`
154162
input.parse::<kw::query>()?;
@@ -165,7 +173,7 @@ impl Parse for Query {
165173
braced!(content in input);
166174
let modifiers = content.parse()?;
167175

168-
Ok(Query { modifiers, name, key, arg, result })
176+
Ok(Query { doc_comments, modifiers, name, key, arg, result })
169177
}
170178
}
171179

@@ -476,9 +484,10 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
476484
};
477485

478486
let attribute_stream = quote! {#(#attributes),*};
479-
487+
let doc_comments = query.doc_comments.iter();
480488
// Add the query to the group
481489
group_stream.extend(quote! {
490+
#(#doc_comments)*
482491
[#attribute_stream] fn #name: #name(#arg) #result,
483492
});
484493

compiler/rustc_middle/src/query/mod.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -42,48 +42,48 @@ rustc_queries! {
4242
}
4343

4444
Other {
45-
// Represents crate as a whole (as distinct from the top-level crate module).
46-
// If you call `hir_crate` (e.g., indirectly by calling `tcx.hir().krate()`),
47-
// we will have to assume that any change means that you need to be recompiled.
48-
// This is because the `hir_crate` query gives you access to all other items.
49-
// To avoid this fate, do not call `tcx.hir().krate()`; instead,
50-
// prefer wrappers like `tcx.visit_all_items_in_krate()`.
45+
/// Represents crate as a whole (as distinct from the top-level crate module).
46+
/// If you call `hir_crate` (e.g., indirectly by calling `tcx.hir().krate()`),
47+
/// we will have to assume that any change means that you need to be recompiled.
48+
/// This is because the `hir_crate` query gives you access to all other items.
49+
/// To avoid this fate, do not call `tcx.hir().krate()`; instead,
50+
/// prefer wrappers like `tcx.visit_all_items_in_krate()`.
5151
query hir_crate(key: CrateNum) -> &'tcx Crate<'tcx> {
5252
eval_always
5353
no_hash
5454
desc { "get the crate HIR" }
5555
}
5656

57-
// The indexed HIR. This can be conveniently accessed by `tcx.hir()`.
58-
// Avoid calling this query directly.
57+
/// The indexed HIR. This can be conveniently accessed by `tcx.hir()`.
58+
/// Avoid calling this query directly.
5959
query index_hir(_: CrateNum) -> &'tcx map::IndexedHir<'tcx> {
6060
eval_always
6161
no_hash
6262
desc { "index HIR" }
6363
}
6464

65-
// The items in a module.
66-
//
67-
// This can be conveniently accessed by `tcx.hir().visit_item_likes_in_module`.
68-
// Avoid calling this query directly.
65+
/// The items in a module.
66+
///
67+
/// This can be conveniently accessed by `tcx.hir().visit_item_likes_in_module`.
68+
/// Avoid calling this query directly.
6969
query hir_module_items(key: LocalDefId) -> &'tcx hir::ModuleItems {
7070
eval_always
7171
desc { |tcx| "HIR module items in `{}`", tcx.def_path_str(key.to_def_id()) }
7272
}
7373

74-
// Gives access to the HIR node for the HIR owner `key`.
75-
//
76-
// This can be conveniently accessed by methods on `tcx.hir()`.
77-
// Avoid calling this query directly.
74+
/// Gives access to the HIR node for the HIR owner `key`.
75+
///
76+
/// This can be conveniently accessed by methods on `tcx.hir()`.
77+
/// Avoid calling this query directly.
7878
query hir_owner(key: LocalDefId) -> Option<&'tcx crate::hir::Owner<'tcx>> {
7979
eval_always
8080
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
8181
}
8282

83-
// Gives access to the HIR nodes and bodies inside the HIR owner `key`.
84-
//
85-
// This can be conveniently accessed by methods on `tcx.hir()`.
86-
// Avoid calling this query directly.
83+
/// Gives access to the HIR nodes and bodies inside the HIR owner `key`.
84+
///
85+
/// This can be conveniently accessed by methods on `tcx.hir()`.
86+
/// Avoid calling this query directly.
8787
query hir_owner_nodes(key: LocalDefId) -> Option<&'tcx crate::hir::OwnerNodes<'tcx>> {
8888
eval_always
8989
desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
@@ -334,9 +334,9 @@ rustc_queries! {
334334
}
335335

336336
TypeChecking {
337-
// Erases regions from `ty` to yield a new type.
338-
// Normally you would just use `tcx.erase_regions(&value)`,
339-
// however, which uses this query as a kind of cache.
337+
/// Erases regions from `ty` to yield a new type.
338+
/// Normally you would just use `tcx.erase_regions(&value)`,
339+
/// however, which uses this query as a kind of cache.
340340
query erase_regions_ty(ty: Ty<'tcx>) -> Ty<'tcx> {
341341
// This query is not expected to have input -- as a result, it
342342
// is not a good candidates for "replay" because it is essentially a
@@ -1538,7 +1538,7 @@ rustc_queries! {
15381538
desc { "looking up supported target features" }
15391539
}
15401540

1541-
// Get an estimate of the size of an InstanceDef based on its MIR for CGU partitioning.
1541+
/// Get an estimate of the size of an InstanceDef based on its MIR for CGU partitioning.
15421542
query instance_def_size_estimate(def: ty::InstanceDef<'tcx>)
15431543
-> usize {
15441544
desc { |tcx| "estimating size for `{}`", tcx.def_path_str(def.def_id()) }

0 commit comments

Comments
 (0)