Skip to content

Commit 26eec96

Browse files
committed
fix type ambiguity with clonable error types
1 parent 29319cf commit 26eec96

File tree

6 files changed

+292
-165
lines changed

6 files changed

+292
-165
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
<!-- ## [Unreleased] -->
8+
### Added
9+
- `{Function,Attribute,Derive}MacroHandler`,
10+
11+
### Removed
12+
- **Breaking Change** `MacroHandler` was replaced by dedicated traits for each macro type, this probably doesn't affect any usages
13+
14+
### Fixed
15+
- `syn::Result` could not be used as return type of macro handlers
16+
817
## [0.10.4] - 2023-11-24
918
### Changed
1019
- Allow parsing of types that do not implement `ToTokens`

examples/macro/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ pub fn parse_quote_dummy_error(_: TokenStream) -> Result<syn::Ident> {
158158
bail!("error message")
159159
}
160160

161+
#[manyhow(input_as_dummy)]
162+
#[proc_macro]
163+
pub fn parse_quote_dummy_error_syn_result(_: TokenStream) -> syn::Result<syn::Ident> {
164+
bail!("error message")
165+
}
166+
161167
#[manyhow]
162168
#[proc_macro_attribute]
163169
pub fn parse_quote_attribute(_: syn::LitStr, item: syn::DeriveInput) -> syn::DeriveInput {
@@ -176,8 +182,20 @@ pub fn parse_quote_dummy_error_attribute(_: TokenStream, _: TokenStream) -> Resu
176182
bail!("error message")
177183
}
178184

185+
#[manyhow(item_as_dummy)]
186+
#[proc_macro_attribute]
187+
pub fn parse_quote_dummy_error_attribute_syn_result(_: TokenStream, _: TokenStream) -> syn::Result<syn::Ident> {
188+
bail!("error message")
189+
}
190+
179191
#[manyhow]
180192
#[proc_macro_derive(ParseQuote)]
181193
pub fn parse_quote_derive(item: syn::ItemStruct) -> syn::ItemStruct {
182194
item
183195
}
196+
197+
#[manyhow]
198+
#[proc_macro_derive(ParseQuoteSynResult)]
199+
pub fn parse_quote_derive_syn_result(item: syn::ItemStruct) -> syn::Result<syn::ItemStruct> {
200+
Ok(item)
201+
}

examples/no_macro/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ pub fn parse_quote_dummy_error(input: TokenStream) -> TokenStream {
148148
)
149149
}
150150

151+
#[proc_macro]
152+
pub fn parse_quote_dummy_error_syn_result(input: TokenStream) -> TokenStream {
153+
function!(
154+
#[as_dummy]
155+
input,
156+
|_: TokenStream| -> syn::Result<syn::Ident> { bail!("error message") }
157+
)
158+
}
159+
151160
#[proc_macro_attribute]
152161
pub fn parse_quote_attribute(input: TokenStream, item: TokenStream) -> TokenStream {
153162
attribute!(input, item, |_: syn::LitStr, item: syn::DeriveInput| item)
@@ -173,7 +182,25 @@ pub fn parse_quote_dummy_error_attribute(input: TokenStream, item: TokenStream)
173182
)
174183
}
175184

185+
#[proc_macro_attribute]
186+
pub fn parse_quote_dummy_error_attribute_syn_result(input: TokenStream, item: TokenStream) -> TokenStream {
187+
attribute!(
188+
input,
189+
#[as_dummy]
190+
item,
191+
|_: TokenStream, _: TokenStream| -> syn::Result<syn::Ident> { bail!("error message") }
192+
)
193+
}
194+
176195
#[proc_macro_derive(ParseQuote)]
177196
pub fn parse_quote_derive(item: TokenStream) -> TokenStream {
178197
derive!(item, |item: syn::ItemStruct| item)
179198
}
199+
200+
#[proc_macro_derive(ParseQuoteSynResult)]
201+
pub fn parse_quote_derive_syn_result(item: TokenStream) -> TokenStream {
202+
derive!(
203+
item,
204+
|item: syn::ItemStruct| -> syn::Result<syn::ItemStruct> { Ok(item) }
205+
)
206+
}

src/error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use syn1::Error as Syn1Error;
1414
use syn2::Error as Syn2Error;
1515

1616
#[cfg(doc)]
17-
use crate::MacroHandler;
17+
use crate::MacroOutput;
1818
use crate::{to_tokens_span_range, SpanRanged};
1919

2020
/// An alias for [`Result`](std::result::Result) suited for use with this crate
@@ -85,8 +85,8 @@ impl<I: ToTokensError + 'static> Extend<I> for Error {
8585
/// Can take additional attachments like [`help`](Self::help) or
8686
/// [`note`](Self::note).
8787
///
88-
/// Implements `ToTokensError` and can therefore be returned from
89-
/// [`MacroHandler`]s.
88+
/// Implements `ToTokensError` and can therefore be used with
89+
/// [`MacroOutput`]s.
9090
#[derive(Debug)]
9191
#[must_use]
9292
pub struct ErrorMessage {
@@ -275,8 +275,8 @@ impl<I: ToTokensError + 'static> Extend<I> for Emitter {
275275
}
276276
}
277277

278-
/// Error that can be converted to a [`TokenStream`] required to be returned by
279-
/// a [`MacroHandler`]
278+
/// Error that can be converted to a [`TokenStream`] required to be used with
279+
/// [`MacroOutput`]
280280
///
281281
/// This trait is equivalent to [`ToTokens`].
282282
pub trait ToTokensError: Debug {

0 commit comments

Comments
 (0)