You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm experiencing a strange behavior in a procedural macro where some invalid expression strings seem to prevent successful compilation with syn::parse_str::<syn::Expr>(...), specifically with unexpected closing braces.
Testing in the same project as the proc macro (in the tests/ folder in the project root) with an arbitrary input string:
#[test]pubfntest(){let result = macro_example::some_macro!("some unused input");}
won't compile (via cargo test):
error: unexpected closing delimiter: `}`
--> tests/test.rs:3:19
|
3 | let result = macro_example::some_macro!("some unused input");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected closing delimiter
|
= note: this error originates in the macro `macro_example::some_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: could not compile `macro-example` (test "test") due to 1 previous error
Creating a binary crate elsewhere, adding this proc macro as a dependency, and running normally (via cargo run) also fails to compile with the same error.
Expected Behavior
My understanding of this is that the program should still compile even when passing an invalid expression to syn::parse_str, because it returns a Result and the documentation for it doesn't say anything about possible panics. This is useful in particular for checking if a string is a valid expression by pattern matching on the result of syn::parse_str. I know other bugs have been filed about internal panics in parse_str, so I assume this should be possible and that this behavior isn't expected.
Other Details
Strangely, this crash doesn't seem to occur when syn::parse_str is called outside of a proc macro, i.e., this compiles fine in a standalone binary crate with just syn as a dependency:
...which makes me wonder if this is actually just an issue with my understanding of how procedural macros work, and not with syn itself; That being said, this issue doesn't occur when calling any other function that returns a Result and doesn't panic, which does make me believe syn is somehow involved.
Description
I'm experiencing a strange behavior in a procedural macro where some invalid expression strings seem to prevent successful compilation with
syn::parse_str::<syn::Expr>(...)
, specifically with unexpected closing braces.Minimal Reproducible Example
Testing in the same project as the proc macro (in the
tests/
folder in the project root) with an arbitrary input string:won't compile (via
cargo test
):Creating a binary crate elsewhere, adding this proc macro as a dependency, and running normally (via
cargo run
) also fails to compile with the same error.Expected Behavior
My understanding of this is that the program should still compile even when passing an invalid expression to
syn::parse_str
, because it returns aResult
and the documentation for it doesn't say anything about possiblepanics
. This is useful in particular for checking if a string is a valid expression by pattern matching on the result ofsyn::parse_str
. I know other bugs have been filed about internalpanics
inparse_str
, so I assume this should be possible and that this behavior isn't expected.Other Details
Strangely, this crash doesn't seem to occur when
syn::parse_str
is called outside of a proc macro, i.e., this compiles fine in a standalone binary crate with justsyn
as a dependency:...which makes me wonder if this is actually just an issue with my understanding of how procedural macros work, and not with
syn
itself; That being said, this issue doesn't occur when calling any other function that returns aResult
and doesn'tpanic
, which does make me believesyn
is somehow involved.System Information
The text was updated successfully, but these errors were encountered: