Skip to content

Commit e67f9d3

Browse files
committed
Use desc as a doc-comment if none exist yet
- Replace {} with the stringified expr Giant thank you to `@danielhenrymantilla` for figuring out how to make this work ❤️ - Note that this is just an approximation and it would be better to add a doc-comment
1 parent 50a9097 commit e67f9d3

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)