Rust 派生宏中的无限递归?
Infinite recursion in Rust derive macro?
我在 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()
}
目标是根据另一个特征 Configurable
.
为 deserialize
提供具体的全面实施
但是,编译器对我的引用语句有以下抱怨:
recursion limit reached while expanding the macro stringify
我在这里没有看到递归。我什至没有使用 stringify
!什么叫什么无穷大?
尝试在本地编译会给出完整的错误消息:
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
将以下内容作为 crate 的第一行可以使错误消失。我的猜测是默认 recursion_limit 对于如此复杂的宏(IMO 并不那么复杂)来说太低了。我确信这是有充分理由的,我很高兴错误消息包含解决方案:
#![recursion_limit="128"]
我在 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()
}
目标是根据另一个特征 Configurable
.
deserialize
提供具体的全面实施
但是,编译器对我的引用语句有以下抱怨:
recursion limit reached while expanding the macro
stringify
我在这里没有看到递归。我什至没有使用 stringify
!什么叫什么无穷大?
尝试在本地编译会给出完整的错误消息:
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
将以下内容作为 crate 的第一行可以使错误消失。我的猜测是默认 recursion_limit 对于如此复杂的宏(IMO 并不那么复杂)来说太低了。我确信这是有充分理由的,我很高兴错误消息包含解决方案:
#![recursion_limit="128"]