syn icon indicating copy to clipboard operation
syn copied to clipboard

Demonstrate parse_quote in the crate-level documentation

Open dtolnay opened this issue 7 years ago • 2 comments

This macro is commonly useful for building a syntax tree and should be featured more prominently.

// Add a trait bound to every type parameter
for param in &mut generics.params {
    if let GenericParam::Type(ref mut type_param) = *param {
        type_param.bounds.push(parse_quote!(diesel::query_builder::QueryId));
    }
}

dtolnay avatar Jan 15 '18 21:01 dtolnay

Do we also show along with the examples that parse_quote! requires an explicit dependency on quote crate in user's code?

Since macro reexport is unstable and has a lot of issues at this moment I suggest that the documentation should inform its readers about this unfortunate consequence of using external macros in macro expansions.

For example, this code fails to compile today if there is no dependencies.quote entry in Cargo.toml and no #[macro_use] extern crate quote; in main.rs:

#[macro_use]
extern crate syn;

fn main() {
    let _x: syn::Path = parse_quote!(core::iter::Iterator);
}

Even though the first example in the parse_quote! docs has #[macro_use] extern crate quote;, in my opinion it doesn't stand out enough to notice that. On top of that the docs don't mention anything about Cargo.toml.

hcpl avatar May 22 '18 22:05 hcpl

It does not require a direct quote dependency as of #512.

dtolnay avatar Dec 11 '22 01:12 dtolnay