Skip to content

Commit 18ac1ec

Browse files
authored
Rollup merge of rust-lang#80337 - jyn514:add-query-desc, r=varkor
Use `desc` as a doc-comment for queries if there are no doc comments This at least gives *some* idea of what the query does even if it's not very readable. Some examples: ![image](https://user-images.githubusercontent.com/23638587/103021399-13e15c00-4518-11eb-8121-940774ae2fd1.png) ![image](https://user-images.githubusercontent.com/23638587/103021448-222f7800-4518-11eb-8ee6-cc10795fdc22.png) ![image](https://user-images.githubusercontent.com/23638587/103021434-1d6ac400-4518-11eb-885b-59d00c57bc70.png) I want to turn `{}` into either `_` or the stringified expr, but [I'm not sure how to do that](https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/Evaluate.20format.20string.20in.20proc-macro). In the meantime, this is better than having no docs at all.
2 parents 88b198b + e67f9d3 commit 18ac1ec

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

compiler/rustc_macros/src/query.rs

+36-2
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, AttrStyle, Attribute, Block, Error, Expr, Ident,
9-
ReturnType, Token, Type,
8+
braced, parenthesized, parse_macro_input, parse_quote, AttrStyle, Attribute, Block, Error,
9+
Expr, Ident, ReturnType, Token, Type,
1010
};
1111

1212
mod kw {
@@ -272,6 +272,40 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
272272
if desc.is_some() {
273273
panic!("duplicate modifier `desc` for query `{}`", query.name);
274274
}
275+
// If there are no doc-comments, give at least some idea of what
276+
// it does by showing the query description.
277+
if query.doc_comments.is_empty() {
278+
use ::syn::*;
279+
let mut list = list.iter();
280+
let format_str: String = match list.next() {
281+
Some(&Expr::Lit(ExprLit { lit: Lit::Str(ref lit_str), .. })) => {
282+
lit_str.value().replace("`{}`", "{}") // We add them later anyways for consistency
283+
}
284+
_ => panic!("Expected a string literal"),
285+
};
286+
let mut fmt_fragments = format_str.split("{}");
287+
let mut doc_string = fmt_fragments.next().unwrap().to_string();
288+
list.map(::quote::ToTokens::to_token_stream).zip(fmt_fragments).for_each(
289+
|(tts, next_fmt_fragment)| {
290+
use ::core::fmt::Write;
291+
write!(
292+
&mut doc_string,
293+
" `{}` {}",
294+
tts.to_string().replace(" . ", "."),
295+
next_fmt_fragment,
296+
)
297+
.unwrap();
298+
},
299+
);
300+
let doc_string = format!(
301+
"[query description - consider adding a doc-comment!] {}",
302+
doc_string
303+
);
304+
let comment = parse_quote! {
305+
#[doc = #doc_string]
306+
};
307+
query.doc_comments.push(comment);
308+
}
275309
desc = Some((tcx, list));
276310
}
277311
QueryModifier::FatalCycle => {

0 commit comments

Comments
 (0)