Skip to content

Commit 4d6fcb5

Browse files
committed
make default work again
1 parent 2d6b65c commit 4d6fcb5

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

macro/src/lib.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use proc_macro2::{Ident, TokenStream};
2-
use proc_macro_error::{abort, abort_call_site, proc_macro_error, ResultExt};
1+
use proc_macro2::TokenStream;
2+
use proc_macro_error::{abort_call_site, proc_macro_error, ResultExt};
33
use quote::quote;
44
use syn::{
55
parse_macro_input, parse_quote, punctuated::Punctuated, DataStruct, DeriveInput, Field, Fields,
6-
FieldsNamed, Lit, Meta, MetaList, MetaNameValue, NestedMeta, Path, Token,
6+
FieldsNamed, Lit, Meta, MetaNameValue, NestedMeta, Path, Token,
77
};
88

99
// TODO generally should use fully qualified names for trait function calls
@@ -88,16 +88,11 @@ pub fn attribute_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStre
8888
{
8989
const VALID_FORMAT: &str = r#"Expected `#[attribute(default, missing="error message", expected="error message"])`"#;
9090
let meta: Meta = attribute.parse_meta().unwrap_or_abort();
91-
match meta {
92-
Meta::List(meta) => {
93-
for meta in meta.nested {
94-
if let NestedMeta::Meta(Meta::NameValue(MetaNameValue {
95-
path,
96-
lit,
97-
..
98-
})) = meta
99-
{
100-
match (
91+
if let Meta::List(meta) = meta {
92+
for meta in meta.nested {
93+
if let NestedMeta::Meta(meta) = meta {
94+
match meta {
95+
Meta::NameValue(MetaNameValue { path, lit, .. }) => match (
10196
path.get_ident()
10297
.unwrap_or_else(|| abort_call_site!(VALID_FORMAT))
10398
.to_string()
@@ -107,20 +102,21 @@ pub fn attribute_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStre
107102
("missing", Lit::Str(lit)) => missing = Some(lit.value()),
108103
("expected", Lit::Str(lit)) => expected = Some(lit.value()),
109104
_ => abort_call_site!(VALID_FORMAT),
105+
},
106+
107+
Meta::Path(path) => {
108+
if path.is_ident("default") {
109+
default = true;
110+
} else {
111+
abort_call_site!(VALID_FORMAT);
112+
}
110113
}
111-
} else {
112-
abort_call_site!(VALID_FORMAT);
114+
_ => abort_call_site!(VALID_FORMAT),
113115
}
114-
}
115-
}
116-
Meta::Path(path) => {
117-
if path.is_ident("default") {
118-
default = true;
119116
} else {
120117
abort_call_site!(VALID_FORMAT);
121118
}
122119
}
123-
_ => (),
124120
}
125121
}
126122

tests/derive.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,15 @@ fn error_specified() {
147147
"error message"
148148
);
149149
}
150+
151+
#[test]
152+
fn default() {
153+
#[derive(Attribute, Debug)]
154+
#[attribute(ident = "test")]
155+
#[attribute(invalid_field = "error message")]
156+
#[allow(dead_code)]
157+
struct Test {
158+
#[attribute(default)]
159+
hi: f32,
160+
}
161+
}

0 commit comments

Comments
 (0)