diff --git a/src/macros.rs b/src/macros.rs index 9ced630..752e77d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1202,6 +1202,21 @@ macro_rules! __init_internal { // have been initialized. Therefore we can now dismiss the guards by forgetting them. $(::core::mem::forget($guards);)* }; + (init_slot($($use_data:ident)?): + @data($data:ident), + @slot($slot:ident), + @guards($($guards:ident,)*), + // arbitrary code block + @munch_fields(_: { $($code:tt)* }, $($rest:tt)*), + ) => { + { $($code)* } + $crate::__init_internal!(init_slot($($use_data)?): + @data($data), + @slot($slot), + @guards($($guards,)*), + @munch_fields($($rest)*), + ); + }; (init_slot($use_data:ident): // `use_data` is present, so we use the `data` to init fields. @data($data:ident), @slot($slot:ident), @@ -1351,6 +1366,20 @@ macro_rules! __init_internal { ); } }; + (make_initializer: + @slot($slot:ident), + @type_name($t:path), + @munch_fields(_: { $($code:tt)* }, $($rest:tt)*), + @acc($($acc:tt)*), + ) => { + // code blocks are ignored for the initializer check + $crate::__init_internal!(make_initializer: + @slot($slot), + @type_name($t), + @munch_fields($($rest)*), + @acc($($acc)*), + ); + }; (make_initializer: @slot($slot:ident), @type_name($t:path), diff --git a/tests/ui/compile-fail/init/wrong_generics2.stderr b/tests/ui/compile-fail/init/wrong_generics2.stderr index d1b1e7a..cc41892 100644 --- a/tests/ui/compile-fail/init/wrong_generics2.stderr +++ b/tests/ui/compile-fail/init/wrong_generics2.stderr @@ -12,7 +12,7 @@ help: you might have forgotten to add the struct literal inside the block --> src/macros.rs | ~ ::core::ptr::write($slot, $t { SomeStruct { - |9 $($acc)* + |4 $($acc)* ~ } }); | diff --git a/tests/underscore.rs b/tests/underscore.rs new file mode 100644 index 0000000..583b3b3 --- /dev/null +++ b/tests/underscore.rs @@ -0,0 +1,31 @@ +use pin_init::{try_init, Init}; + +pub struct Foo { + x: u64, +} + +fn foo() -> bool { + false +} + +fn bar() -> bool { + true +} + +impl Foo { + pub fn new() -> impl Init { + try_init!(Self { + _: { + if foo() { + return Err(()); + } + }, + x: 0, + _: { + if bar() { + return Err(()); + } + } + }? ()) + } +}