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

Conversation

adamcrume
Copy link
Contributor

As a side effect, this changes error messages from:

error: proc-macro derive panicked
   --> test_suite/tests/test_ser.rs:597:10
    |
597 | #[derive(Serialize)]
    |          ^^^^^^^^^
    |
    = help: message: 2 errors:
                # unknown serde field attribute `typo`
                # expected serde with attribute to be a string: `with = "..."`

to

error: 2 errors:
    # unknown serde field attribute `typo`
    # expected serde with attribute to be a string: `with = "..."`
   --> test_suite/tests/test_ser.rs:597:10
    |
597 | #[derive(Serialize)]
    |          ^^^^^^^^^

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks! That improvement to the error message is exactly what we were looking for.

The CI build is failing because we have some compile-fail tests that check the error messages as they existed before this PR, for example this one. All of those tests will need to be updated. The //~ and //~^^ compile-fail comment syntax is explained in COMPILER_TESTS.md.

Running the compile-fail tests locally is somewhat hairy right now. You will need a nightly compiler and the workflow I would recommend is:

# in serde/test_suite/deps, after any change to serde_derive
cargo clean && cargo update && cargo build

# in serde/test_suite
cargo test --features unstable compile_fail

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

@dtolnay dtolnay merged commit 7ad3d17 into serde-rs:master Jun 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants