syn::Field.attrs 不包含惰性属性
syn::Field.attrs doesn't contain inert attributes
给定以下结构定义:
#[derive(Builder)]
pub struct Command {
executable: String,
#[builder(each = "arg")]
args: Vec<String>,
#[builder(each = "env")]
env: Vec<String>,
current_dir: Option<String>,
}
(其中 #[builder(...)]
属性是由我的 Builder
派生宏实现定义的惰性属性),
Builder
宏无法“看到”#[builder(...)]
字段属性。以下代码
let name = &f.ident;
let attrs = &f.attrs;
println!("Attributes: {:?}: {}", name, attrs.len());
(其中 f
是 syn::Field
)给出
Attributes: Some(Ident { ident: "executable", span: #0 bytes(2830..2840) }): 0
Attributes: Some(Ident { ident: "args", span: #0 bytes(2854..2858) }): 0
Attributes: Some(Ident { ident: "env", span: #0 bytes(2877..2880) }): 0
Attributes: Some(Ident { ident: "current_dir", span: #0 bytes(2899..2910) }): 0
谁能帮我找出我在这里遗漏了什么?
我认为您可能只是错误地解释了终端输出,因为当我克隆您的叉子并删除无法编译的测试代码时,这是我为测试获得的具有属性的输出:
test tests/07-repeated-field.rs ... ok
WARNINGS:
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
warning: unused import: `builder_trait:: Builder`
--> tests/07-repeated-field.rs:32:5
|
32 | use builder_trait:: Builder;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: 1 warning emitted
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
STDOUT:
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
Attributes: Some(Ident { ident: "executable", span: #0 bytes(1471..1481) }): 0
Attributes: Some(Ident { ident: "args", span: #0 bytes(1524..1528) }): 1
Attributes: Some(Ident { ident: "env", span: #0 bytes(1576..1579) }): 1
Attributes: Some(Ident { ident: "current_dir", span: #0 bytes(1598..1609) }): 0
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
STDERR:
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
warning: unused import: `builder_trait:: Builder`
--> /home/cassy/projects/forked-proc-macro-workshop/builder/tests/07-repeated-field.rs:32:5
|
32 | use builder_trait:: Builder;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
为了清楚起见,我在 07-repeated-field.rs
中完全注释掉了 main
的主体,因为它无法编译,这样做让我看到了这个输出。
我之前没有使用过 trybuild
,但似乎如果测试构建失败,那么它不会为您提供 proc 宏调用的输出。此外,它在显示测试名称后显示测试编译过程的STDOUT
/STDERR
,这可能是混淆的根源。
给定以下结构定义:
#[derive(Builder)]
pub struct Command {
executable: String,
#[builder(each = "arg")]
args: Vec<String>,
#[builder(each = "env")]
env: Vec<String>,
current_dir: Option<String>,
}
(其中 #[builder(...)]
属性是由我的 Builder
派生宏实现定义的惰性属性),
Builder
宏无法“看到”#[builder(...)]
字段属性。以下代码
let name = &f.ident;
let attrs = &f.attrs;
println!("Attributes: {:?}: {}", name, attrs.len());
(其中 f
是 syn::Field
)给出
Attributes: Some(Ident { ident: "executable", span: #0 bytes(2830..2840) }): 0
Attributes: Some(Ident { ident: "args", span: #0 bytes(2854..2858) }): 0
Attributes: Some(Ident { ident: "env", span: #0 bytes(2877..2880) }): 0
Attributes: Some(Ident { ident: "current_dir", span: #0 bytes(2899..2910) }): 0
谁能帮我找出我在这里遗漏了什么?
我认为您可能只是错误地解释了终端输出,因为当我克隆您的叉子并删除无法编译的测试代码时,这是我为测试获得的具有属性的输出:
test tests/07-repeated-field.rs ... ok
WARNINGS:
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
warning: unused import: `builder_trait:: Builder`
--> tests/07-repeated-field.rs:32:5
|
32 | use builder_trait:: Builder;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: 1 warning emitted
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
STDOUT:
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
Attributes: Some(Ident { ident: "executable", span: #0 bytes(1471..1481) }): 0
Attributes: Some(Ident { ident: "args", span: #0 bytes(1524..1528) }): 1
Attributes: Some(Ident { ident: "env", span: #0 bytes(1576..1579) }): 1
Attributes: Some(Ident { ident: "current_dir", span: #0 bytes(1598..1609) }): 0
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
STDERR:
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
warning: unused import: `builder_trait:: Builder`
--> /home/cassy/projects/forked-proc-macro-workshop/builder/tests/07-repeated-field.rs:32:5
|
32 | use builder_trait:: Builder;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
为了清楚起见,我在 07-repeated-field.rs
中完全注释掉了 main
的主体,因为它无法编译,这样做让我看到了这个输出。
我之前没有使用过 trybuild
,但似乎如果测试构建失败,那么它不会为您提供 proc 宏调用的输出。此外,它在显示测试名称后显示测试编译过程的STDOUT
/STDERR
,这可能是混淆的根源。