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

Can't parse Stmt::Expr #1731

Open
okamt opened this issue Sep 8, 2024 · 3 comments
Open

Can't parse Stmt::Expr #1731

okamt opened this issue Sep 8, 2024 · 3 comments

Comments

@okamt
Copy link

okamt commented Sep 8, 2024

Not sure if I'm doing something wrong, but I can't get syn to parse a Stmt::Expr without a semicolon. It panics with unexpected end of input, expected semicolon.

Rust playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=cbc75065fde246a57eda32f48d40bddd

@a101010
Copy link

a101010 commented Oct 11, 2024

Yes, this should work. Final expression without semicolon is important. Is there a workaround or another way to parse this?

use syn; // 2.0.79

fn main() {
    let return_stmt = syn::parse_str::<syn::Stmt>("2")
        .expect("failed to parse return statment");
    println!("{return_stmt:?}");
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a89e0abe4ec8a0314dda0e1b077f7011

@a101010
Copy link

a101010 commented Oct 11, 2024

It should get parsed as sym::Stmt::Expr(Expr, None)

@a101010
Copy link

a101010 commented Oct 11, 2024

Oh, jaja, I guess the workaround is pretty simple. Just parse as expression and wrap it explicitly.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ad09ef9cba00784aa8506eb2e0feb4e7

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

No branches or pull requests

2 participants