Rust的无限递归派生宏?

时间:2019-01-04 18:39:47

标签: macros rust infinite-recursion

我在Rust中的一个衍生物宏上做了以下修饰:

extern crate proc_macro;

use crate::proc_macro::TokenStream;
use quote::quote;
use syn;

#[proc_macro_derive(DeserializeConfigurable)]
pub fn deserialize_configurable_derive(input: TokenStream) -> TokenStream {
    let ast: syn::DeriveInput = syn::parse(input).unwrap();
    let name = &ast.ident;
    let gen = quote! {
        impl<'de> Deserialize<'de> for #name {
            fn deserialize<D>(deserializer: D) -> Result<#name, D::Error>
            where
                D: Deserializer<'de>,
            {
                let config = <#name as Configurable>::Config::deserialize(deserializer)?;
                Ok(#name::from_config(config))
            }
        }
    };
    gen.into()
}

目标是基于另一个特征deserializeConfigurable提供特定的一揽子实施。

但是,编译器对我的报价单有以下投诉:

  扩展宏stringify时达到

递归限制

我在这里看不到递归。我什至没有使用stringify!什么叫无穷大?

1 个答案:

答案 0 :(得分:1)

尝试在本地进行编译会给出完整的错误消息:

error: recursion limit reached while expanding the macro `stringify`
  --> src/lib.rs:13:15
   |
13 |       let gen = quote! {
   |  _______________^
14 | |         impl<'de> Deserialize<'de> for #name {
15 | |             fn deserialize<D>(deserializer: D) -> Result<#name, D::Error>
16 | |             where
...  |
22 | |         }
23 | |     };
   | |_____^
   |
   = help: consider adding a `#![recursion_limit="128"]` attribute to your crate
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)                                                                                                                                 

error: aborting due to previous error

将以下内容放入包装箱的第一行即可使错误消失。我的猜测是,对于如此复杂的宏(IMO并不那么复杂),默认的recursion_limit太低了。我确定有充分的理由,并且很高兴错误消息包含了解决方案:

#![recursion_limit="128"]