-
-
Notifications
You must be signed in to change notification settings - Fork 298
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
syn::parse_str<Ident>()
panics inside a proc-macro on "'1"
#749
Comments
This is a compiler bug. rust-lang/rust#58736 |
A simple fix in our case is to check for invalid characters ourselves: let is_valid_ident = name.chars()
.all(|c| c.is_alphanumeric() || c == '_');
if is_valid_ident {
if let Ok(ident) = syn::parse_str(name) {
return Ok(ident)
}
}
// emit our own error |
I added a catch_unwind in proc-macro2 1.0.8, hopefully that helps! I think rustc will still write its own diagnostic out to the console from inside proc_macro::TokenStream::from_str which is not always desirable... Be aware there is a separate issue that proc_macro::TokenStream::from_str will sometimes return Ok on invalid inputs: rust-lang/rust#58772. |
Thanks for clarifying, and the quick fix! I imagine the workaround I came up with will avoid emitting the extra diagnostics noise anyway so it's probably worth keeping. |
This is pretty peculiar.
If you run the following code snippet in a regular program, it prints an error as expected:
However, if you write it in a proc-macro, the proc-macro panics:
you get the following:
I've captured the panic backtrace in a gist (
parse_str()
call is frame 18): https://gist.github.com/abonander/02fa0e207002f447ae7c5f77f687c8d6The actual error is in constructing the
ParseStream
which suggests a behavior discrepancy inproc_macro2
between running in a proc-macro vs not, however I expectedparse_str()
to emit an error instead of panic (as documented onIdent::new()
: https://docs.rs/syn/1.0.13/syn/struct.Ident.html#panics) so I leave it up to you to decide where it belongs.cc launchbadge/sqlx#53
The text was updated successfully, but these errors were encountered: