Skip to content

Commit

Permalink
Merge pull request #760 from mkroening/syn2
Browse files Browse the repository at this point in the history
`defmt-macros`: Upgrade to syn 2
  • Loading branch information
Urhengulas authored Jun 15, 2023
2 parents f82051f + 579ab74 commit ba53b2e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 56 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- [#760]: `defmt-macros`: Upgrade to syn 2
- [#758]: `defmt-print`: Tidy up
- [#757]: `defmt-print`: Allow reading from a tcp port
- [#756]: `CI`: Switch from bors to merge queue
- [#753]: `demft` Add `Format` impls for `core::ptr::NonNull` and `fn(Args...) -> Ret` (up to 12 arguments)

[#760]: https:/knurling-rs/defmt/pull/760
[#758]: https:/knurling-rs/defmt/pull/758
[#757]: https:/knurling-rs/defmt/pull/757
[#756]: https:/knurling-rs/defmt/pull/756
Expand Down
2 changes: 1 addition & 1 deletion firmware/defmt-test/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = ["extra-traits", "full"] }
syn = { version = "2", features = ["extra-traits", "full"] }

[dev-dependencies]
trybuild = "1"
14 changes: 7 additions & 7 deletions firmware/defmt-test/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea
let mut ignore = false;

f.attrs.retain(|attr| {
if attr.path.is_ident("init") {
if attr.path().is_ident("init") {
test_kind = Some(Attr::Init);
false
} else if attr.path.is_ident("test") {
} else if attr.path().is_ident("test") {
test_kind = Some(Attr::Test);
false
} else if attr.path.is_ident("before_each") {
} else if attr.path().is_ident("before_each") {
test_kind = Some(Attr::BeforeEach);
false
} else if attr.path.is_ident("after_each") {
} else if attr.path().is_ident("after_each") {
test_kind = Some(Attr::AfterEach);
false
} else if attr.path.is_ident("should_error") {
} else if attr.path().is_ident("should_error") {
should_error = true;
false
} else if attr.path.is_ident("ignore") {
} else if attr.path().is_ident("ignore") {
ignore = true;
false
} else {
Expand Down Expand Up @@ -520,7 +520,7 @@ fn extract_cfgs(attrs: &[Attribute]) -> Vec<Attribute> {
let mut cfgs = vec![];

for attr in attrs {
if attr.path.is_ident("cfg") {
if attr.path().is_ident("cfg") {
cfgs.push(attr.clone());
}
}
Expand Down
3 changes: 1 addition & 2 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ defmt-parser = { version = "=0.3.3", path = "../parser", features = ["unstable"]
proc-macro-error = "1"
proc-macro2 = "1"
quote = "1"
# we require at least 1.0.56; see https:/knurling-rs/defmt/pull/684
syn = { version = "1.0.101", features = ["full"] }
syn = { version = "2", features = ["full"] }

[dev-dependencies]
maplit = "1"
Expand Down
2 changes: 1 addition & 1 deletion macros/src/attributes/panic_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn check_for_attribute_conflicts(
reject_list: &[&str],
) {
for attr in attrs_to_check {
if let Some(ident) = attr.path.get_ident() {
if let Some(ident) = attr.path().get_ident() {
let ident = ident.to_string();

if reject_list.contains(&ident.as_str()) {
Expand Down
84 changes: 40 additions & 44 deletions macros/src/derives/format/codegen/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Write as _;

use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::{Field, Fields, Index, Meta, NestedMeta, Type};
use syn::{Field, Fields, Index, Type};

use crate::consts;

Expand Down Expand Up @@ -87,51 +87,47 @@ enum FormatOption {
/// Returns `Err` if we can't parse a valid defmt attribute.
/// Returns `Ok(None)` if there are no `defmt` attributes on the field.
fn get_defmt_format_option(field: &Field) -> syn::Result<Option<FormatOption>> {
use syn::Error;
let attrs = field
.attrs
.iter()
.filter(|a| a.path.is_ident("defmt"))
.map(|a| a.parse_meta())
.collect::<syn::Result<Vec<_>>>()?;
if attrs.is_empty() {
return Ok(None);
} else if attrs.len() > 1 {
return Err(Error::new_spanned(
field,
"multiple `defmt` attributes not supported",
));
} // else attrs.len() == 1
let attr = &attrs[0];
let args = match attr {
Meta::List(list) => &list.nested,
bad => return Err(syn::Error::new_spanned(bad, "unrecognized attribute")),
};
if args.len() != 1 {
return Err(syn::Error::new_spanned(
attr,
"expected 1 attribute argument",
));
}
let arg = match &args[0] {
NestedMeta::Meta(Meta::Path(arg)) => arg,
bad => {
return Err(syn::Error::new_spanned(
bad,
"expected `Debug2Format` or `Display2Format`",
))
let mut format_option = None;

for attr in &field.attrs {
if attr.path().is_ident("defmt") {
if format_option.is_some() {
return Err(syn::Error::new_spanned(
field,
"multiple `defmt` attributes not supported",
));
}

let mut parsed_format = None;

attr.parse_nested_meta(|meta| {
// #[defmt(Debug2Format)]
if meta.path.is_ident("Debug2Format") {
parsed_format = Some(FormatOption::Debug2Format);
return Ok(());
}

// #[defmt(Display2Format)]
if meta.path.is_ident("Display2Format") {
parsed_format = Some(FormatOption::Display2Format);
return Ok(());
}

Err(meta.error("expected `Debug2Format` or `Display2Format`"))
})?;

if parsed_format.is_none() {
return Err(syn::Error::new_spanned(
&attr.meta,
"expected 1 attribute argument",
));
}

format_option = parsed_format;
}
};
if arg.is_ident("Debug2Format") {
Ok(Some(FormatOption::Debug2Format))
} else if arg.is_ident("Display2Format") {
Ok(Some(FormatOption::Display2Format))
} else {
Err(syn::Error::new_spanned(
arg,
"expected `Debug2Format` or `Display2Format`",
))
}

Ok(format_option)
}

/// Returns `Some` if `ty` refers to a builtin Rust type that has native support from defmt and does
Expand Down
2 changes: 1 addition & 1 deletion macros/src/items/bitflags/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn extract_cfgs(attrs: &[Attribute]) -> Vec<Attribute> {
let mut cfgs = vec![];

for attr in attrs {
if attr.path.is_ident("cfg") {
if attr.path().is_ident("cfg") {
cfgs.push(attr.clone());
}
}
Expand Down

0 comments on commit ba53b2e

Please sign in to comment.