Skip to content

Commit 1e2ab81

Browse files
authored
Merge pull request #1288 from andy128k/remove-proc-macro-error
Replace usage of macro `proc_macro_error` with explicit propagation of `syn::Result`
2 parents 3bd2800 + c74a40a commit 1e2ab81

13 files changed

+235
-277
lines changed

Cargo.lock

Lines changed: 7 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deny.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ highlight = "all"
3030
unknown-registry = "deny"
3131
unknown-git = "deny"
3232

33-
# proc-macro-error depends on an old version of syn
34-
# See https://github.com/gtk-rs/gtk-rs-core/issues/1174
35-
[[bans.skip]]
36-
name = "syn"
37-
version = "1.0"
3833
# https://gitlab.redox-os.org/redox-os/syscall/-/issues/34
3934
[[bans.skip]]
4035
name = "bitflags"

glib-macros/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ rust-version = "1.70"
1313

1414
[dependencies]
1515
heck = "0.4"
16-
proc-macro-error = "1.0"
1716
proc-macro2 = "1.0"
1817
quote = "1.0"
1918
syn = { version = "2.0", features = ["full"] }

glib-macros/src/boxed_derive.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

33
use proc_macro2::{Ident, TokenStream};
4-
use proc_macro_error::abort_call_site;
54
use quote::quote;
65

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

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

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

108-
match found {
109-
Ok(None) => {
110-
abort_call_site!(
111-
"#[derive(glib::Boxed)] requires #[boxed_type(name = \"BoxedTypeName\")]"
112-
)
113-
}
114-
Err(e) => return e.to_compile_error(),
115-
Ok(_) => {}
116-
};
107+
if found.is_none() {
108+
return Err(syn::Error::new_spanned(
109+
input,
110+
"#[derive(glib::Boxed)] requires #[boxed_type(name = \"BoxedTypeName\")]",
111+
));
112+
}
117113

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

134-
quote! {
130+
Ok(quote! {
135131
impl #crate_ident::subclass::boxed::BoxedType for #name {
136132
const NAME: &'static ::core::primitive::str = #gtype_name;
137133
}
@@ -284,5 +280,5 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
284280
|name| Self::ParamSpec::builder(name)
285281
}
286282
}
287-
}
283+
})
288284
}

glib-macros/src/closure.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

33
use proc_macro2::{Ident, Span, TokenStream};
4-
use proc_macro_error::abort;
54
use quote::{quote, ToTokens, TokenStreamExt};
65
use syn::{ext::IdentExt, spanned::Spanned, Token};
76

@@ -91,17 +90,17 @@ impl syn::parse::Parse for CaptureKind {
9190
.map(|i| i.to_string())
9291
.collect::<Vec<_>>()
9392
.join("-");
94-
Ok(match keyword.as_str() {
95-
"strong" => CaptureKind::Strong,
96-
"watch" => CaptureKind::Watch,
97-
"weak-allow-none" => CaptureKind::WeakAllowNone,
98-
"to-owned" => CaptureKind::ToOwned,
99-
k => abort!(
100-
idents,
101-
"Unknown keyword `{}`, only `watch`, `weak-allow-none`, `to-owned` and `strong` are allowed",
102-
k,
103-
),
104-
})
93+
match keyword.as_str() {
94+
"strong" => Ok(CaptureKind::Strong),
95+
"watch" => Ok(CaptureKind::Watch),
96+
"weak-allow-none" => Ok(CaptureKind::WeakAllowNone),
97+
"to-owned" => Ok(CaptureKind::ToOwned),
98+
k => Err(syn::Error::new(
99+
idents.span(),
100+
format!("Unknown keyword `{}`, only `watch`, `weak-allow-none`, `to-owned` and `strong` are allowed",
101+
k),
102+
)),
103+
}
105104
}
106105
}
107106

@@ -124,18 +123,20 @@ impl syn::parse::Parse for Capture {
124123
};
125124
if alias.is_none() {
126125
if name.to_string() == "self" {
127-
abort!(
126+
return Err(syn::Error::new_spanned(
128127
name,
129128
"Can't use `self` as variable name. Try storing it in a temporary variable or \
130-
rename it using `as`."
131-
);
129+
rename it using `as`.",
130+
));
132131
}
133132
if name.to_string().contains('.') {
134-
abort!(
133+
return Err(syn::Error::new(
135134
name.span(),
136-
"`{}`: Field accesses are not allowed as is, you must rename it!",
137-
name
138-
);
135+
format!(
136+
"`{}`: Field accesses are not allowed as is, you must rename it!",
137+
name
138+
),
139+
));
139140
}
140141
}
141142
Ok(Capture {
@@ -162,11 +163,10 @@ impl syn::parse::Parse for Closure {
162163
let capture = input.parse::<Capture>()?;
163164
if capture.kind == CaptureKind::Watch {
164165
if let Some(existing) = captures.iter().find(|c| c.kind == CaptureKind::Watch) {
165-
abort!(
166-
capture.start,
167-
"Only one `@watch` capture is allowed per closure";
168-
note = existing.start => "Previous `@watch` found here"
169-
);
166+
return Err(syn::Error::new(
167+
existing.start,
168+
"Only one `@watch` capture is allowed per closure",
169+
));
170170
}
171171
}
172172
captures.push(capture);
@@ -185,13 +185,16 @@ impl syn::parse::Parse for Closure {
185185
}
186186
let mut closure = input.parse::<syn::ExprClosure>()?;
187187
if closure.asyncness.is_some() {
188-
abort!(closure, "Async closure not allowed");
188+
return Err(syn::Error::new_spanned(
189+
closure,
190+
"Async closure not allowed",
191+
));
189192
}
190193
if !captures.is_empty() && closure.capture.is_none() {
191-
abort!(
194+
return Err(syn::Error::new_spanned(
192195
closure,
193-
"Closure with captures needs to be \"moved\" so please add `move` before closure"
194-
)
196+
"Closure with captures needs to be \"moved\" so please add `move` before closure",
197+
));
195198
}
196199
let args = closure
197200
.inputs

glib-macros/src/derived_properties_attribute.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use proc_macro2::TokenStream;
4-
use proc_macro_error::abort_call_site;
3+
use proc_macro2::{Span, TokenStream};
54
use quote::quote;
65

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

10-
pub fn impl_derived_properties(input: &syn::ItemImpl) -> TokenStream {
9+
pub fn impl_derived_properties(input: &syn::ItemImpl) -> syn::Result<TokenStream> {
1110
let syn::ItemImpl {
1211
attrs,
1312
generics,
@@ -17,10 +16,10 @@ pub fn impl_derived_properties(input: &syn::ItemImpl) -> TokenStream {
1716
..
1817
} = input;
1918

20-
let trait_path = match &trait_ {
21-
Some(path) => &path.1,
22-
None => abort_call_site!(WRONG_PLACE_MSG),
23-
};
19+
let trait_path = &trait_
20+
.as_ref()
21+
.ok_or_else(|| syn::Error::new(Span::call_site(), WRONG_PLACE_MSG))?
22+
.1;
2423

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

69-
quote!(
68+
Ok(quote!(
7069
#(#attrs)*
7170
impl #generics #trait_path for #self_ty {
7271
#(#items)*
7372
#(#generated)*
7473
}
75-
)
74+
))
7675
}

0 commit comments

Comments
 (0)