Improve overall quality of `compile_error!` errors
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Also updates UI tests.
authored by hcpl · committed by hcpl · parent 8f3f073 · Browse files · 69 files·+714 added−429 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
69 files+714 added−429 removed
- tests:+291 added−206 removed
- other:+423 added−223 removed
Provenance
- Commit
034db9f- Tree
dffbae6- Author
hcpl <hcpl.prog@gmail.com>- Committer
hcpl <hcpl.prog@gmail.com>- Parent
8f3f073
69 files changed+714 added−429 removed
modified:serde_derive/src/internals/ast.rs+9 added-3 removed
15 unmodified lines16 /// The contents of the struct or enum.17 pub data: Data<'a>,18 /// Any generics on the struct or enum.19 pub generics: &'a syn::Generics,20+ /// Original input.21+ pub original: &'a syn::DeriveInput,22 }23 24 /// The fields of a struct or enum.25 ///8 unmodified lines34 pub ident: syn::Ident,35 pub attrs: attr::Variant,36 pub style: Style,37 pub fields: Vec<Field<'a>>,38+ pub original: &'a syn::Variant,39 }40 41 /// A field of a struct.42 pub struct Field<'a> {16 unmodified lines59 }60 61 impl<'a> Container<'a> {62 /// Convert the raw Syn ast into a parsed container object, collecting errors in `cx`.60- pub fn from_ast(cx: &Ctxt, item: &'a syn::DeriveInput, derive: Derive) -> Container<'a> {63+ pub fn from_ast(cx: &Ctxt, item: &'a syn::DeriveInput, derive: Derive) -> Option<Container<'a>> {64 let mut attrs = attr::Container::from_ast(cx, item);65 66 let mut data = match item.data {67 syn::Data::Enum(ref data) => {3 unmodified lines71 let (style, fields) = struct_from_ast(cx, &data.fields, None, attrs.default());72 Data::Struct(style, fields)73 }74 syn::Data::Union(_) => {72- panic!("Serde does not support derive for unions");75+ cx.error_spanned_by(item, "Serde does not support derive for unions");76+ return None;77 }78 };79 80 let mut has_flatten = false;27 unmodified lines108 ident: item.ident.clone(),109 attrs: attrs,110 data: data,111 generics: &item.generics,112+ original: item,113 };114 check::check(cx, &mut item, derive);110- item115+ Some(item)116 }117 }118 119 impl<'a> Data<'a> {26 unmodified lines146 ident: variant.ident.clone(),147 attrs: attrs,148 style: style,149 fields: fields,150+ original: variant,151 }152 })153 .collect()154 }44 unmodified linesmodified:serde_derive/src/internals/attr.rs+290 added-152 removed
1 use internals::Ctxt;2 use proc_macro2::{Group, Span, TokenStream, TokenTree};3+use quote::ToTokens;4 use std::collections::BTreeSet;5 use std::str::FromStr;6 use syn;7 use syn::parse::{self, Parse, ParseStream};11 unmodified lines19 // rather than just the first.20 21 pub use internals::case::RenameRule;22 22-#[derive(Copy, Clone)]23+#[derive(Clone)]24 struct Attr<'c, T> {25 cx: &'c Ctxt,26 name: &'static str,27+ tokens: TokenStream,28 value: Option<T>,29 }30 31 impl<'c, T> Attr<'c, T> {32 fn none(cx: &'c Ctxt, name: &'static str) -> Self {33 Attr {34 cx: cx,35 name: name,36+ tokens: TokenStream::new(),37 value: None,38 }39 }40 38- fn set(&mut self, value: T) {41+ fn set<A: ToTokens>(&mut self, obj: A, value: T) {42+ let tokens = obj.into_token_stream();43+44 if self.value.is_some() {40- self.cx41- .error(format!("duplicate serde attribute `{}`", self.name));45+ self.cx.error_spanned_by(46+ tokens,47+ format!("duplicate serde attribute `{}`", self.name),48+ );49 } else {50+ self.tokens = tokens;51 self.value = Some(value);52 }53 }54 47- fn set_opt(&mut self, value: Option<T>) {55+ fn set_opt<A: ToTokens>(&mut self, obj: A, value: Option<T>) {56 if let Some(value) = value {49- self.set(value);57+ self.set(obj, value);58 }59 }60 61 fn set_if_none(&mut self, value: T) {4 unmodified lines66 67 fn get(self) -> Option<T> {68 self.value69 }70+71+ fn get_with_tokens(self) -> Option<(TokenStream, T)> {72+ match self.value {73+ Some(v) => Some((self.tokens, v)),74+ None => None,75+ }76+ }77 }78 79 struct BoolAttr<'c>(Attr<'c, ()>);80 1 unmodified line82 fn none(cx: &'c Ctxt, name: &'static str) -> Self {83 BoolAttr(Attr::none(cx, name))84 }85 71- fn set_true(&mut self) {72- self.0.set(());86+ fn set_true<A: ToTokens>(&mut self, obj: A) {87+ self.0.set(obj, ());88 }89 90 fn get(&self) -> bool {91 self.0.value.is_some()121 unmodified lines213 match meta_item {214 // Parse `#[serde(rename = "foo")]`215 Meta(NameValue(ref m)) if m.ident == "rename" => {216 if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {202- ser_name.set(s.value());203- de_name.set(s.value());217+ ser_name.set(&m.ident, s.value());218+ de_name.set(&m.ident, s.value());219 }220 }221 222 // Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`223 Meta(List(ref m)) if m.ident == "rename" => {224 if let Ok((ser, de)) = get_renames(cx, &m.nested) {210- ser_name.set_opt(ser.map(syn::LitStr::value));211- de_name.set_opt(de.map(syn::LitStr::value));225+ ser_name.set_opt(&m.ident, ser.map(syn::LitStr::value));226+ de_name.set_opt(&m.ident, de.map(syn::LitStr::value));227 }228 }229 230 // Parse `#[serde(rename_all = "foo")]`231 Meta(NameValue(ref m)) if m.ident == "rename_all" => {232 if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {233 match RenameRule::from_str(&s.value()) {219- Ok(rename_rule) => rename_all.set(rename_rule),220- Err(()) => cx.error(format!(234+ Ok(rename_rule) => rename_all.set(&m.ident, rename_rule),235+ Err(()) => cx.error_spanned_by(s, format!(236 "unknown rename rule for #[serde(rename_all \237 = {:?})]",223- s.value()238+ s.value(),239 )),240 }241 }242 }243 244 // Parse `#[serde(transparent)]`245 Meta(Word(ref word)) if word == "transparent" => {231- transparent.set_true();246+ transparent.set_true(word);247 }248 249 // Parse `#[serde(deny_unknown_fields)]`250 Meta(Word(ref word)) if word == "deny_unknown_fields" => {236- deny_unknown_fields.set_true();251+ deny_unknown_fields.set_true(word);252 }253 254 // Parse `#[serde(default)]`255 Meta(Word(ref word)) if word == "default" => match item.data {241- syn::Data::Struct(syn::DataStruct {242- fields: syn::Fields::Named(_),243- ..244- }) => {245- default.set(Default::Default);246- }247- _ => cx.error(256+ syn::Data::Struct(syn::DataStruct { ref fields, .. }) => match *fields {257+ syn::Fields::Named(_) => {258+ default.set(word, Default::Default);259+ }260+ syn::Fields::Unnamed(_) | syn::Fields::Unit => cx.error_spanned_by(261+ fields,262+ "#[serde(default)] can only be used on structs \263+ with named fields",264+ )265+ },266+ syn::Data::Enum(syn::DataEnum { ref enum_token, .. }) => cx.error_spanned_by(267+ enum_token,268 "#[serde(default)] can only be used on structs \269 with named fields",270 ),271+ syn::Data::Union(syn::DataUnion { ref union_token, .. }) => cx.error_spanned_by(272+ union_token,273+ "#[serde(default)] can only be used on structs \274+ with named fields",275+ ),276 },277 278 // Parse `#[serde(default = "...")]`279 Meta(NameValue(ref m)) if m.ident == "default" => {280 if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {281 match item.data {257- syn::Data::Struct(syn::DataStruct {258- fields: syn::Fields::Named(_),259- ..260- }) => {261- default.set(Default::Path(path));262- }263- _ => cx.error(282+ syn::Data::Struct(syn::DataStruct { ref fields, .. }) => match *fields {283+ syn::Fields::Named(_) => {284+ default.set(&m.ident, Default::Path(path));285+ }286+ syn::Fields::Unnamed(_) | syn::Fields::Unit => cx.error_spanned_by(287+ fields,288+ "#[serde(default = \"...\")] can only be used \289+ on structs with named fields",290+ )291+ },292+ syn::Data::Enum(syn::DataEnum { ref enum_token, .. }) => cx.error_spanned_by(293+ enum_token,294 "#[serde(default = \"...\")] can only be used \295 on structs with named fields",296 ),297+ syn::Data::Union(syn::DataUnion { ref union_token, .. }) => cx.error_spanned_by(298+ union_token,299+ "#[serde(default = \"...\")] can only be used \300+ on structs with named fields",301+ ),302 }303 }304 }305 1 unmodified line307 Meta(NameValue(ref m)) if m.ident == "bound" => {308 if let Ok(where_predicates) =309 parse_lit_into_where(cx, &m.ident, &m.ident, &m.lit)310 {276- ser_bound.set(where_predicates.clone());277- de_bound.set(where_predicates);311+ ser_bound.set(&m.ident, where_predicates.clone());312+ de_bound.set(&m.ident, where_predicates);313 }314 }315 316 // Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`317 Meta(List(ref m)) if m.ident == "bound" => {318 if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {284- ser_bound.set_opt(ser);285- de_bound.set_opt(de);319+ ser_bound.set_opt(&m.ident, ser);320+ de_bound.set_opt(&m.ident, de);321 }322 }323 324 // Parse `#[serde(untagged)]`325 Meta(Word(ref word)) if word == "untagged" => match item.data {326 syn::Data::Enum(_) => {292- untagged.set_true();327+ untagged.set_true(word);328 }294- syn::Data::Struct(_) | syn::Data::Union(_) => {295- cx.error("#[serde(untagged)] can only be used on enums")329+ syn::Data::Struct(syn::DataStruct { ref struct_token, .. }) => {330+ cx.error_spanned_by(331+ struct_token,332+ "#[serde(untagged)] can only be used on enums",333+ );334 }335+ syn::Data::Union(syn::DataUnion { ref union_token, .. }) => {336+ cx.error_spanned_by(337+ union_token,338+ "#[serde(untagged)] can only be used on enums",339+ );340+ }341 },342 343 // Parse `#[serde(tag = "type")]`344 Meta(NameValue(ref m)) if m.ident == "tag" => {345 if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {346 match item.data {347 syn::Data::Enum(_) => {304- internal_tag.set(s.value());348+ internal_tag.set(&m.ident, s.value());349 }306- syn::Data::Struct(_) | syn::Data::Union(_) => {307- cx.error("#[serde(tag = \"...\")] can only be used on enums")308- }350+ syn::Data::Struct(syn::DataStruct { ref struct_token, .. }) => {351+ cx.error_spanned_by(352+ struct_token,353+ "#[serde(tag = \"...\")] can only be used on enums",354+ );355+ },356+ syn::Data::Union(syn::DataUnion { ref union_token, .. }) => {357+ cx.error_spanned_by(358+ union_token,359+ "#[serde(tag = \"...\")] can only be used on enums",360+ );361+ },362 }363 }364 }365 1 unmodified line367 Meta(NameValue(ref m)) if m.ident == "content" => {368 if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {369 match item.data {370 syn::Data::Enum(_) => {318- content.set(s.value());371+ content.set(&m.ident, s.value());372 }320- syn::Data::Struct(_) | syn::Data::Union(_) => cx.error(321- "#[serde(content = \"...\")] can only be used on \322- enums",323- ),373+ syn::Data::Struct(syn::DataStruct { ref struct_token, .. }) => {374+ cx.error_spanned_by(375+ struct_token,376+ "#[serde(content = \"...\")] can only be used on enums",377+ );378+ },379+ syn::Data::Union(syn::DataUnion { ref union_token, .. }) => {380+ cx.error_spanned_by(381+ union_token,382+ "#[serde(content = \"...\")] can only be used on enums",383+ );384+ },385 }386 }387 }388 389 // Parse `#[serde(from = "Type")]390 Meta(NameValue(ref m)) if m.ident == "from" => {391 if let Ok(from_ty) = parse_lit_into_ty(cx, &m.ident, &m.lit) {331- type_from.set_opt(Some(from_ty));392+ type_from.set_opt(&m.ident, Some(from_ty));393 }394 }395 396 // Parse `#[serde(into = "Type")]397 Meta(NameValue(ref m)) if m.ident == "into" => {398 if let Ok(into_ty) = parse_lit_into_ty(cx, &m.ident, &m.lit) {338- type_into.set_opt(Some(into_ty));399+ type_into.set_opt(&m.ident, Some(into_ty));400 }401 }402 403 // Parse `#[serde(remote = "...")]`404 Meta(NameValue(ref m)) if m.ident == "remote" => {405 if let Ok(path) = parse_lit_into_path(cx, &m.ident, &m.lit) {406 if is_primitive_path(&path, "Self") {346- remote.set(item.ident.clone().into());407+ remote.set(&m.ident, item.ident.clone().into());408 } else {348- remote.set(path);409+ remote.set(&m.ident, path);410 }411 }412 }413 414 // Parse `#[serde(field_identifier)]`415 Meta(Word(ref word)) if word == "field_identifier" => {355- field_identifier.set_true();416+ field_identifier.set_true(word);417 }418 419 // Parse `#[serde(variant_identifier)]`420 Meta(Word(ref word)) if word == "variant_identifier" => {360- variant_identifier.set_true();421+ variant_identifier.set_true(word);422 }423 424 Meta(ref meta_item) => {364- cx.error(format!(425+ cx.error_spanned_by(meta_item.name(), format!(426 "unknown serde container attribute `{}`",427 meta_item.name()428 ));429 }430 370- Literal(_) => {371- cx.error("unexpected literal in serde container attribute");431+ Literal(ref lit) => {432+ cx.error_spanned_by(lit, "unexpected literal in serde container attribute");433 }434 }435 }436 }8 unmodified lines445 default: default.get().unwrap_or(Default::None),446 rename_all: rename_all.get().unwrap_or(RenameRule::None),447 ser_bound: ser_bound.get(),448 de_bound: de_bound.get(),388- tag: decide_tag(cx, item, &untagged, internal_tag, content),449+ tag: decide_tag(cx, item, untagged, internal_tag, content),450 type_from: type_from.get(),451 type_into: type_into.get(),452 remote: remote.get(),392- identifier: decide_identifier(cx, item, &field_identifier, &variant_identifier),453+ identifier: decide_identifier(cx, item, field_identifier, variant_identifier),454 has_flatten: false,455 }456 }457 56 unmodified lines514 515 fn decide_tag(516 cx: &Ctxt,517 item: &syn::DeriveInput,457- untagged: &BoolAttr,518+ untagged: BoolAttr,519 internal_tag: Attr<String>,520 content: Attr<String>,521 ) -> EnumTag {461- match (untagged.get(), internal_tag.get(), content.get()) {462- (false, None, None) => EnumTag::External,463- (true, None, None) => EnumTag::None,464- (false, Some(tag), None) => {522+ match (untagged.0.get_with_tokens(), internal_tag.get_with_tokens(), content.get_with_tokens()) {523+ (None, None, None) => EnumTag::External,524+ (Some(_), None, None) => EnumTag::None,525+ (None, Some((_, tag)), None) => {526 // Check that there are no tuple variants.527 if let syn::Data::Enum(ref data) = item.data {528 for variant in &data.variants {529 match variant.fields {530 syn::Fields::Named(_) | syn::Fields::Unit => {}531 syn::Fields::Unnamed(ref fields) => {532 if fields.unnamed.len() != 1 {472- cx.error(533+ cx.error_spanned_by(534+ variant,535 "#[serde(tag = \"...\")] cannot be used with tuple \536 variants",537 );538 break;3 unmodified lines542 }543 }544 EnumTag::Internal { tag: tag }545 }484- (true, Some(_), None) => {485- cx.error("enum cannot be both untagged and internally tagged");546+ (Some((untagged_tokens, _)), Some((tag_tokens, _)), None) => {547+ cx.error_spanned_by(548+ untagged_tokens,549+ "enum cannot be both untagged and internally tagged",550+ );551+ cx.error_spanned_by(552+ tag_tokens,553+ "enum cannot be both untagged and internally tagged",554+ );555 EnumTag::External // doesn't matter, will error556 }488- (false, None, Some(_)) => {489- cx.error("#[serde(tag = \"...\", content = \"...\")] must be used together");557+ (None, None, Some((content_tokens, _))) => {558+ cx.error_spanned_by(559+ content_tokens,560+ "#[serde(tag = \"...\", content = \"...\")] must be used together",561+ );562 EnumTag::External563 }492- (true, None, Some(_)) => {493- cx.error("untagged enum cannot have #[serde(content = \"...\")]");564+ (Some((untagged_tokens, _)), None, Some((content_tokens, _))) => {565+ cx.error_spanned_by(566+ untagged_tokens,567+ "untagged enum cannot have #[serde(content = \"...\")]",568+ );569+ cx.error_spanned_by(570+ content_tokens,571+ "untagged enum cannot have #[serde(content = \"...\")]",572+ );573 EnumTag::External574 }496- (false, Some(tag), Some(content)) => EnumTag::Adjacent {575+ (None, Some((_, tag)), Some((_, content))) => EnumTag::Adjacent {576 tag: tag,577 content: content,578 },500- (true, Some(_), Some(_)) => {501- cx.error("untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]");579+ (Some((untagged_tokens, _)), Some((tag_tokens, _)), Some((content_tokens, _))) => {580+ cx.error_spanned_by(581+ untagged_tokens,582+ "untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]",583+ );584+ cx.error_spanned_by(585+ tag_tokens,586+ "untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]",587+ );588+ cx.error_spanned_by(589+ content_tokens,590+ "untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]",591+ );592 EnumTag::External593 }594 }595 }596 597 fn decide_identifier(598 cx: &Ctxt,599 item: &syn::DeriveInput,510- field_identifier: &BoolAttr,511- variant_identifier: &BoolAttr,600+ field_identifier: BoolAttr,601+ variant_identifier: BoolAttr,602 ) -> Identifier {513- match (&item.data, field_identifier.get(), variant_identifier.get()) {514- (_, false, false) => Identifier::No,515- (_, true, true) => {516- cx.error("`field_identifier` and `variant_identifier` cannot both be set");603+ match (&item.data, field_identifier.0.get_with_tokens(), variant_identifier.0.get_with_tokens()) {604+ (_, None, None) => Identifier::No,605+ (_, Some((field_identifier_tokens, _)), Some((variant_identifier_tokens, _))) => {606+ cx.error_spanned_by(607+ field_identifier_tokens,608+ "#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set",609+ );610+ cx.error_spanned_by(611+ variant_identifier_tokens,612+ "#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set",613+ );614 Identifier::No615 }519- (&syn::Data::Enum(_), true, false) => Identifier::Field,520- (&syn::Data::Enum(_), false, true) => Identifier::Variant,521- (&syn::Data::Struct(_), true, false) | (&syn::Data::Union(_), true, false) => {522- cx.error("`field_identifier` can only be used on an enum");616+ (&syn::Data::Enum(_), Some(_), None) => Identifier::Field,617+ (&syn::Data::Enum(_), None, Some(_)) => Identifier::Variant,618+ (&syn::Data::Struct(syn::DataStruct { ref struct_token, .. }), Some(_), None) => {619+ cx.error_spanned_by(620+ struct_token,621+ "#[serde(field_identifier)] can only be used on an enum",622+ );623 Identifier::No624 }525- (&syn::Data::Struct(_), false, true) | (&syn::Data::Union(_), false, true) => {526- cx.error("`variant_identifier` can only be used on an enum");625+ (&syn::Data::Union(syn::DataUnion { ref union_token, .. }), Some(_), None) => {626+ cx.error_spanned_by(627+ union_token,628+ "#[serde(field_identifier)] can only be used on an enum",629+ );630 Identifier::No631 }632+ (&syn::Data::Struct(syn::DataStruct { ref struct_token, .. }), None, Some(_)) => {633+ cx.error_spanned_by(634+ struct_token,635+ "#[serde(variant_identifier)] can only be used on an enum",636+ );637+ Identifier::No638+ }639+ (&syn::Data::Union(syn::DataUnion { ref union_token, .. }), None, Some(_)) => {640+ cx.error_spanned_by(641+ union_token,642+ "#[serde(variant_identifier)] can only be used on an enum",643+ );644+ Identifier::No645+ }646 }647 }648 649 /// Represents variant attribute information31 unmodified lines681 match meta_item {682 // Parse `#[serde(rename = "foo")]`683 Meta(NameValue(ref m)) if m.ident == "rename" => {684 if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {568- ser_name.set(s.value());569- de_name.set(s.value());685+ ser_name.set(&m.ident, s.value());686+ de_name.set(&m.ident, s.value());687 }688 }689 690 // Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`691 Meta(List(ref m)) if m.ident == "rename" => {692 if let Ok((ser, de)) = get_renames(cx, &m.nested) {576- ser_name.set_opt(ser.map(syn::LitStr::value));577- de_name.set_opt(de.map(syn::LitStr::value));693+ ser_name.set_opt(&m.ident, ser.map(syn::LitStr::value));694+ de_name.set_opt(&m.ident, de.map(syn::LitStr::value));695 }696 }697 698 // Parse `#[serde(rename_all = "foo")]`699 Meta(NameValue(ref m)) if m.ident == "rename_all" => {700 if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {701 match RenameRule::from_str(&s.value()) {585- Ok(rename_rule) => rename_all.set(rename_rule),586- Err(()) => cx.error(format!(702+ Ok(rename_rule) => rename_all.set(&m.ident, rename_rule),703+ Err(()) => cx.error_spanned_by(s, format!(704 "unknown rename rule for #[serde(rename_all \705 = {:?})]",706 s.value()707 )),2 unmodified lines710 }711 712 // Parse `#[serde(skip)]`713 Meta(Word(ref word)) if word == "skip" => {597- skip_serializing.set_true();598- skip_deserializing.set_true();714+ skip_serializing.set_true(word);715+ skip_deserializing.set_true(word);716 }717 718 // Parse `#[serde(skip_deserializing)]`719 Meta(Word(ref word)) if word == "skip_deserializing" => {603- skip_deserializing.set_true();720+ skip_deserializing.set_true(word);721 }722 723 // Parse `#[serde(skip_serializing)]`724 Meta(Word(ref word)) if word == "skip_serializing" => {608- skip_serializing.set_true();725+ skip_serializing.set_true(word);726 }727 728 // Parse `#[serde(other)]`729 Meta(Word(ref word)) if word == "other" => {613- other.set_true();730+ other.set_true(word);731 }732 733 // Parse `#[serde(bound = "T: SomeBound")]`734 Meta(NameValue(ref m)) if m.ident == "bound" => {735 if let Ok(where_predicates) =736 parse_lit_into_where(cx, &m.ident, &m.ident, &m.lit)737 {621- ser_bound.set(where_predicates.clone());622- de_bound.set(where_predicates);738+ ser_bound.set(&m.ident, where_predicates.clone());739+ de_bound.set(&m.ident, where_predicates);740 }741 }742 743 // Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`744 Meta(List(ref m)) if m.ident == "bound" => {745 if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {629- ser_bound.set_opt(ser);630- de_bound.set_opt(de);746+ ser_bound.set_opt(&m.ident, ser);747+ de_bound.set_opt(&m.ident, de);748 }749 }750 751 // Parse `#[serde(with = "...")]`3 unmodified lines755 ser_path756 .path757 .segments758 .push(Ident::new("serialize", Span::call_site()).into());642- serialize_with.set(ser_path);759+ serialize_with.set(&m.ident, ser_path);760 let mut de_path = path;761 de_path762 .path763 .segments764 .push(Ident::new("deserialize", Span::call_site()).into());648- deserialize_with.set(de_path);765+ deserialize_with.set(&m.ident, de_path);766 }767 }768 769 // Parse `#[serde(serialize_with = "...")]`770 Meta(NameValue(ref m)) if m.ident == "serialize_with" => {771 if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {655- serialize_with.set(path);772+ serialize_with.set(&m.ident, path);773 }774 }775 776 // Parse `#[serde(deserialize_with = "...")]`777 Meta(NameValue(ref m)) if m.ident == "deserialize_with" => {778 if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {662- deserialize_with.set(path);779+ deserialize_with.set(&m.ident, path);780 }781 }782 783 // Defer `#[serde(borrow)]` and `#[serde(borrow = "'a + 'b")]`784 Meta(ref m) if m.name() == "borrow" => match variant.fields {785 syn::Fields::Unnamed(ref fields) if fields.unnamed.len() == 1 => {669- borrow.set(m.clone());786+ borrow.set(m.name(), m.clone());787 }788 _ => {672- cx.error("#[serde(borrow)] may only be used on newtype variants");789+ cx.error_spanned_by(790+ variant,791+ "#[serde(borrow)] may only be used on newtype variants",792+ );793 }794 },795 796 Meta(ref meta_item) => {677- cx.error(format!(797+ cx.error_spanned_by(meta_item.name(), format!(798 "unknown serde variant attribute `{}`",799 meta_item.name()800 ));801 }802 683- Literal(_) => {684- cx.error("unexpected literal in serde variant attribute");803+ Literal(ref lit) => {804+ cx.error_spanned_by(805+ lit,806+ "unexpected literal in serde variant attribute",807+ );808 }809 }810 }811 }147 unmodified lines959 match meta_item {960 // Parse `#[serde(rename = "foo")]`961 Meta(NameValue(ref m)) if m.ident == "rename" => {962 if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {840- ser_name.set(s.value());841- de_name.set(s.value());963+ ser_name.set(&m.ident, s.value());964+ de_name.set(&m.ident, s.value());965 }966 }967 968 // Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`969 Meta(List(ref m)) if m.ident == "rename" => {970 if let Ok((ser, de)) = get_renames(cx, &m.nested) {848- ser_name.set_opt(ser.map(syn::LitStr::value));849- de_name.set_opt(de.map(syn::LitStr::value));971+ ser_name.set_opt(&m.ident, ser.map(syn::LitStr::value));972+ de_name.set_opt(&m.ident, de.map(syn::LitStr::value));973 }974 }975 976 // Parse `#[serde(default)]`977 Meta(Word(ref word)) if word == "default" => {855- default.set(Default::Default);978+ default.set(word, Default::Default);979 }980 981 // Parse `#[serde(default = "...")]`982 Meta(NameValue(ref m)) if m.ident == "default" => {983 if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {861- default.set(Default::Path(path));984+ default.set(&m.ident, Default::Path(path));985 }986 }987 988 // Parse `#[serde(skip_serializing)]`989 Meta(Word(ref word)) if word == "skip_serializing" => {867- skip_serializing.set_true();990+ skip_serializing.set_true(word);991 }992 993 // Parse `#[serde(skip_deserializing)]`994 Meta(Word(ref word)) if word == "skip_deserializing" => {872- skip_deserializing.set_true();995+ skip_deserializing.set_true(word);996 }997 998 // Parse `#[serde(skip)]`999 Meta(Word(ref word)) if word == "skip" => {877- skip_serializing.set_true();878- skip_deserializing.set_true();1000+ skip_serializing.set_true(word);1001+ skip_deserializing.set_true(word);1002 }1003 1004 // Parse `#[serde(skip_serializing_if = "...")]`1005 Meta(NameValue(ref m)) if m.ident == "skip_serializing_if" => {1006 if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {884- skip_serializing_if.set(path);1007+ skip_serializing_if.set(&m.ident, path);1008 }1009 }1010 1011 // Parse `#[serde(serialize_with = "...")]`1012 Meta(NameValue(ref m)) if m.ident == "serialize_with" => {1013 if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {891- serialize_with.set(path);1014+ serialize_with.set(&m.ident, path);1015 }1016 }1017 1018 // Parse `#[serde(deserialize_with = "...")]`1019 Meta(NameValue(ref m)) if m.ident == "deserialize_with" => {1020 if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {898- deserialize_with.set(path);1021+ deserialize_with.set(&m.ident, path);1022 }1023 }1024 1025 // Parse `#[serde(with = "...")]`3 unmodified lines1029 ser_path1030 .path1031 .segments1032 .push(Ident::new("serialize", Span::call_site()).into());910- serialize_with.set(ser_path);1033+ serialize_with.set(&m.ident, ser_path);1034 let mut de_path = path;1035 de_path1036 .path1037 .segments1038 .push(Ident::new("deserialize", Span::call_site()).into());916- deserialize_with.set(de_path);1039+ deserialize_with.set(&m.ident, de_path);1040 }1041 }1042 1043 // Parse `#[serde(bound = "T: SomeBound")]`1044 Meta(NameValue(ref m)) if m.ident == "bound" => {1045 if let Ok(where_predicates) =1046 parse_lit_into_where(cx, &m.ident, &m.ident, &m.lit)1047 {925- ser_bound.set(where_predicates.clone());926- de_bound.set(where_predicates);1048+ ser_bound.set(&m.ident, where_predicates.clone());1049+ de_bound.set(&m.ident, where_predicates);1050 }1051 }1052 1053 // Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`1054 Meta(List(ref m)) if m.ident == "bound" => {1055 if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {933- ser_bound.set_opt(ser);934- de_bound.set_opt(de);1056+ ser_bound.set_opt(&m.ident, ser);1057+ de_bound.set_opt(&m.ident, de);1058 }1059 }1060 1061 // Parse `#[serde(borrow)]`1062 Meta(Word(ref word)) if word == "borrow" => {940- if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, &field.ty) {941- borrowed_lifetimes.set(borrowable);1063+ if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, field) {1064+ borrowed_lifetimes.set(word, borrowable);1065 }1066 }1067 1068 // Parse `#[serde(borrow = "'a + 'b")]`1069 Meta(NameValue(ref m)) if m.ident == "borrow" => {1070 if let Ok(lifetimes) = parse_lit_into_lifetimes(cx, &m.ident, &m.lit) {948- if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, &field.ty) {1071+ if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, field) {1072 for lifetime in &lifetimes {1073 if !borrowable.contains(lifetime) {951- cx.error(format!(1074+ cx.error_spanned_by(field, format!(1075 "field `{}` does not have lifetime {}",1076 ident, lifetime1077 ));1078 }1079 }957- borrowed_lifetimes.set(lifetimes);1080+ borrowed_lifetimes.set(&m.ident, lifetimes);1081 }1082 }1083 }1084 1085 // Parse `#[serde(getter = "...")]`1086 Meta(NameValue(ref m)) if m.ident == "getter" => {1087 if let Ok(path) = parse_lit_into_expr_path(cx, &m.ident, &m.lit) {965- getter.set(path);1088+ getter.set(&m.ident, path);1089 }1090 }1091 1092 // Parse `#[serde(flatten)]`1093 Meta(Word(ref word)) if word == "flatten" => {971- flatten.set_true();1094+ flatten.set_true(word);1095 }1096 1097 Meta(ref meta_item) => {975- cx.error(format!(1098+ cx.error_spanned_by(meta_item.name(), format!(1099 "unknown serde field attribute `{}`",1100 meta_item.name()1101 ));1102 }1103 981- Literal(_) => {982- cx.error("unexpected literal in serde field attribute");1104+ Literal(ref lit) => {1105+ cx.error_spanned_by(1106+ lit,1107+ "unexpected literal in serde field attribute",1108+ );1109 }1110 }1111 }1112 }174 unmodified lines1287 for meta in metas {1288 match *meta {1289 Meta(NameValue(ref meta)) if meta.ident == "serialize" => {1290 if let Ok(v) = f(cx, &attr_name, &meta.ident, &meta.lit) {1165- ser_meta.set(v);1291+ ser_meta.set(&meta.ident, v);1292 }1293 }1294 1295 Meta(NameValue(ref meta)) if meta.ident == "deserialize" => {1296 if let Ok(v) = f(cx, &attr_name, &meta.ident, &meta.lit) {1171- de_meta.set(v);1297+ de_meta.set(&meta.ident, v);1298 }1299 }1300 1301 _ => {1176- cx.error(format!(1302+ cx.error_spanned_by(meta, format!(1303 "malformed {0} attribute, expected `{0}(serialize = ..., \1304 deserialize = ...)`",1305 attr_name1306 ));41 unmodified lines1348 ) -> Result<&'a syn::LitStr, ()> {1349 if let syn::Lit::Str(ref lit) = *lit {1350 Ok(lit)1351 } else {1226- cx.error(format!(1352+ cx.error_spanned_by(lit, format!(1353 "expected serde {} attribute to be a string: `{} = \"...\"`",1354 attr_name, meta_item_name1355 ));1356 Err(())2 unmodified lines1359 1360 fn parse_lit_into_path(cx: &Ctxt, attr_name: &Ident, lit: &syn::Lit) -> Result<syn::Path, ()> {1361 let string = try!(get_lit_str(cx, attr_name, attr_name, lit));1362 parse_lit_str(string)1237- .map_err(|_| cx.error(format!("failed to parse path: {:?}", string.value())))1363+ .map_err(|_| cx.error_spanned_by(1364+ lit,1365+ format!("failed to parse path: {:?}", string.value()),1366+ ))1367 }1368 1369 fn parse_lit_into_expr_path(1370 cx: &Ctxt,1 unmodified line1372 lit: &syn::Lit,1373 ) -> Result<syn::ExprPath, ()> {1374 let string = try!(get_lit_str(cx, attr_name, attr_name, lit));1375 parse_lit_str(string)1247- .map_err(|_| cx.error(format!("failed to parse path: {:?}", string.value())))1376+ .map_err(|_| cx.error_spanned_by(1377+ lit,1378+ format!("failed to parse path: {:?}", string.value()),1379+ ))1380 }1381 1382 fn parse_lit_into_where(1383 cx: &Ctxt,9 unmodified lines1393 let where_string = syn::LitStr::new(&format!("where {}", string.value()), string.span());1394 1395 parse_lit_str::<syn::WhereClause>(&where_string)1396 .map(|wh| wh.predicates.into_iter().collect())1265- .map_err(|err| cx.error(err))1397+ .map_err(|err| cx.error_spanned_by(lit, err))1398 }1399 1400 fn parse_lit_into_ty(cx: &Ctxt, attr_name: &Ident, lit: &syn::Lit) -> Result<syn::Type, ()> {1401 let string = try!(get_lit_str(cx, attr_name, attr_name, lit));1402 1403 parse_lit_str(string).map_err(|_| {1272- cx.error(format!(1404+ cx.error_spanned_by(lit, format!(1405 "failed to parse type: {} = {:?}",1406 attr_name,1407 string.value()1408 ))8 unmodified lines1417 lit: &syn::Lit,1418 ) -> Result<BTreeSet<syn::Lifetime>, ()> {1419 let string = try!(get_lit_str(cx, attr_name, attr_name, lit));1420 if string.value().is_empty() {1289- cx.error("at least one lifetime must be borrowed");1421+ cx.error_spanned_by(lit, "at least one lifetime must be borrowed");1422 return Err(());1423 }1424 1425 struct BorrowedLifetimes(Punctuated<syn::Lifetime, Token![+]>);7 unmodified lines1433 if let Ok(BorrowedLifetimes(lifetimes)) = parse_lit_str(string) {1434 let mut set = BTreeSet::new();1435 for lifetime in lifetimes {1436 if !set.insert(lifetime.clone()) {1305- cx.error(format!("duplicate borrowed lifetime `{}`", lifetime));1437+ cx.error_spanned_by(1438+ lit,1439+ format!("duplicate borrowed lifetime `{}`", lifetime),1440+ );1441 }1442 }1443 return Ok(set);1444 }1445 1311- cx.error(format!(1446+ cx.error_spanned_by(lit, format!(1447 "failed to parse borrowed lifetimes: {:?}",1448 string.value()1449 ));1450 Err(())144 unmodified lines1595 // attribute on the field so there must be at least one borrowable lifetime.1596 fn borrowable_lifetimes(1597 cx: &Ctxt,1598 name: &str,1464- ty: &syn::Type,1599+ field: &syn::Field,1600 ) -> Result<BTreeSet<syn::Lifetime>, ()> {1601 let mut lifetimes = BTreeSet::new();1467- collect_lifetimes(ty, &mut lifetimes);1602+ collect_lifetimes(&field.ty, &mut lifetimes);1603 if lifetimes.is_empty() {1469- cx.error(format!("field `{}` has no lifetimes to borrow", name));1604+ cx.error_spanned_by(1605+ field,1606+ format!("field `{}` has no lifetimes to borrow", name),1607+ );1608 Err(())1609 } else {1610 Ok(lifetimes)1611 }89 unmodified linesmodified:serde_derive/src/internals/check.rs+89 added-37 removed
19 unmodified lines20 fn check_getter(cx: &Ctxt, cont: &Container) {21 match cont.data {22 Data::Enum(_) => {23 if cont.data.has_getter() {24- cx.error("#[serde(getter = \"...\")] is not allowed in an enum");24+ cx.error_spanned_by(25+ cont.original,26+ "#[serde(getter = \"...\")] is not allowed in an enum",27+ );28 }29 }30 Data::Struct(_, _) => {31 if cont.data.has_getter() && cont.attrs.remote().is_none() {29- cx.error(32+ cx.error_spanned_by(33+ cont.original,34 "#[serde(getter = \"...\")] can only be used in structs \35 that have #[serde(remote = \"...\")]",36 );37 }24 unmodified lines62 return;63 }64 match style {65 Style::Tuple => {62- cx.error("#[serde(flatten)] cannot be used on tuple structs");66+ cx.error_spanned_by(67+ field.original,68+ "#[serde(flatten)] cannot be used on tuple structs",69+ );70 }71 Style::Newtype => {65- cx.error("#[serde(flatten)] cannot be used on newtype structs");72+ cx.error_spanned_by(73+ field.original,74+ "#[serde(flatten)] cannot be used on newtype structs",75+ );76 }77 _ => {}78 }79 if field.attrs.skip_serializing() {70- cx.error(71- "#[serde(flatten] can not be combined with \80+ cx.error_spanned_by(81+ field.original,82+ "#[serde(flatten)] can not be combined with \83 #[serde(skip_serializing)]",84 );85 } else if field.attrs.skip_serializing_if().is_some() {75- cx.error(76- "#[serde(flatten] can not be combined with \86+ cx.error_spanned_by(87+ field.original,88+ "#[serde(flatten)] can not be combined with \89 #[serde(skip_serializing_if = \"...\")]",90 );91 } else if field.attrs.skip_deserializing() {80- cx.error(81- "#[serde(flatten] can not be combined with \92+ cx.error_spanned_by(93+ field.original,94+ "#[serde(flatten)] can not be combined with \95 #[serde(skip_deserializing)]",96 );97 }98 }20 unmodified lines119 cont.attrs.tag(),120 ) {121 // The `other` attribute may not be used in a variant_identifier.122 (_, Identifier::Variant, true, _) => {110- cx.error("#[serde(other)] may not be used on a variant_identifier");123+ cx.error_spanned_by(124+ variant.original,125+ "#[serde(other)] may not be used on a variant identifier",126+ );127 }128 129 // Variant with `other` attribute cannot appear in untagged enum130 (_, Identifier::No, true, &EnumTag::None) => {115- cx.error("#[serde(other)] cannot appear on untagged enum");131+ cx.error_spanned_by(132+ variant.original,133+ "#[serde(other)] cannot appear on untagged enum",134+ );135 }136 137 // Variant with `other` attribute must be the last one.138 (Style::Unit, Identifier::Field, true, _) | (Style::Unit, Identifier::No, true, _) => {139 if i < variants.len() - 1 {121- cx.error("#[serde(other)] must be the last variant");140+ cx.error_spanned_by(141+ variant.original,142+ "#[serde(other)] must be on the last variant",143+ );144 }145 }146 147 // Variant with `other` attribute must be a unit variant.148 (_, Identifier::Field, true, _) | (_, Identifier::No, true, _) => {127- cx.error("#[serde(other)] must be on a unit variant");149+ cx.error_spanned_by(150+ variant.original,151+ "#[serde(other)] must be on a unit variant",152+ );153 }154 155 // Any sort of variant is allowed if this is not an identifier.156 (_, Identifier::No, false, _) => {}3 unmodified lines160 161 // The last field is allowed to be a newtype catch-all.162 (Style::Newtype, Identifier::Field, false, _) => {163 if i < variants.len() - 1 {139- cx.error(format!("`{}` must be the last variant", variant.ident));164+ cx.error_spanned_by(165+ variant.original,166+ format!("`{}` must be the last variant", variant.ident),167+ );168 }169 }170 171 (_, Identifier::Field, false, _) => {144- cx.error("field_identifier may only contain unit variants");172+ cx.error_spanned_by(173+ variant.original,174+ "#[serde(field_identifier)] may only contain unit variants",175+ );176 }177 178 (_, Identifier::Variant, false, _) => {148- cx.error("variant_identifier may only contain unit variants");179+ cx.error_spanned_by(180+ variant.original,181+ "#[serde(variant_identifier)] may only contain unit variants",182+ );183 }184 }185 }186 }10 unmodified lines197 198 for variant in variants.iter() {199 if variant.attrs.serialize_with().is_some() {200 if variant.attrs.skip_serializing() {167- cx.error(format!(201+ cx.error_spanned_by(variant.original, format!(202 "variant `{}` cannot have both #[serde(serialize_with)] and \203 #[serde(skip_serializing)]",204 variant.ident205 ));2 unmodified lines208 for field in &variant.fields {209 let member = member_message(&field.member);210 211 if field.attrs.skip_serializing() {178- cx.error(format!(212+ cx.error_spanned_by(variant.original, format!(213 "variant `{}` cannot have both #[serde(serialize_with)] and \214 a field {} marked with #[serde(skip_serializing)]",215 variant.ident, member216 ));217 }218 219 if field.attrs.skip_serializing_if().is_some() {186- cx.error(format!(220+ cx.error_spanned_by(variant.original, format!(221 "variant `{}` cannot have both #[serde(serialize_with)] and \222 a field {} marked with #[serde(skip_serializing_if)]",223 variant.ident, member224 ));2 unmodified lines227 }228 229 if variant.attrs.deserialize_with().is_some() {230 if variant.attrs.skip_deserializing() {197- cx.error(format!(231+ cx.error_spanned_by(variant.original, format!(232 "variant `{}` cannot have both #[serde(deserialize_with)] and \233 #[serde(skip_deserializing)]",234 variant.ident235 ));2 unmodified lines238 for field in &variant.fields {239 if field.attrs.skip_deserializing() {240 let member = member_message(&field.member);241 208- cx.error(format!(242+ cx.error_spanned_by(variant.original, format!(243 "variant `{}` cannot have both #[serde(deserialize_with)] \244 and a field {} marked with #[serde(skip_deserializing)]",245 variant.ident, member246 ));17 unmodified lines264 EnumTag::Internal { ref tag } => tag.as_str(),265 EnumTag::External | EnumTag::Adjacent { .. } | EnumTag::None => return,266 };267 234- let diagnose_conflict = || {235- let message = format!("variant field name `{}` conflicts with internal tag", tag);236- cx.error(message);237- };268+ let diagnose_conflict = || cx.error_spanned_by(269+ cont.original,270+ format!("variant field name `{}` conflicts with internal tag", tag),271+ );272 273 for variant in variants {274 match variant.style {275 Style::Struct => {26 unmodified lines302 EnumTag::Internal { .. } | EnumTag::External | EnumTag::None => return,303 };304 305 if type_tag == content_tag {272- let message = format!(306+ cx.error_spanned_by(cont.original, format!(307 "enum tags `{}` for type and content conflict with each other",308 type_tag275- );276- cx.error(message);309+ ));310 }311 }312 313 /// Enums and unit structs cannot be transparent.2 unmodified lines316 return;317 }318 319 if cont.attrs.type_from().is_some() {287- cx.error("#[serde(transparent)] is not allowed with #[serde(from = \"...\")]");320+ cx.error_spanned_by(321+ cont.original,322+ "#[serde(transparent)] is not allowed with #[serde(from = \"...\")]",323+ );324 }325 326 if cont.attrs.type_into().is_some() {291- cx.error("#[serde(transparent)] is not allowed with #[serde(into = \"...\")]");327+ cx.error_spanned_by(328+ cont.original,329+ "#[serde(transparent)] is not allowed with #[serde(into = \"...\")]",330+ );331 }332 333 let fields = match cont.data {334 Data::Enum(_) => {296- cx.error("#[serde(transparent)] is not allowed on an enum");335+ cx.error_spanned_by(336+ cont.original,337+ "#[serde(transparent)] is not allowed on an enum",338+ );339 return;340 }341 Data::Struct(Style::Unit, _) => {300- cx.error("#[serde(transparent)] is not allowed on a unit struct");342+ cx.error_spanned_by(343+ cont.original,344+ "#[serde(transparent)] is not allowed on a unit struct",345+ );346 return;347 }348 Data::Struct(_, ref mut fields) => fields,349 };2 unmodified lines352 353 for field in fields {354 if allow_transparent(field, derive) {355 if transparent_field.is_some() {311- cx.error(356+ cx.error_spanned_by(357+ cont.original,358 "#[serde(transparent)] requires struct to have at most one transparent field",359 );360 return;361 }4 unmodified lines366 match transparent_field {367 Some(transparent_field) => transparent_field.attrs.mark_transparent(),368 None => match derive {369 Derive::Serialize => {324- cx.error("#[serde(transparent)] requires at least one field that is not skipped");370+ cx.error_spanned_by(371+ cont.original,372+ "#[serde(transparent)] requires at least one field that is not skipped",373+ );374 }375 Derive::Deserialize => {327- cx.error("#[serde(transparent)] requires at least one field that is neither skipped nor has a default");376+ cx.error_spanned_by(377+ cont.original,378+ "#[serde(transparent)] requires at least one field that is neither skipped nor has a default",379+ );380 }381 },382 }383 }384 385 fn member_message(member: &Member) -> String {386 match *member {387 Member::Named(ref ident) => format!("`{}`", ident),336- Member::Unnamed(ref i) => i.index.to_string(),388+ Member::Unnamed(ref i) => format!("#{}", i.index),389 }390 }391 392 fn allow_transparent(field: &Field, derive: Derive) -> bool {13 unmodified linesmodified:serde_derive/src/internals/ctxt.rs+12 added-15 removed
1+use quote::ToTokens;2 use std::cell::RefCell;3 use std::fmt::Display;4 use std::thread;5+use syn;6 7 /// A type to collect errors together and format them.8 ///9 /// Dropping this object will cause a panic. It must be consumed using `check`.2 unmodified lines12 #[derive(Default)]13 pub struct Ctxt {14 // The contents will be set to `None` during checking. This is so that checking can be15 // enforced.14- errors: RefCell<Option<Vec<String>>>,16+ errors: RefCell<Option<Vec<syn::Error>>>,17 }18 19 impl Ctxt {20 /// Create a new context object.4 unmodified lines25 errors: RefCell::new(Some(Vec::new())),26 }27 }28 27- /// Add an error to the context object.28- pub fn error<T: Display>(&self, msg: T) {29+ /// Add an error to the context object with a tokenenizable object.30+ ///31+ /// The object is used for spanning in error messages.32+ pub fn error_spanned_by<A: ToTokens, T: Display>(&self, obj: A, msg: T) {33 self.errors34 .borrow_mut()35 .as_mut()36 .unwrap()33- .push(msg.to_string());37+ // Curb monomorphization from generating too many identical methods.38+ .push(syn::Error::new_spanned(obj.into_token_stream(), msg));39 }40 41 /// Consume this object, producing a formatted error string if there are errors.37- pub fn check(self) -> Result<(), String> {38- let mut errors = self.errors.borrow_mut().take().unwrap();42+ pub fn check(self) -> Result<(), Vec<syn::Error>> {43+ let errors = self.errors.borrow_mut().take().unwrap();44 match errors.len() {45 0 => Ok(()),41- 1 => Err(errors.pop().unwrap()),42- n => {43- let mut msg = format!("{} errors:", n);44- for err in errors {45- msg.push_str("\n\t# ");46- msg.push_str(&err);47- }48- Err(msg)49- }46+ _ => Err(errors),47 }48 }49 }50 7 unmodified linesmodified:serde_derive/src/de.rs+10 added-4 removed
11 unmodified lines12 use try;13 14 use std::collections::BTreeSet;15 16-pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<TokenStream, String> {16+pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<TokenStream, Vec<syn::Error>> {17 let ctxt = Ctxt::new();18- let cont = Container::from_ast(&ctxt, input, Derive::Deserialize);18+ let cont = match Container::from_ast(&ctxt, input, Derive::Deserialize) {19+ Some(cont) => cont,20+ None => return Err(ctxt.check().unwrap_err()),21+ };22 precondition(&ctxt, &cont);23 try!(ctxt.check());24 25 let ident = &cont.ident;62 unmodified lines88 fn precondition_sized(cx: &Ctxt, cont: &Container) {89 if let Data::Struct(_, ref fields) = cont.data {90 if let Some(last) = fields.last() {91 if let syn::Type::Slice(_) = *last.ty {89- cx.error("cannot deserialize a dynamically sized struct");92+ cx.error_spanned_by(cont.original, "cannot deserialize a dynamically sized struct");93 }94 }95 }96 }1 unmodified line98 fn precondition_no_de_lifetime(cx: &Ctxt, cont: &Container) {99 if let BorrowedLifetimes::Borrowed(_) = borrowed_lifetimes(cont) {100 for param in cont.generics.lifetimes() {101 if param.lifetime.to_string() == "'de" {99- cx.error("cannot deserialize when there is a lifetime parameter called 'de");102+ cx.error_spanned_by(103+ ¶m.lifetime,104+ "cannot deserialize when there is a lifetime parameter called 'de",105+ );106 return;107 }108 }109 }2875 unmodified linesmodified:serde_derive/src/lib.rs+5 added-7 removed
60 unmodified lines61 mod internals;62 63 use proc_macro::TokenStream;64 use syn::DeriveInput;65-use syn::spanned::Spanned;65 66 #[macro_use]67 mod bound;68 #[macro_use]7 unmodified lines76 #[proc_macro_derive(Serialize, attributes(serde))]77 pub fn derive_serialize(input: TokenStream) -> TokenStream {78 let input = parse_macro_input!(input as DeriveInput);79 ser::expand_derive_serialize(&input)81- .unwrap_or_else(|message| compile_error(input.span(), message))80+ .unwrap_or_else(to_compile_errors)81 .into()82 }83 84 #[proc_macro_derive(Deserialize, attributes(serde))]85 pub fn derive_deserialize(input: TokenStream) -> TokenStream {86 let input = parse_macro_input!(input as DeriveInput);87 de::expand_derive_deserialize(&input)89- .unwrap_or_else(|message| compile_error(input.span(), message))88+ .unwrap_or_else(to_compile_errors)89 .into()90 }91 93-fn compile_error(span: proc_macro2::Span, message: String) -> proc_macro2::TokenStream {94- quote_spanned! {span=>95- compile_error!(#message);96- }92+fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {93+ let compile_errors = errors.iter().map(syn::Error::to_compile_error);94+ quote!(#(#compile_errors)*)95 }modified:serde_derive/src/ser.rs+7 added-4 removed
7 unmodified lines8 use internals::{attr, Ctxt, Derive};9 use pretend;10 use try;11 12-pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream, String> {12+pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream, Vec<syn::Error>> {13 let ctxt = Ctxt::new();14- let cont = Container::from_ast(&ctxt, input, Derive::Serialize);14+ let cont = match Container::from_ast(&ctxt, input, Derive::Serialize) {15+ Some(cont) => cont,16+ None => return Err(ctxt.check().unwrap_err()),17+ };18 precondition(&ctxt, &cont);19 try!(ctxt.check());20 21 let ident = &cont.ident;52 unmodified lines74 fn precondition(cx: &Ctxt, cont: &Container) {75 match cont.attrs.identifier() {76 attr::Identifier::No => {}77 attr::Identifier::Field => {75- cx.error("field identifiers cannot be serialized");78+ cx.error_spanned_by(cont.original, "field identifiers cannot be serialized");79 }80 attr::Identifier::Variant => {78- cx.error("variant identifiers cannot be serialized");81+ cx.error_spanned_by(cont.original, "variant identifiers cannot be serialized");82 }83 }84 }85 1214 unmodified linesmodified:serde_derive/Cargo.toml+1 added-1 removed
24 unmodified lines25262728292930313224 unmodified lines
[dependencies]proc-macro2 = "0.4"quote = "0.6.3"syn = { version = "0.15", features = ["visit"] }syn = { version = "0.15.22", features = ["visit"] }
[dev-dependencies]serde = { version = "1.0", path = "../serde" }modified:test_suite/tests/ui/borrow/bad_lifetimes.stderr+3 added-3 removed
1 error: failed to parse borrowed lifetimes: "zzz"2- --> $DIR/bad_lifetimes.rs:4:102+ --> $DIR/bad_lifetimes.rs:6:223 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | #[serde(borrow = "zzz")]5+ | ^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/borrow/duplicate_lifetime.stderr+3 added-3 removed
1 error: duplicate borrowed lifetime `'a`2- --> $DIR/duplicate_lifetime.rs:4:102+ --> $DIR/duplicate_lifetime.rs:6:223 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | #[serde(borrow = "'a + 'a")]5+ | ^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/borrow/duplicate_variant.stderr+3 added-3 removed
1 error: duplicate serde attribute `borrow`2- --> $DIR/duplicate_variant.rs:7:102+ --> $DIR/duplicate_variant.rs:9:133 |4-7 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+9 | #[serde(borrow)]5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/borrow/empty_lifetimes.stderr+3 added-3 removed
1 error: at least one lifetime must be borrowed2- --> $DIR/empty_lifetimes.rs:4:102+ --> $DIR/empty_lifetimes.rs:6:223 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | #[serde(borrow = "")]5+ | ^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/borrow/no_lifetimes.stderr+4 added-3 removed
1 error: field `s` has no lifetimes to borrow2- --> $DIR/no_lifetimes.rs:4:102+ --> $DIR/no_lifetimes.rs:6:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | / #[serde(borrow)]5+7 | | s: String,6+ | |_____________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/borrow/struct_variant.stderr+5 added-4 removed
1 error: #[serde(borrow)] may only be used on newtype variants2- --> $DIR/struct_variant.rs:7:103- |4-7 | #[derive(Deserialize)]5- | ^^^^^^^^^^^2+ --> $DIR/struct_variant.rs:9:53+ |4+9 | / #[serde(borrow)]5+10 | | S { s: Str<'a> },6+ | |____________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/borrow/wrong_lifetime.stderr+4 added-3 removed
1 error: field `s` does not have lifetime 'b2- --> $DIR/wrong_lifetime.rs:4:102+ --> $DIR/wrong_lifetime.rs:6:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | / #[serde(borrow = "'b")]5+7 | | s: &'a str,6+ | |______________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/conflict/adjacent-tag.stderr+7 added-3 removed
1 error: enum tags `conflict` for type and content conflict with each other2- --> $DIR/adjacent-tag.rs:4:102+ --> $DIR/adjacent-tag.rs:5:13 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | / #[serde(tag = "conflict", content = "conflict")]5+6 | | enum E {6+7 | | A,7+8 | | B,8+9 | | }9+ | |_^10 11 error: aborting due to previous error12 modified:test_suite/tests/ui/conflict/flatten-newtype-struct.stderr+3 added-3 removed
1 error: #[serde(flatten)] cannot be used on newtype structs2- --> $DIR/flatten-newtype-struct.rs:4:102+ --> $DIR/flatten-newtype-struct.rs:5:123 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | struct Foo(#[serde(flatten)] HashMap<String, String>);5+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/conflict/flatten-skip-deserializing.stderr+5 added-4 removed
1-error: #[serde(flatten] can not be combined with #[serde(skip_deserializing)]2- --> $DIR/flatten-skip-deserializing.rs:4:101+error: #[serde(flatten)] can not be combined with #[serde(skip_deserializing)]2+ --> $DIR/flatten-skip-deserializing.rs:6:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | / #[serde(flatten, skip_deserializing)]5+7 | | other: Other,6+ | |________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/conflict/flatten-skip-serializing-if.stderr+5 added-4 removed
1-error: #[serde(flatten] can not be combined with #[serde(skip_serializing_if = "...")]2- --> $DIR/flatten-skip-serializing-if.rs:4:101+error: #[serde(flatten)] can not be combined with #[serde(skip_serializing_if = "...")]2+ --> $DIR/flatten-skip-serializing-if.rs:6:53 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | / #[serde(flatten, skip_serializing_if = "Option::is_none")]5+7 | | other: Option<Other>,6+ | |________________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/conflict/flatten-skip-serializing.stderr+5 added-4 removed
1-error: #[serde(flatten] can not be combined with #[serde(skip_serializing)]2- --> $DIR/flatten-skip-serializing.rs:4:101+error: #[serde(flatten)] can not be combined with #[serde(skip_serializing)]2+ --> $DIR/flatten-skip-serializing.rs:6:53 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | / #[serde(flatten, skip_serializing)]5+7 | | other: Other,6+ | |________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/conflict/flatten-tuple-struct.stderr+3 added-3 removed
1 error: #[serde(flatten)] cannot be used on tuple structs2- --> $DIR/flatten-tuple-struct.rs:4:102+ --> $DIR/flatten-tuple-struct.rs:5:173 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | struct Foo(u32, #[serde(flatten)] HashMap<String, String>);5+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/conflict/internal-tag.stderr+10 added-4 removed
1 error: variant field name `conflict` conflicts with internal tag2- --> $DIR/internal-tag.rs:4:103- |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^2+ --> $DIR/internal-tag.rs:5:13+ |4+5 | / #[serde(tag = "conflict")]5+6 | | enum E {6+7 | | A {7+8 | | #[serde(rename = "conflict")]8+9 | | x: (),9+10 | | },10+11 | | }11+ | |_^12 13 error: aborting due to previous error14 modified:test_suite/tests/ui/default-attribute/enum.stderr+3 added-3 removed
1 error: #[serde(default)] can only be used on structs with named fields2- --> $DIR/enum.rs:4:102+ --> $DIR/enum.rs:6:13 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | enum E {5+ | ^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/default-attribute/nameless_struct_fields.stderr+3 added-3 removed
1 error: #[serde(default)] can only be used on structs with named fields2- --> $DIR/nameless_struct_fields.rs:4:102+ --> $DIR/nameless_struct_fields.rs:6:93 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | struct T(u8, u8);5+ | ^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/duplicate-attribute/rename-and-ser.stderr+3 added-3 removed
1 error: unknown serde field attribute `serialize`2- --> $DIR/rename-and-ser.rs:4:102+ --> $DIR/rename-and-ser.rs:6:273 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | #[serde(rename = "x", serialize = "y")]5+ | ^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/duplicate-attribute/rename-rename-de.stderr+3 added-3 removed
1 error: duplicate serde attribute `rename`2- --> $DIR/rename-rename-de.rs:4:102+ --> $DIR/rename-rename-de.rs:7:133 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+7 | #[serde(rename(deserialize = "y"))]5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/duplicate-attribute/rename-ser-rename-ser.stderr+3 added-3 removed
1 error: duplicate serde attribute `rename`2- --> $DIR/rename-ser-rename-ser.rs:4:102+ --> $DIR/rename-ser-rename-ser.rs:6:383 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | #[serde(rename(serialize = "x"), rename(serialize = "y"))]5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/duplicate-attribute/rename-ser-rename.stderr+3 added-3 removed
1 error: duplicate serde attribute `rename`2- --> $DIR/rename-ser-rename.rs:4:102+ --> $DIR/rename-ser-rename.rs:7:133 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+7 | #[serde(rename = "y")]5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/duplicate-attribute/rename-ser-ser.stderr+3 added-3 removed
1 error: duplicate serde attribute `rename`2- --> $DIR/rename-ser-ser.rs:4:102+ --> $DIR/rename-ser-ser.rs:6:373 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | #[serde(rename(serialize = "x", serialize = "y"))]5+ | ^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/duplicate-attribute/two-rename-ser.stderr+3 added-3 removed
1 error: duplicate serde attribute `rename`2- --> $DIR/two-rename-ser.rs:4:102+ --> $DIR/two-rename-ser.rs:7:133 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+7 | #[serde(rename(serialize = "y"))]5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/duplicate-attribute/with-and-serialize-with.stderr+3 added-3 removed
1 error: duplicate serde attribute `serialize_with`2- --> $DIR/with-and-serialize-with.rs:4:102+ --> $DIR/with-and-serialize-with.rs:6:253 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | #[serde(with = "w", serialize_with = "s")]5+ | ^^^^^^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/enum-representation/internal-tuple-variant.stderr+3 added-3 removed
1 error: #[serde(tag = "...")] cannot be used with tuple variants2- --> $DIR/internal-tuple-variant.rs:4:102+ --> $DIR/internal-tuple-variant.rs:7:53 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+7 | Tuple(u8, u8),5+ | ^^^^^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/enum-representation/internally-tagged-struct.stderr+3 added-3 removed
1 error: #[serde(tag = "...")] can only be used on enums2- --> $DIR/internally-tagged-struct.rs:4:102+ --> $DIR/internally-tagged-struct.rs:6:13 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | struct S;5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/enum-representation/untagged-and-internal.stderr+10 added-4 removed
1 error: enum cannot be both untagged and internally tagged2- --> $DIR/untagged-and-internal.rs:4:102+ --> $DIR/untagged-and-internal.rs:5:93 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | #[serde(untagged)]5+ | ^^^^^^^^6 7-error: aborting due to previous error7+error: enum cannot be both untagged and internally tagged8+ --> $DIR/untagged-and-internal.rs:6:99+ |10+6 | #[serde(tag = "type")]11+ | ^^^12 13+error: aborting due to 2 previous errors14+modified:test_suite/tests/ui/enum-representation/untagged-struct.stderr+3 added-3 removed
1 error: #[serde(untagged)] can only be used on enums2- --> $DIR/untagged-struct.rs:4:102+ --> $DIR/untagged-struct.rs:6:13 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | struct S;5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/identifier/both.stderr+11 added-5 removed
1-error: `field_identifier` and `variant_identifier` cannot both be set2- --> $DIR/both.rs:4:101+error: #[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set2+ --> $DIR/both.rs:5:93 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+5 | #[serde(field_identifier, variant_identifier)]5+ | ^^^^^^^^^^^^^^^^6 7-error: aborting due to previous error7+error: #[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set8+ --> $DIR/both.rs:5:279+ |10+5 | #[serde(field_identifier, variant_identifier)]11+ | ^^^^^^^^^^^^^^^^^^12 13+error: aborting due to 2 previous errors14+modified:test_suite/tests/ui/identifier/field_struct.stderr+4 added-4 removed
1-error: `field_identifier` can only be used on an enum2- --> $DIR/field_struct.rs:4:101+error: #[serde(field_identifier)] can only be used on an enum2+ --> $DIR/field_struct.rs:6:13 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | struct S;5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/identifier/field_tuple.stderr+4 added-4 removed
1-error: field_identifier may only contain unit variants2- --> $DIR/field_tuple.rs:4:101+error: #[serde(field_identifier)] may only contain unit variants2+ --> $DIR/field_tuple.rs:8:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+8 | B(u8, u8),5+ | ^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/identifier/newtype_not_last.stderr+3 added-3 removed
1 error: `Other` must be the last variant2- --> $DIR/newtype_not_last.rs:4:102+ --> $DIR/newtype_not_last.rs:8:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+8 | Other(String),5+ | ^^^^^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/identifier/not_unit.stderr+4 added-3 removed
1 error: #[serde(other)] must be on a unit variant2- --> $DIR/not_unit.rs:4:102+ --> $DIR/not_unit.rs:8:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+8 | / #[serde(other)]5+9 | | Other(u8, u8),6+ | |_________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/identifier/other_not_last.stderr+5 added-4 removed
1-error: #[serde(other)] must be the last variant2- --> $DIR/other_not_last.rs:4:101+error: #[serde(other)] must be on the last variant2+ --> $DIR/other_not_last.rs:8:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+8 | / #[serde(other)]5+9 | | Other,6+ | |_________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/identifier/serialize.stderr+7 added-3 removed
1 error: field identifiers cannot be serialized2- --> $DIR/serialize.rs:4:102+ --> $DIR/serialize.rs:5:13 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | / #[serde(field_identifier)]5+6 | | enum F {6+7 | | A,7+8 | | B,8+9 | | }9+ | |_^10 11 error: aborting due to previous error12 modified:test_suite/tests/ui/identifier/variant_struct.stderr+4 added-4 removed
1-error: `variant_identifier` can only be used on an enum2- --> $DIR/variant_struct.rs:4:101+error: #[serde(variant_identifier)] can only be used on an enum2+ --> $DIR/variant_struct.rs:6:13 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | struct S;5+ | ^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/identifier/variant_tuple.stderr+4 added-4 removed
1-error: variant_identifier may only contain unit variants2- --> $DIR/variant_tuple.rs:4:101+error: #[serde(variant_identifier)] may only contain unit variants2+ --> $DIR/variant_tuple.rs:8:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+8 | B(u8, u8),5+ | ^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/precondition/deserialize_de_lifetime.stderr+3 added-3 removed
1 error: cannot deserialize when there is a lifetime parameter called 'de2- --> $DIR/deserialize_de_lifetime.rs:4:102+ --> $DIR/deserialize_de_lifetime.rs:5:103 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+5 | struct S<'de> {5+ | ^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/precondition/deserialize_dst.stderr+6 added-3 removed
1 error: cannot deserialize a dynamically sized struct2- --> $DIR/deserialize_dst.rs:4:102+ --> $DIR/deserialize_dst.rs:5:13 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+5 | / struct S {5+6 | | string: String,6+7 | | slice: [u8],7+8 | | }8+ | |_^9 10 error: aborting due to previous error11 modified:test_suite/tests/ui/remote/bad_getter.stderr+3 added-3 removed
1 error: failed to parse path: "~~~"2- --> $DIR/bad_getter.rs:10:102+ --> $DIR/bad_getter.rs:13:223 |4-10 | #[derive(Serialize)]5- | ^^^^^^^^^4+13 | #[serde(getter = "~~~")]5+ | ^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/remote/bad_remote.stderr+3 added-3 removed
1 error: failed to parse path: "~~~"2- --> $DIR/bad_remote.rs:10:102+ --> $DIR/bad_remote.rs:11:183 |4-10 | #[derive(Serialize)]5- | ^^^^^^^^^4+11 | #[serde(remote = "~~~")]5+ | ^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/remote/enum_getter.stderr+9 added-3 removed
1 error: #[serde(getter = "...")] is not allowed in an enum2- --> $DIR/enum_getter.rs:10:102+ --> $DIR/enum_getter.rs:11:13 |4-10 | #[derive(Serialize)]5- | ^^^^^^^^^4+11 | / #[serde(remote = "remote::E")]5+12 | | pub enum E {6+13 | | A {7+14 | | #[serde(getter = "get_a")]8+15 | | a: u8,9+16 | | },10+17 | | }11+ | |_^12 13 error: aborting due to previous error14 modified:test_suite/tests/ui/remote/nonremote_getter.stderr+6 added-3 removed
1 error: #[serde(getter = "...")] can only be used in structs that have #[serde(remote = "...")]2- --> $DIR/nonremote_getter.rs:4:102+ --> $DIR/nonremote_getter.rs:5:13 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | / struct S {5+6 | | #[serde(getter = "S::get")]6+7 | | a: u8,7+8 | | }8+ | |_^9 10 error: aborting due to previous error11 modified:test_suite/tests/ui/transparent/at_most_one.stderr+7 added-3 removed
1 error: #[serde(transparent)] requires struct to have at most one transparent field2- --> $DIR/at_most_one.rs:4:102+ --> $DIR/at_most_one.rs:5:13 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | / #[serde(transparent)]5+6 | | struct S {6+7 | | a: u8,7+8 | | b: u8,8+9 | | }9+ | |_^10 11 error: aborting due to previous error12 modified:test_suite/tests/ui/transparent/de_at_least_one.stderr+10 added-4 removed
1 error: #[serde(transparent)] requires at least one field that is neither skipped nor has a default2- --> $DIR/de_at_least_one.rs:4:103- |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^2+ --> $DIR/de_at_least_one.rs:5:13+ |4+5 | / #[serde(transparent)]5+6 | | struct S {6+7 | | #[serde(skip)]7+8 | | a: u8,8+9 | | #[serde(default)]9+10 | | b: u8,10+11 | | }11+ | |_^12 13 error: aborting due to previous error14 modified:test_suite/tests/ui/transparent/ser_at_least_one.stderr+7 added-3 removed
1 error: #[serde(transparent)] requires at least one field that is not skipped2- --> $DIR/ser_at_least_one.rs:4:102+ --> $DIR/ser_at_least_one.rs:5:13 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | / #[serde(transparent)]5+6 | | struct S {6+7 | | #[serde(skip)]7+8 | | a: u8,8+9 | | }9+ | |_^10 11 error: aborting due to previous error12 modified:test_suite/tests/ui/type-attribute/from.stderr+3 added-3 removed
1 error: failed to parse type: from = "Option<T"2- --> $DIR/from.rs:4:102+ --> $DIR/from.rs:5:163 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+5 | #[serde(from = "Option<T")]5+ | ^^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/type-attribute/into.stderr+3 added-3 removed
1 error: failed to parse type: into = "Option<T"2- --> $DIR/into.rs:4:102+ --> $DIR/into.rs:5:163 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | #[serde(into = "Option<T")]5+ | ^^^^^^^^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/unknown-attribute/container.stderr+3 added-3 removed
1 error: unknown serde container attribute `abc`2- --> $DIR/container.rs:4:102+ --> $DIR/container.rs:5:93 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+5 | #[serde(abc = "xyz")]5+ | ^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/unknown-attribute/field.stderr+3 added-3 removed
1 error: unknown serde field attribute `abc`2- --> $DIR/field.rs:4:102+ --> $DIR/field.rs:6:133 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | #[serde(abc = "xyz")]5+ | ^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/unknown-attribute/variant.stderr+3 added-3 removed
1 error: unknown serde variant attribute `abc`2- --> $DIR/variant.rs:4:102+ --> $DIR/variant.rs:6:133 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | #[serde(abc = "xyz")]5+ | ^^^6 7 error: aborting due to previous error8 modified:test_suite/tests/ui/with-variant/skip_de_newtype_field.stderr+5 added-4 removed
1-error: variant `Newtype` cannot have both #[serde(deserialize_with)] and a field 0 marked with #[serde(skip_deserializing)]2- --> $DIR/skip_de_newtype_field.rs:4:101+error: variant `Newtype` cannot have both #[serde(deserialize_with)] and a field #0 marked with #[serde(skip_deserializing)]2+ --> $DIR/skip_de_newtype_field.rs:6:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | / #[serde(deserialize_with = "deserialize_some_newtype_variant")]5+7 | | Newtype(#[serde(skip_deserializing)] String),6+ | |________________________________________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/with-variant/skip_de_struct_field.stderr+9 added-4 removed
1 error: variant `Struct` cannot have both #[serde(deserialize_with)] and a field `f1` marked with #[serde(skip_deserializing)]2- --> $DIR/skip_de_struct_field.rs:4:103- |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^2+ --> $DIR/skip_de_struct_field.rs:6:53+ |4+6 | / #[serde(deserialize_with = "deserialize_some_other_variant")]5+7 | | Struct {6+8 | | #[serde(skip_deserializing)]7+9 | | f1: String,8+10 | | f2: u8,9+11 | | },10+ | |_____^11 12 error: aborting due to previous error13 modified:test_suite/tests/ui/with-variant/skip_de_tuple_field.stderr+5 added-4 removed
1-error: variant `Tuple` cannot have both #[serde(deserialize_with)] and a field 0 marked with #[serde(skip_deserializing)]2- --> $DIR/skip_de_tuple_field.rs:4:101+error: variant `Tuple` cannot have both #[serde(deserialize_with)] and a field #0 marked with #[serde(skip_deserializing)]2+ --> $DIR/skip_de_tuple_field.rs:6:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | / #[serde(deserialize_with = "deserialize_some_other_variant")]5+7 | | Tuple(#[serde(skip_deserializing)] String, u8),6+ | |__________________________________________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/with-variant/skip_de_whole_variant.stderr+5 added-3 removed
1 error: variant `Unit` cannot have both #[serde(deserialize_with)] and #[serde(skip_deserializing)]2- --> $DIR/skip_de_whole_variant.rs:4:102+ --> $DIR/skip_de_whole_variant.rs:6:53 |4-4 | #[derive(Deserialize)]5- | ^^^^^^^^^^^4+6 | / #[serde(deserialize_with = "deserialize_some_unit_variant")]5+7 | | #[serde(skip_deserializing)]6+8 | | Unit,7+ | |________^8 9 error: aborting due to previous error10 modified:test_suite/tests/ui/with-variant/skip_ser_newtype_field_if.stderr+5 added-4 removed
1-error: variant `Newtype` cannot have both #[serde(serialize_with)] and a field 0 marked with #[serde(skip_serializing_if)]2- --> $DIR/skip_ser_newtype_field_if.rs:4:101+error: variant `Newtype` cannot have both #[serde(serialize_with)] and a field #0 marked with #[serde(skip_serializing_if)]2+ --> $DIR/skip_ser_newtype_field_if.rs:6:53 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | / #[serde(serialize_with = "serialize_some_newtype_variant")]5+7 | | Newtype(#[serde(skip_serializing_if = "always")] String),6+ | |____________________________________________________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/with-variant/skip_ser_newtype_field.stderr+5 added-4 removed
1-error: variant `Newtype` cannot have both #[serde(serialize_with)] and a field 0 marked with #[serde(skip_serializing)]2- --> $DIR/skip_ser_newtype_field.rs:4:101+error: variant `Newtype` cannot have both #[serde(serialize_with)] and a field #0 marked with #[serde(skip_serializing)]2+ --> $DIR/skip_ser_newtype_field.rs:6:53 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | / #[serde(serialize_with = "serialize_some_newtype_variant")]5+7 | | Newtype(#[serde(skip_serializing)] String),6+ | |______________________________________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/with-variant/skip_ser_struct_field_if.stderr+9 added-4 removed
1 error: variant `Struct` cannot have both #[serde(serialize_with)] and a field `f1` marked with #[serde(skip_serializing_if)]2- --> $DIR/skip_ser_struct_field_if.rs:4:103- |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^2+ --> $DIR/skip_ser_struct_field_if.rs:6:53+ |4+6 | / #[serde(serialize_with = "serialize_some_newtype_variant")]5+7 | | Struct {6+8 | | #[serde(skip_serializing_if = "always")]7+9 | | f1: String,8+10 | | f2: u8,9+11 | | },10+ | |_____^11 12 error: aborting due to previous error13 modified:test_suite/tests/ui/with-variant/skip_ser_struct_field.stderr+9 added-4 removed
1 error: variant `Struct` cannot have both #[serde(serialize_with)] and a field `f1` marked with #[serde(skip_serializing)]2- --> $DIR/skip_ser_struct_field.rs:4:103- |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^2+ --> $DIR/skip_ser_struct_field.rs:6:53+ |4+6 | / #[serde(serialize_with = "serialize_some_other_variant")]5+7 | | Struct {6+8 | | #[serde(skip_serializing)]7+9 | | f1: String,8+10 | | f2: u8,9+11 | | },10+ | |_____^11 12 error: aborting due to previous error13 modified:test_suite/tests/ui/with-variant/skip_ser_tuple_field_if.stderr+5 added-4 removed
1-error: variant `Tuple` cannot have both #[serde(serialize_with)] and a field 0 marked with #[serde(skip_serializing_if)]2- --> $DIR/skip_ser_tuple_field_if.rs:4:101+error: variant `Tuple` cannot have both #[serde(serialize_with)] and a field #0 marked with #[serde(skip_serializing_if)]2+ --> $DIR/skip_ser_tuple_field_if.rs:6:53 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | / #[serde(serialize_with = "serialize_some_other_variant")]5+7 | | Tuple(#[serde(skip_serializing_if = "always")] String, u8),6+ | |______________________________________________________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/with-variant/skip_ser_tuple_field.stderr+5 added-4 removed
1-error: variant `Tuple` cannot have both #[serde(serialize_with)] and a field 0 marked with #[serde(skip_serializing)]2- --> $DIR/skip_ser_tuple_field.rs:4:101+error: variant `Tuple` cannot have both #[serde(serialize_with)] and a field #0 marked with #[serde(skip_serializing)]2+ --> $DIR/skip_ser_tuple_field.rs:6:53 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | / #[serde(serialize_with = "serialize_some_other_variant")]5+7 | | Tuple(#[serde(skip_serializing)] String, u8),6+ | |________________________________________________^7 8 error: aborting due to previous error9 modified:test_suite/tests/ui/with-variant/skip_ser_whole_variant.stderr+5 added-3 removed
1 error: variant `Unit` cannot have both #[serde(serialize_with)] and #[serde(skip_serializing)]2- --> $DIR/skip_ser_whole_variant.rs:4:102+ --> $DIR/skip_ser_whole_variant.rs:6:53 |4-4 | #[derive(Serialize)]5- | ^^^^^^^^^4+6 | / #[serde(serialize_with = "serialize_some_unit_variant")]5+7 | | #[serde(skip_serializing)]6+8 | | Unit,7+ | |________^8 9 error: aborting due to previous error10