gpt4 book ai didi

macros - Rust 派生宏中的无限递归?

转载 作者:行者123 更新时间:2023-11-29 08:29:17 36 4
gpt4 key购买 nike

我在 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()
}

目标是根据另一个特征 Configurabledeserialize 提供特定的总体实现。

但是,编译器对我的引用语句有以下提示:

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"]

关于macros - Rust 派生宏中的无限递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54044382/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com