Skip to content

Commit 6c6522c

Browse files
committed
[mmtk-macro] Update syn dependency to 2.0
1 parent ec02302 commit 6c6522c

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ proc-macro = true
1313

1414
[dependencies]
1515
proc-macro2 = "1.0.37"
16-
syn = { version = "1.0.91", features = ["extra-traits"] }
16+
syn = { version = "2.0.29", features = ["extra-traits"] }
1717
quote = "1.0.18"
1818
proc-macro-error = "1.0.4"

macros/src/lib.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,35 @@ pub fn derive_plan_trace_object(input: TokenStream) -> TokenStream {
3434
let ident = input.ident;
3535
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
3636

37-
let output = if let syn::Data::Struct(syn::DataStruct {
37+
let syn::Data::Struct(syn::DataStruct {
3838
fields: syn::Fields::Named(ref fields),
3939
..
40-
}) = input.data
41-
{
42-
let spaces = util::get_fields_with_attribute(fields, "trace");
43-
let post_scan_spaces = util::get_fields_with_attribute(fields, "post_scan");
44-
let fallback = util::get_unique_field_with_attribute(fields, "fallback_trace");
40+
}) = input.data else {
41+
abort_call_site!("`#[derive(PlanTraceObject)]` only supports structs with named fields.");
42+
};
43+
44+
let spaces = util::get_fields_with_attribute(fields, "trace");
45+
let post_scan_spaces = util::get_fields_with_attribute(fields, "post_scan");
46+
let fallback = util::get_unique_field_with_attribute(fields, "fallback_trace");
47+
48+
let trace_object_function =
49+
plan_trace_object_impl::generate_trace_object(&spaces, &fallback, &ty_generics);
50+
let post_scan_object_function = plan_trace_object_impl::generate_post_scan_object(
51+
&post_scan_spaces,
52+
&fallback,
53+
&ty_generics,
54+
);
55+
let may_move_objects_function =
56+
plan_trace_object_impl::generate_may_move_objects(&spaces, &fallback, &ty_generics);
4557

46-
let trace_object_function =
47-
plan_trace_object_impl::generate_trace_object(&spaces, &fallback, &ty_generics);
48-
let post_scan_object_function = plan_trace_object_impl::generate_post_scan_object(
49-
&post_scan_spaces,
50-
&fallback,
51-
&ty_generics,
52-
);
53-
let may_move_objects_function =
54-
plan_trace_object_impl::generate_may_move_objects(&spaces, &fallback, &ty_generics);
55-
quote! {
56-
impl #impl_generics crate::plan::PlanTraceObject #ty_generics for #ident #ty_generics #where_clause {
57-
#trace_object_function
58+
let output = quote! {
59+
impl #impl_generics crate::plan::PlanTraceObject #ty_generics for #ident #ty_generics #where_clause {
60+
#trace_object_function
5861

59-
#post_scan_object_function
62+
#post_scan_object_function
6063

61-
#may_move_objects_function
62-
}
64+
#may_move_objects_function
6365
}
64-
} else {
65-
abort_call_site!("`#[derive(PlanTraceObject)]` only supports structs with named fields.")
6666
};
6767

6868
// Debug the output - use the following code to debug the generated code (when cargo exapand is not working)

macros/src/plan_trace_object_impl.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use proc_macro2::TokenStream as TokenStream2;
2+
use proc_macro_error::abort_call_site;
23
use quote::quote;
3-
use syn::{Field, TypeGenerics};
4+
use syn::{Expr, Field, TypeGenerics};
45

56
use crate::util;
67

@@ -16,20 +17,23 @@ pub(crate) fn generate_trace_object<'a>(
1617

1718
// Figure out copy
1819
let trace_attr = util::get_field_attribute(f, "trace").unwrap();
19-
let copy = if !trace_attr.tokens.is_empty() {
20-
use syn::Token;
21-
use syn::NestedMeta;
22-
use syn::punctuated::Punctuated;
23-
24-
let args = trace_attr.parse_args_with(Punctuated::<NestedMeta, Token![,]>::parse_terminated).unwrap();
25-
// CopySemantics::X is a path.
26-
if let Some(NestedMeta::Meta(syn::Meta::Path(p))) = args.first() {
27-
quote!{ Some(#p) }
28-
} else {
20+
let copy = match &trace_attr.meta {
21+
syn::Meta::Path(_) => {
22+
// #[trace]
2923
quote!{ None }
30-
}
31-
} else {
32-
quote!{ None }
24+
},
25+
syn::Meta::List(list) => {
26+
// #[trace(CopySemantics::BlahBlah)]
27+
let copy_semantics = list.parse_args::<Expr>().unwrap_or_else(|_| {
28+
abort_call_site!("In `#[trace(copy_semantics)]`, copy_semantics must be an expression.");
29+
});
30+
31+
quote!{ Some(#copy_semantics) }
32+
},
33+
syn::Meta::NameValue(_) => {
34+
// #[trace = BlahBlah]
35+
abort_call_site!("The #[trace] macro does not support name-value form.");
36+
},
3337
};
3438

3539
quote! {

macros/src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ pub fn get_field_attribute<'f>(field: &'f Field, attr_name: &str) -> Option<&'f
55
let attrs = field
66
.attrs
77
.iter()
8-
.filter(|a| a.path.is_ident(attr_name))
8+
.filter(|a| a.path().is_ident(attr_name))
99
.collect::<Vec<_>>();
1010
if attrs.len() > 1 {
1111
let second_attr = attrs.get(1).unwrap();
12-
abort! { second_attr.path.span(), "Duplicated attribute: #[{}]", attr_name }
12+
abort! { second_attr.path().span(), "Duplicated attribute: #[{}]", attr_name }
1313
};
1414

1515
attrs.get(0).cloned()
@@ -35,7 +35,7 @@ pub fn get_unique_field_with_attribute<'f>(
3535
result = Some(field);
3636
continue;
3737
} else {
38-
let span = attr.path.span();
38+
let span = attr.path().span();
3939
abort! { span, "At most one field in a struct can have the #[{}] attribute.", attr_name };
4040
}
4141
}

0 commit comments

Comments
 (0)