Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation fails for syn::parse_str::<Expr>(...) in a procedural macro when providing an unexpected closing brace #1783

Closed
vi013t opened this issue Nov 20, 2024 · 0 comments · Fixed by dtolnay/proc-macro2#482

Comments

@vi013t
Copy link

vi013t commented Nov 20, 2024

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

#[proc_macro]
pub fn some_macro(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let maybe_error: Result<syn::Expr, _> = syn::parse_str("some string }");
    tokens
}

Testing in the same project as the proc macro (in the tests/ folder in the project root) with an arbitrary input string:

#[test]
pub fn test() {
    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:

fn main() {
    let value: Result<syn::Expr, _> = syn::parse_str("some string }");
}

...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.

System Information

rustc: 1.79.0
syn: v2.0.87
Toolchain: stable-x86_64-unknown-linux-gnu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant