mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 02:57:37 +00:00
Rollup merge of #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:    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.
This commit is contained in:
commit
18ac1ecac9
@ -5,8 +5,8 @@ use syn::parse::{Parse, ParseStream, Result};
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::spanned::Spanned;
|
||||
use syn::{
|
||||
braced, parenthesized, parse_macro_input, AttrStyle, Attribute, Block, Error, Expr, Ident,
|
||||
ReturnType, Token, Type,
|
||||
braced, parenthesized, parse_macro_input, parse_quote, AttrStyle, Attribute, Block, Error,
|
||||
Expr, Ident, ReturnType, Token, Type,
|
||||
};
|
||||
|
||||
mod kw {
|
||||
@ -272,6 +272,40 @@ fn process_modifiers(query: &mut Query) -> QueryModifiers {
|
||||
if desc.is_some() {
|
||||
panic!("duplicate modifier `desc` for query `{}`", query.name);
|
||||
}
|
||||
// If there are no doc-comments, give at least some idea of what
|
||||
// it does by showing the query description.
|
||||
if query.doc_comments.is_empty() {
|
||||
use ::syn::*;
|
||||
let mut list = list.iter();
|
||||
let format_str: String = match list.next() {
|
||||
Some(&Expr::Lit(ExprLit { lit: Lit::Str(ref lit_str), .. })) => {
|
||||
lit_str.value().replace("`{}`", "{}") // We add them later anyways for consistency
|
||||
}
|
||||
_ => panic!("Expected a string literal"),
|
||||
};
|
||||
let mut fmt_fragments = format_str.split("{}");
|
||||
let mut doc_string = fmt_fragments.next().unwrap().to_string();
|
||||
list.map(::quote::ToTokens::to_token_stream).zip(fmt_fragments).for_each(
|
||||
|(tts, next_fmt_fragment)| {
|
||||
use ::core::fmt::Write;
|
||||
write!(
|
||||
&mut doc_string,
|
||||
" `{}` {}",
|
||||
tts.to_string().replace(" . ", "."),
|
||||
next_fmt_fragment,
|
||||
)
|
||||
.unwrap();
|
||||
},
|
||||
);
|
||||
let doc_string = format!(
|
||||
"[query description - consider adding a doc-comment!] {}",
|
||||
doc_string
|
||||
);
|
||||
let comment = parse_quote! {
|
||||
#[doc = #doc_string]
|
||||
};
|
||||
query.doc_comments.push(comment);
|
||||
}
|
||||
desc = Some((tcx, list));
|
||||
}
|
||||
QueryModifier::FatalCycle => {
|
||||
|
Loading…
Reference in New Issue
Block a user