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

Use compile_error! instead of panicking #1297

Merged
merged 2 commits into from Jun 3, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions serde_derive/src/lib.rs
Expand Up @@ -53,6 +53,7 @@ extern crate proc_macro2;

mod internals;

use std::str::FromStr;
use proc_macro::TokenStream;
use syn::DeriveInput;

Expand All @@ -71,7 +72,7 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
let input: DeriveInput = syn::parse(input).unwrap();
match ser::expand_derive_serialize(&input) {
Ok(expanded) => expanded.into(),
Err(msg) => panic!(msg),
Err(msg) => TokenStream::from_str(&format!("compile_error!({:?});", msg)).unwrap(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically macros try to avoid working with Rust source code as strings -- strings are unfriendly to our text editor or IDE when it comes to brace matching, syntax highlighting, indentation, etc. Serde's derive currently uses the quote crate instead of strings of source code.

Here it's just one line so there may not be an appreciable advantage, but I think it would be better to be consistent with the code generation done in the rest of serde_derive and use quote! here as well.

}
}

Expand All @@ -80,6 +81,6 @@ pub fn derive_deserialize(input: TokenStream) -> TokenStream {
let input: DeriveInput = syn::parse(input).unwrap();
match de::expand_derive_deserialize(&input) {
Ok(expanded) => expanded.into(),
Err(msg) => panic!(msg),
Err(msg) => TokenStream::from_str(&format!("compile_error!({:?});", msg)).unwrap(),
}
}