Skip to content

Commit 420fb2f

Browse files
committed
fix item_as_dummy
1 parent b9ac73c commit 420fb2f

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ All notable changes to this project will be documented in this file.
44
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

7-
<!-- <!-- ## [Unreleased] -->
8-
## [0.11.0] - 2024-01-27 -->
7+
## [Unreleased]
8+
### Fixed
9+
- fix `item_as_dummy` for attribute macros
10+
11+
## [0.11.0] - 2024-01-27
912
### Added
1013
- `{Function,Attribute,Derive}MacroHandler`,
1114

examples/macro/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ pub fn attr_item_as_dummy(_input: TokenStream, _item: TokenStream) -> SilentResu
1111
Err(SilentError)
1212
}
1313

14+
#[manyhow(item_as_dummy)]
15+
#[proc_macro_attribute]
16+
pub fn attr_item_as_dummy_ok(_input: TokenStream, _item: TokenStream) -> TokenStream2 {
17+
quote!()
18+
}
19+
1420
#[manyhow()]
1521
#[proc_macro_attribute]
1622
pub fn attr_no_dummy(_input: TokenStream, _item: TokenStream) -> SilentResult {

examples/macro/tests/test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ fn attr() {
66
struct ItemAsDummy;
77
_ = ItemAsDummy;
88

9+
#[attr_item_as_dummy_ok] // does not conflict with above
10+
struct ItemAsDummy;
11+
912
#[attr_no_dummy]
1013
struct ItemAsDummy; // does not conflict with above
1114

src/lib.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
//! ```
3737
//!
3838
//! # Using the `#[manyhow]` macro
39-
//! To activate the error hadling, just add [`#[manyhow]`](manyhow) above any
39+
//! To activate the error handling, just add [`#[manyhow]`](manyhow) above any
4040
//! proc-macro implementation, reducing the above example to:
4141
//!
4242
//! ```
@@ -70,8 +70,8 @@
7070
//! See [Without macros](#without-macros) to see what this expands to under the
7171
//! hood.
7272
//!
73-
//! You can also use the `#[manyhow]` attrubutes on a use statement, useful when
74-
//! moving your proc-macro implementations in seperate modules.
73+
//! You can also use the `#[manyhow]` attributes on a use statement, useful when
74+
//! moving your proc-macro implementations in separate modules.
7575
//!
7676
//! ```
7777
//! # use quote::quote;
@@ -98,7 +98,7 @@
9898
//!
9999
//! A proc macro function marked as `#[manyhow]` can take and return any
100100
//! [`TokenStream`](AnyTokenStream), and can also return `Result<TokenStream,
101-
//! E>` where `E` implments [`ToTokensError`]. As additional parameters a
101+
//! E>` where `E` implements [`ToTokensError`]. As additional parameters a
102102
//! [dummy](#dummy-mut-tokenstream) and/or [emitter](#emitter-mut-emitter) can
103103
//! be specified.
104104
//!
@@ -226,7 +226,7 @@
226226
//! macro.
227227
//! - `syn`/`syn2` **default** Enables errors for [`syn` 2.x](https://docs.rs/syn/latest/syn/).
228228
//! - `syn1` Enables errors for [`syn` 1.x](https://docs.rs/syn/1.0.109/syn/index.html).
229-
//! - `darling` Enables erros for [`darling`](https://docs.rs/darling/latest/index.html).
229+
//! - `darling` Enables errors for [`darling`](https://docs.rs/darling/latest/index.html).
230230
231231
#[cfg(feature = "macros")]
232232
pub use macros::manyhow;
@@ -290,9 +290,13 @@ macro_rules! __macro_handler {
290290
)+ $($dummy,)? implementation)
291291
{
292292
Err(tokens) => tokens.into(),
293-
Ok((output, mut tokens)) => {
293+
Ok((output, mut tokens, mut dummy)) => {
294294
match (&$crate::__private::WhatType::from(&output)).manyhow_try(output) {
295-
Err(error) => (&$crate::__private::WhatType::from(&error)).manyhow_to_tokens(error, &mut tokens),
295+
Err(error) => {
296+
dummy.extend(tokens);
297+
tokens = dummy;
298+
(&$crate::__private::WhatType::from(&error)).manyhow_to_tokens(error, &mut tokens);
299+
},
296300
Ok(output) => (&$crate::__private::WhatType::from(&output)).manyhow_to_tokens(output, &mut tokens),
297301
};
298302
tokens.into()
@@ -762,7 +766,7 @@ macro_rules! macro_input {
762766
///
763767
/// Trait is implemented for any [`function`](FnOnce), taking in
764768
#[doc = concat!($token_streams, ".")]
765-
/// Additionally they can take optionally in any order a [`&mut
769+
/// Additionally, they can take optionally in any order a [`&mut
766770
/// Emitter`](Emitter) which allows emitting errors without returning early. And
767771
/// a `&mut TokenStream` to return a dummy `TokenStream` on failure.
768772
///

src/parse_to_tokens.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ macro_rules! transparent_handlers {
144144
$($input: Result<$Input, TokenStream>,)*
145145
$($dummy: Option<impl AnyTokenStream>,)?
146146
body: impl $MacroInput<Function, $($Input = $Input,)* Dummy = Dummy, Output = Output>,
147-
) -> Result<(Output, TokenStream), TokenStream> {
147+
) -> Result<(Output, TokenStream, TokenStream), TokenStream> {
148148
// use $crate::ToTokensError as _;
149149
#[allow(unused)]
150150
let mut dummy = TokenStream::new();
@@ -157,12 +157,12 @@ macro_rules! transparent_handlers {
157157
return Err(dummy);
158158
}
159159
};)*
160-
let mut tokens = dummy.into();
160+
let mut dummy = dummy.into();
161161
let mut emitter = Emitter::new();
162-
let output = body.call($($input,)+ &mut tokens, &mut emitter);
163-
let mut tokens = tokens.into();
162+
let output = body.call($($input,)+ &mut dummy, &mut emitter);
163+
let mut tokens = TokenStream::new();
164164
emitter.to_tokens(&mut tokens);
165-
Ok((output, tokens))
165+
Ok((output, tokens, dummy.into()))
166166
}
167167
};
168168
}

0 commit comments

Comments
 (0)