Skip to content

Commit

Permalink
Merge pull request #1288 from andy128k/remove-proc-macro-error
Browse files Browse the repository at this point in the history
Replace usage of macro `proc_macro_error` with explicit propagation of  `syn::Result`
  • Loading branch information
sdroege authored Jan 22, 2024
2 parents 3bd2800 + c74a40a commit 1e2ab81
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 277 deletions.
55 changes: 7 additions & 48 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ highlight = "all"
unknown-registry = "deny"
unknown-git = "deny"

# proc-macro-error depends on an old version of syn
# See https://github.com/gtk-rs/gtk-rs-core/issues/1174
[[bans.skip]]
name = "syn"
version = "1.0"
# https://gitlab.redox-os.org/redox-os/syscall/-/issues/34
[[bans.skip]]
name = "bitflags"
Expand Down
1 change: 0 additions & 1 deletion glib-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ rust-version = "1.70"

[dependencies]
heck = "0.4"
proc-macro-error = "1.0"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0", features = ["full"] }
Expand Down
24 changes: 10 additions & 14 deletions glib-macros/src/boxed_derive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use proc_macro2::{Ident, TokenStream};
use proc_macro_error::abort_call_site;
use quote::quote;

use crate::utils::{crate_ident_new, parse_nested_meta_items, NestedMetaItem};
Expand Down Expand Up @@ -91,7 +90,7 @@ fn gen_impl_to_value_optional(name: &Ident, crate_ident: &TokenStream) -> TokenS
}
}

pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
pub fn impl_boxed(input: &syn::DeriveInput) -> syn::Result<TokenStream> {
let name = &input.ident;

let mut gtype_name = NestedMetaItem::<syn::LitStr>::new("name")
Expand All @@ -103,17 +102,14 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
&input.attrs,
"boxed_type",
&mut [&mut gtype_name, &mut nullable],
);
)?;

match found {
Ok(None) => {
abort_call_site!(
"#[derive(glib::Boxed)] requires #[boxed_type(name = \"BoxedTypeName\")]"
)
}
Err(e) => return e.to_compile_error(),
Ok(_) => {}
};
if found.is_none() {
return Err(syn::Error::new_spanned(
input,
"#[derive(glib::Boxed)] requires #[boxed_type(name = \"BoxedTypeName\")]",
));
}

let gtype_name = gtype_name.value.unwrap();
let nullable = nullable.found || nullable.value.map(|b| b.value()).unwrap_or(false);
Expand All @@ -131,7 +127,7 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
quote! {}
};

quote! {
Ok(quote! {
impl #crate_ident::subclass::boxed::BoxedType for #name {
const NAME: &'static ::core::primitive::str = #gtype_name;
}
Expand Down Expand Up @@ -284,5 +280,5 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
|name| Self::ParamSpec::builder(name)
}
}
}
})
}
59 changes: 31 additions & 28 deletions glib-macros/src/closure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use proc_macro2::{Ident, Span, TokenStream};
use proc_macro_error::abort;
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{ext::IdentExt, spanned::Spanned, Token};

Expand Down Expand Up @@ -91,17 +90,17 @@ impl syn::parse::Parse for CaptureKind {
.map(|i| i.to_string())
.collect::<Vec<_>>()
.join("-");
Ok(match keyword.as_str() {
"strong" => CaptureKind::Strong,
"watch" => CaptureKind::Watch,
"weak-allow-none" => CaptureKind::WeakAllowNone,
"to-owned" => CaptureKind::ToOwned,
k => abort!(
idents,
"Unknown keyword `{}`, only `watch`, `weak-allow-none`, `to-owned` and `strong` are allowed",
k,
),
})
match keyword.as_str() {
"strong" => Ok(CaptureKind::Strong),
"watch" => Ok(CaptureKind::Watch),
"weak-allow-none" => Ok(CaptureKind::WeakAllowNone),
"to-owned" => Ok(CaptureKind::ToOwned),
k => Err(syn::Error::new(
idents.span(),
format!("Unknown keyword `{}`, only `watch`, `weak-allow-none`, `to-owned` and `strong` are allowed",
k),
)),
}
}
}

Expand All @@ -124,18 +123,20 @@ impl syn::parse::Parse for Capture {
};
if alias.is_none() {
if name.to_string() == "self" {
abort!(
return Err(syn::Error::new_spanned(
name,
"Can't use `self` as variable name. Try storing it in a temporary variable or \
rename it using `as`."
);
rename it using `as`.",
));
}
if name.to_string().contains('.') {
abort!(
return Err(syn::Error::new(
name.span(),
"`{}`: Field accesses are not allowed as is, you must rename it!",
name
);
format!(
"`{}`: Field accesses are not allowed as is, you must rename it!",
name
),
));
}
}
Ok(Capture {
Expand All @@ -162,11 +163,10 @@ impl syn::parse::Parse for Closure {
let capture = input.parse::<Capture>()?;
if capture.kind == CaptureKind::Watch {
if let Some(existing) = captures.iter().find(|c| c.kind == CaptureKind::Watch) {
abort!(
capture.start,
"Only one `@watch` capture is allowed per closure";
note = existing.start => "Previous `@watch` found here"
);
return Err(syn::Error::new(
existing.start,
"Only one `@watch` capture is allowed per closure",
));
}
}
captures.push(capture);
Expand All @@ -185,13 +185,16 @@ impl syn::parse::Parse for Closure {
}
let mut closure = input.parse::<syn::ExprClosure>()?;
if closure.asyncness.is_some() {
abort!(closure, "Async closure not allowed");
return Err(syn::Error::new_spanned(
closure,
"Async closure not allowed",
));
}
if !captures.is_empty() && closure.capture.is_none() {
abort!(
return Err(syn::Error::new_spanned(
closure,
"Closure with captures needs to be \"moved\" so please add `move` before closure"
)
"Closure with captures needs to be \"moved\" so please add `move` before closure",
));
}
let args = closure
.inputs
Expand Down
17 changes: 8 additions & 9 deletions glib-macros/src/derived_properties_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use proc_macro2::TokenStream;
use proc_macro_error::abort_call_site;
use proc_macro2::{Span, TokenStream};
use quote::quote;

pub const WRONG_PLACE_MSG: &str =
"This macro should be used on `impl` block for `glib::ObjectImpl` trait";

pub fn impl_derived_properties(input: &syn::ItemImpl) -> TokenStream {
pub fn impl_derived_properties(input: &syn::ItemImpl) -> syn::Result<TokenStream> {
let syn::ItemImpl {
attrs,
generics,
Expand All @@ -17,10 +16,10 @@ pub fn impl_derived_properties(input: &syn::ItemImpl) -> TokenStream {
..
} = input;

let trait_path = match &trait_ {
Some(path) => &path.1,
None => abort_call_site!(WRONG_PLACE_MSG),
};
let trait_path = &trait_
.as_ref()
.ok_or_else(|| syn::Error::new(Span::call_site(), WRONG_PLACE_MSG))?
.1;

let mut has_property = false;
let mut has_properties = false;
Expand Down Expand Up @@ -66,11 +65,11 @@ pub fn impl_derived_properties(input: &syn::ItemImpl) -> TokenStream {
(!has_property).then_some(property),
];

quote!(
Ok(quote!(
#(#attrs)*
impl #generics #trait_path for #self_ty {
#(#items)*
#(#generated)*
}
)
))
}
Loading

0 comments on commit 1e2ab81

Please sign in to comment.