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

Improve error messages on failure to derive #329

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rustler_codegen/src/ex_struct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::{Span, TokenStream};

use syn::{self, Field, Ident};
use syn::{self, spanned::Spanned, Field, Ident};

use super::context::Context;
use super::RustlerAttr;
Expand Down Expand Up @@ -70,7 +70,7 @@ fn gen_decoder(ctx: &Context, fields: &[&Field], atoms_module_name: &Ident) -> T
let atom_fun = Context::field_to_atom_fun(field);
let variable = Context::escape_ident_with_index(&ident.to_string(), index, "struct");

let assignment = quote! {
let assignment = quote_spanned! { field.span() =>
let #variable = try_decode_field(env, term, #atom_fun())?;
};

Expand Down Expand Up @@ -131,7 +131,7 @@ fn gen_encoder(ctx: &Context, fields: &[&Field], atoms_module_name: &Ident) -> T
.map(|field| {
let field_ident = field.ident.as_ref().unwrap();
let atom_fun = Context::field_to_atom_fun(field);
quote! {
quote_spanned! { field.span() =>
map = map.map_put(#atom_fun().encode(env), self.#field_ident.encode(env)).unwrap();
}
})
Expand Down
6 changes: 3 additions & 3 deletions rustler_codegen/src/map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::{Span, TokenStream};

use syn::{self, Field, Ident};
use syn::{self, spanned::Spanned, Field, Ident};

use super::context::Context;

Expand Down Expand Up @@ -64,7 +64,7 @@ fn gen_decoder(ctx: &Context, fields: &[&Field], atoms_module_name: &Ident) -> T
let atom_fun = Context::field_to_atom_fun(field);
let variable = Context::escape_ident_with_index(&ident.to_string(), index, "map");

let assignment = quote! {
let assignment = quote_spanned! { field.span() =>
let #variable = try_decode_field(env, term, #atom_fun())?;
};

Expand Down Expand Up @@ -119,7 +119,7 @@ fn gen_encoder(ctx: &Context, fields: &[&Field], atoms_module_name: &Ident) -> T
let field_ident = field.ident.as_ref().unwrap();
let atom_fun = Context::field_to_atom_fun(field);

quote! {
quote_spanned! { field.span() =>
map = map.map_put(#atom_fun().encode(env), self.#field_ident.encode(env)).unwrap();
}
})
Expand Down
6 changes: 3 additions & 3 deletions rustler_codegen/src/record.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::{Span, TokenStream};

use syn::{self, Field, Ident, Index};
use syn::{self, spanned::Spanned, Field, Ident, Index};

use super::context::Context;
use super::RustlerAttr;
Expand Down Expand Up @@ -66,7 +66,7 @@ fn gen_decoder(ctx: &Context, fields: &[&Field], atoms_module_name: &Ident) -> T

let variable = Context::escape_ident(&pos_in_struct, "record");

let assignment = quote! {
let assignment = quote_spanned! { field.span() =>
let #variable = try_decode_index(&terms, #pos_in_struct, #actual_index)?;
};

Expand Down Expand Up @@ -150,7 +150,7 @@ fn gen_encoder(ctx: &Context, fields: &[&Field], atoms_module_name: &Ident) -> T
Some(ident) => quote! { self.#ident },
};

quote! { #field_source.encode(env) }
quote_spanned! { field.span() => #field_source.encode(env) }
})
.collect();

Expand Down
6 changes: 3 additions & 3 deletions rustler_codegen/src/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::TokenStream;

use syn::{self, Field, Index};
use syn::{self, spanned::Spanned, Field, Index};

use super::context::Context;

Expand Down Expand Up @@ -51,7 +51,7 @@ fn gen_decoder(ctx: &Context, fields: &[&Field]) -> TokenStream {

let variable = Context::escape_ident(&pos_in_struct, "struct");

let assignment = quote! {
let assignment = quote_spanned! { field.span() =>
let #variable = try_decode_index(&terms, #pos_in_struct, #index)?;
};

Expand Down Expand Up @@ -120,7 +120,7 @@ fn gen_encoder(ctx: &Context, fields: &[&Field]) -> TokenStream {
Some(ident) => quote! { self.#ident },
};

quote! { #field_source.encode(env) }
quote_spanned! { field.span() => #field_source.encode(env) }
})
.collect();

Expand Down
6 changes: 4 additions & 2 deletions rustler_codegen/src/unit_enum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::{Span, TokenStream};

use heck::SnakeCase;
use syn::{self, Fields, Ident, Variant};
use syn::{self, spanned::Spanned, Fields, Ident, Variant};

use super::context::Context;

Expand All @@ -16,7 +16,9 @@ pub fn transcoder_decorator(ast: &syn::DeriveInput) -> TokenStream {
for variant in variants {
if let Fields::Unit = variant.fields {
} else {
panic!("NifUnitEnum can only be used with enums that contain all unit variants.");
return quote_spanned! { variant.span() =>
compile_error!("NifUnitEnum can only be used with enums containing unit variants.");
};
}
}

Expand Down
12 changes: 7 additions & 5 deletions rustler_codegen/src/untagged_enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::TokenStream;

use syn::{self, Fields, Variant};
use syn::{self, spanned::Spanned, Fields, Variant};

use super::context::Context;

Expand All @@ -15,12 +15,14 @@ pub fn transcoder_decorator(ast: &syn::DeriveInput) -> TokenStream {
for variant in variants {
if let Fields::Unnamed(_) = variant.fields {
if variant.fields.iter().count() != 1 {
panic!("NifUntaggedEnum can only be used with enums that contain all NewType variants.");
return quote_spanned! { variant.span() =>
compile_error!("NifUntaggedEnum can only be used with enums that contain all NewType variants.");
};
}
} else {
panic!(
"NifUntaggedEnum can only be used with enums that contain all NewType variants."
);
return quote_spanned! { variant.span() =>
compile_error!("NifUntaggedEnum can only be used with enums that contain all NewType variants.");
};
}
}

Expand Down