Skip to content
Myr

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

69 files+714 added−429 removed

modified:serde_derive/src/internals/ast.rs+9 added-3 removed
15 unmodified lines
16 /// 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}2324/// The fields of a struct or enum.25///
8 unmodified lines
34 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}4041/// A field of a struct.42pub struct Field<'a> {
16 unmodified lines
59}6061impl<'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);6566 let mut data = match item.data {67 syn::Data::Enum(ref data) => {
3 unmodified lines
71 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 };7980 let mut has_flatten = false;
27 unmodified lines
108 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}118119impl<'a> Data<'a> {
26 unmodified lines
146 ident: variant.ident.clone(),147 attrs: attrs,148 style: style,149 fields: fields,150 original: variant,151 }152 })153 .collect()154}
44 unmodified lines
modified:serde_derive/src/internals/attr.rs+290 added-152 removed
1use internals::Ctxt;2use proc_macro2::{Group, Span, TokenStream, TokenTree};3use quote::ToTokens;4use std::collections::BTreeSet;5use std::str::FromStr;6use syn;7use syn::parse::{self, Parse, ParseStream};
11 unmodified lines
19// rather than just the first.2021pub use internals::case::RenameRule;2222#[derive(Copy, Clone)]23#[derive(Clone)]24struct Attr<'c, T> {25 cx: &'c Ctxt,26 name: &'static str,27 tokens: TokenStream,28 value: Option<T>,29}3031impl<'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 }4038 fn set(&mut self, value: T) {41 fn set<A: ToTokens>(&mut self, obj: A, value: T) {42 let tokens = obj.into_token_stream();4344 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 }5447 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 }6061 fn set_if_none(&mut self, value: T) {
4 unmodified lines
6667 fn get(self) -> Option<T> {68 self.value69 }7071 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}7879struct BoolAttr<'c>(Attr<'c, ()>);80
1 unmodified line
82 fn none(cx: &'c Ctxt, name: &'static str) -> Self {83 BoolAttr(Attr::none(cx, name))84 }8571 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 }8990 fn get(&self) -> bool {91 self.0.value.is_some()
121 unmodified lines
213 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 }221222 // 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 }229230 // 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 }243244 // Parse `#[serde(transparent)]`245 Meta(Word(ref word)) if word == "transparent" => {231 transparent.set_true();246 transparent.set_true(word);247 }248249 // 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 }253254 // 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 },277278 // 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 line
307 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 }315316 // 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 }323324 // 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 },342343 // 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 line
367 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 }388389 // 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 }395396 // 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 }402403 // 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 }413414 // 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 }418419 // 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 }423424 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 }430370 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 lines
445 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 lines
514515fn 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 lines
542 }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}596597fn 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}648649/// Represents variant attribute information
31 unmodified lines
681 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 }689690 // 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 }697698 // 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 lines
710 }711712 // 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 }717718 // 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 }722723 // 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 }727728 // Parse `#[serde(other)]`729 Meta(Word(ref word)) if word == "other" => {613 other.set_true();730 other.set_true(word);731 }732733 // 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 }742743 // 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 }750751 // Parse `#[serde(with = "...")]`
3 unmodified lines
755 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 }768769 // 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 }775776 // 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 }782783 // 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 },795796 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 }802683 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 lines
959 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 }967968 // 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 }975976 // Parse `#[serde(default)]`977 Meta(Word(ref word)) if word == "default" => {855 default.set(Default::Default);978 default.set(word, Default::Default);979 }980981 // 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 }987988 // 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 }992993 // 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 }997998 // 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 }10031004 // 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 }10101011 // 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 }10171018 // 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 }10241025 // Parse `#[serde(with = "...")]`
3 unmodified lines
1029 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 }10421043 // 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 }10521053 // 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 }10601061 // 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 }10671068 // 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 }10841085 // 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 }10911092 // Parse `#[serde(flatten)]`1093 Meta(Word(ref word)) if word == "flatten" => {971 flatten.set_true();1094 flatten.set_true(word);1095 }10961097 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 }1103981 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 lines
1287 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 }12941295 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 }13001301 _ => {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 lines
1348) -> 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 lines
13591360fn 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}13681369fn parse_lit_into_expr_path(1370 cx: &Ctxt,
1 unmodified line
1372 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}13811382fn parse_lit_into_where(1383 cx: &Ctxt,
9 unmodified lines
1393 let where_string = syn::LitStr::new(&format!("where {}", string.value()), string.span());13941395 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}13991400fn 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));14021403 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 lines
1417 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 }14241425 struct BorrowedLifetimes(Punctuated<syn::Lifetime, Token![+]>);
7 unmodified lines
1433 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 }14451311 cx.error(format!(1446 cx.error_spanned_by(lit, format!(1447 "failed to parse borrowed lifetimes: {:?}",1448 string.value()1449 ));1450 Err(())
144 unmodified lines
1595// attribute on the field so there must be at least one borrowable lifetime.1596fn 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 lines
modified:serde_derive/src/internals/check.rs+89 added-37 removed
19 unmodified lines
20fn 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 lines
62 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 lines
119 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 }128129 // 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 }136137 // 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 }146147 // 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 }154155 // Any sort of variant is allowed if this is not an identifier.156 (_, Identifier::No, false, _) => {}
3 unmodified lines
160161 // 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 }170171 (_, 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 }177178 (_, 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 lines
197198 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 lines
208 for field in &variant.fields {209 let member = member_message(&field.member);210211 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 }218219 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 lines
227 }228229 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 lines
238 for field in &variant.fields {239 if field.attrs.skip_deserializing() {240 let member = member_message(&field.member);241208 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 lines
264 EnumTag::Internal { ref tag } => tag.as_str(),265 EnumTag::External | EnumTag::Adjacent { .. } | EnumTag::None => return,266 };267234 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 );272273 for variant in variants {274 match variant.style {275 Style::Struct => {
26 unmodified lines
302 EnumTag::Internal { .. } | EnumTag::External | EnumTag::None => return,303 };304305 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}312313/// Enums and unit structs cannot be transparent.
2 unmodified lines
316 return;317 }318319 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 }325326 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 }332333 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 lines
352353 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 lines
366 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}384385fn 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}391392fn allow_transparent(field: &Field, derive: Derive) -> bool {
13 unmodified lines
modified:serde_derive/src/internals/ctxt.rs+12 added-15 removed
1use quote::ToTokens;2use std::cell::RefCell;3use std::fmt::Display;4use std::thread;5use syn;67/// 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 lines
12#[derive(Default)]13pub 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}1819impl Ctxt {20 /// Create a new context object.
4 unmodified lines
25 errors: RefCell::new(Some(Vec::new())),26 }27 }2827 /// 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 }4041 /// 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 lines
modified:serde_derive/src/de.rs+10 added-4 removed
11 unmodified lines
12use try;1314use std::collections::BTreeSet;1516pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<TokenStream, String> {16pub 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());2425 let ident = &cont.ident;
62 unmodified lines
88fn 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 line
98fn 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 &param.lifetime,104 "cannot deserialize when there is a lifetime parameter called 'de",105 );106 return;107 }108 }109 }
2875 unmodified lines
modified:serde_derive/src/lib.rs+5 added-7 removed
60 unmodified lines
61mod internals;6263use proc_macro::TokenStream;64use syn::DeriveInput;65use syn::spanned::Spanned;6566#[macro_use]67mod bound;68#[macro_use]
7 unmodified lines
76#[proc_macro_derive(Serialize, attributes(serde))]77pub 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}8384#[proc_macro_derive(Deserialize, attributes(serde))]85pub 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}9193fn compile_error(span: proc_macro2::Span, message: String) -> proc_macro2::TokenStream {94 quote_spanned! {span=>95 compile_error!(#message);96 }92fn 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 lines
8use internals::{attr, Ctxt, Derive};9use pretend;10use try;1112pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream, String> {12pub 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());2021 let ident = &cont.ident;
52 unmodified lines
74fn 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 lines
modified:serde_derive/Cargo.toml+1 added-1 removed
modified:test_suite/tests/ui/borrow/bad_lifetimes.stderr+3 added-3 removed
1error: failed to parse borrowed lifetimes: "zzz"2 --> $DIR/bad_lifetimes.rs:4:102 --> $DIR/bad_lifetimes.rs:6:223 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | #[serde(borrow = "zzz")]5 | ^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/borrow/duplicate_lifetime.stderr+3 added-3 removed
1error: duplicate borrowed lifetime `'a`2 --> $DIR/duplicate_lifetime.rs:4:102 --> $DIR/duplicate_lifetime.rs:6:223 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | #[serde(borrow = "'a + 'a")]5 | ^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/borrow/duplicate_variant.stderr+3 added-3 removed
1error: duplicate serde attribute `borrow`2 --> $DIR/duplicate_variant.rs:7:102 --> $DIR/duplicate_variant.rs:9:133 |47 | #[derive(Deserialize)]5 | ^^^^^^^^^^^49 | #[serde(borrow)]5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/borrow/empty_lifetimes.stderr+3 added-3 removed
1error: at least one lifetime must be borrowed2 --> $DIR/empty_lifetimes.rs:4:102 --> $DIR/empty_lifetimes.rs:6:223 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | #[serde(borrow = "")]5 | ^^67error: aborting due to previous error8
modified:test_suite/tests/ui/borrow/no_lifetimes.stderr+4 added-3 removed
1error: field `s` has no lifetimes to borrow2 --> $DIR/no_lifetimes.rs:4:102 --> $DIR/no_lifetimes.rs:6:53 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | / #[serde(borrow)]57 | | s: String,6 | |_____________^78error: aborting due to previous error9
modified:test_suite/tests/ui/borrow/struct_variant.stderr+5 added-4 removed
1error: #[serde(borrow)] may only be used on newtype variants2 --> $DIR/struct_variant.rs:7:103 |47 | #[derive(Deserialize)]5 | ^^^^^^^^^^^2 --> $DIR/struct_variant.rs:9:53 |49 | / #[serde(borrow)]510 | | S { s: Str<'a> },6 | |____________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/borrow/wrong_lifetime.stderr+4 added-3 removed
1error: field `s` does not have lifetime 'b2 --> $DIR/wrong_lifetime.rs:4:102 --> $DIR/wrong_lifetime.rs:6:53 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | / #[serde(borrow = "'b")]57 | | s: &'a str,6 | |______________^78error: aborting due to previous error9
modified:test_suite/tests/ui/conflict/adjacent-tag.stderr+7 added-3 removed
1error: enum tags `conflict` for type and content conflict with each other2 --> $DIR/adjacent-tag.rs:4:102 --> $DIR/adjacent-tag.rs:5:13 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | / #[serde(tag = "conflict", content = "conflict")]56 | | enum E {67 | | A,78 | | B,89 | | }9 | |_^1011error: aborting due to previous error12
modified:test_suite/tests/ui/conflict/flatten-newtype-struct.stderr+3 added-3 removed
1error: #[serde(flatten)] cannot be used on newtype structs2 --> $DIR/flatten-newtype-struct.rs:4:102 --> $DIR/flatten-newtype-struct.rs:5:123 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | struct Foo(#[serde(flatten)] HashMap<String, String>);5 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/conflict/flatten-skip-deserializing.stderr+5 added-4 removed
1error: #[serde(flatten] can not be combined with #[serde(skip_deserializing)]2 --> $DIR/flatten-skip-deserializing.rs:4:101error: #[serde(flatten)] can not be combined with #[serde(skip_deserializing)]2 --> $DIR/flatten-skip-deserializing.rs:6:53 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | / #[serde(flatten, skip_deserializing)]57 | | other: Other,6 | |________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/conflict/flatten-skip-serializing-if.stderr+5 added-4 removed
1error: #[serde(flatten] can not be combined with #[serde(skip_serializing_if = "...")]2 --> $DIR/flatten-skip-serializing-if.rs:4:101error: #[serde(flatten)] can not be combined with #[serde(skip_serializing_if = "...")]2 --> $DIR/flatten-skip-serializing-if.rs:6:53 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | / #[serde(flatten, skip_serializing_if = "Option::is_none")]57 | | other: Option<Other>,6 | |________________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/conflict/flatten-skip-serializing.stderr+5 added-4 removed
1error: #[serde(flatten] can not be combined with #[serde(skip_serializing)]2 --> $DIR/flatten-skip-serializing.rs:4:101error: #[serde(flatten)] can not be combined with #[serde(skip_serializing)]2 --> $DIR/flatten-skip-serializing.rs:6:53 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | / #[serde(flatten, skip_serializing)]57 | | other: Other,6 | |________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/conflict/flatten-tuple-struct.stderr+3 added-3 removed
1error: #[serde(flatten)] cannot be used on tuple structs2 --> $DIR/flatten-tuple-struct.rs:4:102 --> $DIR/flatten-tuple-struct.rs:5:173 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | struct Foo(u32, #[serde(flatten)] HashMap<String, String>);5 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/conflict/internal-tag.stderr+10 added-4 removed
1error: variant field name `conflict` conflicts with internal tag2 --> $DIR/internal-tag.rs:4:103 |44 | #[derive(Serialize)]5 | ^^^^^^^^^2 --> $DIR/internal-tag.rs:5:13 |45 | / #[serde(tag = "conflict")]56 | | enum E {67 | | A {78 | | #[serde(rename = "conflict")]89 | | x: (),910 | | },1011 | | }11 | |_^1213error: aborting due to previous error14
modified:test_suite/tests/ui/default-attribute/enum.stderr+3 added-3 removed
1error: #[serde(default)] can only be used on structs with named fields2 --> $DIR/enum.rs:4:102 --> $DIR/enum.rs:6:13 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | enum E {5 | ^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/default-attribute/nameless_struct_fields.stderr+3 added-3 removed
1error: #[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 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | struct T(u8, u8);5 | ^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/duplicate-attribute/rename-and-ser.stderr+3 added-3 removed
1error: unknown serde field attribute `serialize`2 --> $DIR/rename-and-ser.rs:4:102 --> $DIR/rename-and-ser.rs:6:273 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | #[serde(rename = "x", serialize = "y")]5 | ^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/duplicate-attribute/rename-rename-de.stderr+3 added-3 removed
1error: duplicate serde attribute `rename`2 --> $DIR/rename-rename-de.rs:4:102 --> $DIR/rename-rename-de.rs:7:133 |44 | #[derive(Serialize)]5 | ^^^^^^^^^47 | #[serde(rename(deserialize = "y"))]5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/duplicate-attribute/rename-ser-rename-ser.stderr+3 added-3 removed
1error: duplicate serde attribute `rename`2 --> $DIR/rename-ser-rename-ser.rs:4:102 --> $DIR/rename-ser-rename-ser.rs:6:383 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | #[serde(rename(serialize = "x"), rename(serialize = "y"))]5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/duplicate-attribute/rename-ser-rename.stderr+3 added-3 removed
1error: duplicate serde attribute `rename`2 --> $DIR/rename-ser-rename.rs:4:102 --> $DIR/rename-ser-rename.rs:7:133 |44 | #[derive(Serialize)]5 | ^^^^^^^^^47 | #[serde(rename = "y")]5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/duplicate-attribute/rename-ser-ser.stderr+3 added-3 removed
1error: duplicate serde attribute `rename`2 --> $DIR/rename-ser-ser.rs:4:102 --> $DIR/rename-ser-ser.rs:6:373 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | #[serde(rename(serialize = "x", serialize = "y"))]5 | ^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/duplicate-attribute/two-rename-ser.stderr+3 added-3 removed
1error: duplicate serde attribute `rename`2 --> $DIR/two-rename-ser.rs:4:102 --> $DIR/two-rename-ser.rs:7:133 |44 | #[derive(Serialize)]5 | ^^^^^^^^^47 | #[serde(rename(serialize = "y"))]5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/duplicate-attribute/with-and-serialize-with.stderr+3 added-3 removed
1error: duplicate serde attribute `serialize_with`2 --> $DIR/with-and-serialize-with.rs:4:102 --> $DIR/with-and-serialize-with.rs:6:253 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | #[serde(with = "w", serialize_with = "s")]5 | ^^^^^^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/enum-representation/internal-tuple-variant.stderr+3 added-3 removed
1error: #[serde(tag = "...")] cannot be used with tuple variants2 --> $DIR/internal-tuple-variant.rs:4:102 --> $DIR/internal-tuple-variant.rs:7:53 |44 | #[derive(Serialize)]5 | ^^^^^^^^^47 | Tuple(u8, u8),5 | ^^^^^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/enum-representation/internally-tagged-struct.stderr+3 added-3 removed
1error: #[serde(tag = "...")] can only be used on enums2 --> $DIR/internally-tagged-struct.rs:4:102 --> $DIR/internally-tagged-struct.rs:6:13 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | struct S;5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/enum-representation/untagged-and-internal.stderr+10 added-4 removed
1error: enum cannot be both untagged and internally tagged2 --> $DIR/untagged-and-internal.rs:4:102 --> $DIR/untagged-and-internal.rs:5:93 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | #[serde(untagged)]5 | ^^^^^^^^67error: aborting due to previous error7error: enum cannot be both untagged and internally tagged8 --> $DIR/untagged-and-internal.rs:6:99 |106 | #[serde(tag = "type")]11 | ^^^1213error: aborting due to 2 previous errors14
modified:test_suite/tests/ui/enum-representation/untagged-struct.stderr+3 added-3 removed
1error: #[serde(untagged)] can only be used on enums2 --> $DIR/untagged-struct.rs:4:102 --> $DIR/untagged-struct.rs:6:13 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | struct S;5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/identifier/both.stderr+11 added-5 removed
1error: `field_identifier` and `variant_identifier` cannot both be set2 --> $DIR/both.rs:4:101error: #[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set2 --> $DIR/both.rs:5:93 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^45 | #[serde(field_identifier, variant_identifier)]5 | ^^^^^^^^^^^^^^^^67error: aborting due to previous error7error: #[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set8 --> $DIR/both.rs:5:279 |105 | #[serde(field_identifier, variant_identifier)]11 | ^^^^^^^^^^^^^^^^^^1213error: aborting due to 2 previous errors14
modified:test_suite/tests/ui/identifier/field_struct.stderr+4 added-4 removed
1error: `field_identifier` can only be used on an enum2 --> $DIR/field_struct.rs:4:101error: #[serde(field_identifier)] can only be used on an enum2 --> $DIR/field_struct.rs:6:13 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | struct S;5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/identifier/field_tuple.stderr+4 added-4 removed
1error: field_identifier may only contain unit variants2 --> $DIR/field_tuple.rs:4:101error: #[serde(field_identifier)] may only contain unit variants2 --> $DIR/field_tuple.rs:8:53 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^48 | B(u8, u8),5 | ^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/identifier/newtype_not_last.stderr+3 added-3 removed
1error: `Other` must be the last variant2 --> $DIR/newtype_not_last.rs:4:102 --> $DIR/newtype_not_last.rs:8:53 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^48 | Other(String),5 | ^^^^^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/identifier/not_unit.stderr+4 added-3 removed
1error: #[serde(other)] must be on a unit variant2 --> $DIR/not_unit.rs:4:102 --> $DIR/not_unit.rs:8:53 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^48 | / #[serde(other)]59 | | Other(u8, u8),6 | |_________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/identifier/other_not_last.stderr+5 added-4 removed
1error: #[serde(other)] must be the last variant2 --> $DIR/other_not_last.rs:4:101error: #[serde(other)] must be on the last variant2 --> $DIR/other_not_last.rs:8:53 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^48 | / #[serde(other)]59 | | Other,6 | |_________^78error: aborting due to previous error9
modified:test_suite/tests/ui/identifier/serialize.stderr+7 added-3 removed
1error: field identifiers cannot be serialized2 --> $DIR/serialize.rs:4:102 --> $DIR/serialize.rs:5:13 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | / #[serde(field_identifier)]56 | | enum F {67 | | A,78 | | B,89 | | }9 | |_^1011error: aborting due to previous error12
modified:test_suite/tests/ui/identifier/variant_struct.stderr+4 added-4 removed
1error: `variant_identifier` can only be used on an enum2 --> $DIR/variant_struct.rs:4:101error: #[serde(variant_identifier)] can only be used on an enum2 --> $DIR/variant_struct.rs:6:13 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | struct S;5 | ^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/identifier/variant_tuple.stderr+4 added-4 removed
1error: variant_identifier may only contain unit variants2 --> $DIR/variant_tuple.rs:4:101error: #[serde(variant_identifier)] may only contain unit variants2 --> $DIR/variant_tuple.rs:8:53 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^48 | B(u8, u8),5 | ^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/precondition/deserialize_de_lifetime.stderr+3 added-3 removed
1error: cannot deserialize when there is a lifetime parameter called 'de2 --> $DIR/deserialize_de_lifetime.rs:4:102 --> $DIR/deserialize_de_lifetime.rs:5:103 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^45 | struct S<'de> {5 | ^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/precondition/deserialize_dst.stderr+6 added-3 removed
1error: cannot deserialize a dynamically sized struct2 --> $DIR/deserialize_dst.rs:4:102 --> $DIR/deserialize_dst.rs:5:13 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^45 | / struct S {56 | | string: String,67 | | slice: [u8],78 | | }8 | |_^910error: aborting due to previous error11
modified:test_suite/tests/ui/remote/bad_getter.stderr+3 added-3 removed
1error: failed to parse path: "~~~"2 --> $DIR/bad_getter.rs:10:102 --> $DIR/bad_getter.rs:13:223 |410 | #[derive(Serialize)]5 | ^^^^^^^^^413 | #[serde(getter = "~~~")]5 | ^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/remote/bad_remote.stderr+3 added-3 removed
1error: failed to parse path: "~~~"2 --> $DIR/bad_remote.rs:10:102 --> $DIR/bad_remote.rs:11:183 |410 | #[derive(Serialize)]5 | ^^^^^^^^^411 | #[serde(remote = "~~~")]5 | ^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/remote/enum_getter.stderr+9 added-3 removed
1error: #[serde(getter = "...")] is not allowed in an enum2 --> $DIR/enum_getter.rs:10:102 --> $DIR/enum_getter.rs:11:13 |410 | #[derive(Serialize)]5 | ^^^^^^^^^411 | / #[serde(remote = "remote::E")]512 | | pub enum E {613 | | A {714 | | #[serde(getter = "get_a")]815 | | a: u8,916 | | },1017 | | }11 | |_^1213error: aborting due to previous error14
modified:test_suite/tests/ui/remote/nonremote_getter.stderr+6 added-3 removed
1error: #[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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | / struct S {56 | | #[serde(getter = "S::get")]67 | | a: u8,78 | | }8 | |_^910error: aborting due to previous error11
modified:test_suite/tests/ui/transparent/at_most_one.stderr+7 added-3 removed
1error: #[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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | / #[serde(transparent)]56 | | struct S {67 | | a: u8,78 | | b: u8,89 | | }9 | |_^1011error: aborting due to previous error12
modified:test_suite/tests/ui/transparent/de_at_least_one.stderr+10 added-4 removed
1error: #[serde(transparent)] requires at least one field that is neither skipped nor has a default2 --> $DIR/de_at_least_one.rs:4:103 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^2 --> $DIR/de_at_least_one.rs:5:13 |45 | / #[serde(transparent)]56 | | struct S {67 | | #[serde(skip)]78 | | a: u8,89 | | #[serde(default)]910 | | b: u8,1011 | | }11 | |_^1213error: aborting due to previous error14
modified:test_suite/tests/ui/transparent/ser_at_least_one.stderr+7 added-3 removed
1error: #[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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | / #[serde(transparent)]56 | | struct S {67 | | #[serde(skip)]78 | | a: u8,89 | | }9 | |_^1011error: aborting due to previous error12
modified:test_suite/tests/ui/type-attribute/from.stderr+3 added-3 removed
1error: failed to parse type: from = "Option<T"2 --> $DIR/from.rs:4:102 --> $DIR/from.rs:5:163 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^45 | #[serde(from = "Option<T")]5 | ^^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/type-attribute/into.stderr+3 added-3 removed
1error: failed to parse type: into = "Option<T"2 --> $DIR/into.rs:4:102 --> $DIR/into.rs:5:163 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | #[serde(into = "Option<T")]5 | ^^^^^^^^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/unknown-attribute/container.stderr+3 added-3 removed
1error: unknown serde container attribute `abc`2 --> $DIR/container.rs:4:102 --> $DIR/container.rs:5:93 |44 | #[derive(Serialize)]5 | ^^^^^^^^^45 | #[serde(abc = "xyz")]5 | ^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/unknown-attribute/field.stderr+3 added-3 removed
1error: unknown serde field attribute `abc`2 --> $DIR/field.rs:4:102 --> $DIR/field.rs:6:133 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | #[serde(abc = "xyz")]5 | ^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/unknown-attribute/variant.stderr+3 added-3 removed
1error: unknown serde variant attribute `abc`2 --> $DIR/variant.rs:4:102 --> $DIR/variant.rs:6:133 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | #[serde(abc = "xyz")]5 | ^^^67error: aborting due to previous error8
modified:test_suite/tests/ui/with-variant/skip_de_newtype_field.stderr+5 added-4 removed
1error: 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:101error: 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 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | / #[serde(deserialize_with = "deserialize_some_newtype_variant")]57 | | Newtype(#[serde(skip_deserializing)] String),6 | |________________________________________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/with-variant/skip_de_struct_field.stderr+9 added-4 removed
1error: 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 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^2 --> $DIR/skip_de_struct_field.rs:6:53 |46 | / #[serde(deserialize_with = "deserialize_some_other_variant")]57 | | Struct {68 | | #[serde(skip_deserializing)]79 | | f1: String,810 | | f2: u8,911 | | },10 | |_____^1112error: aborting due to previous error13
modified:test_suite/tests/ui/with-variant/skip_de_tuple_field.stderr+5 added-4 removed
1error: 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:101error: 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 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | / #[serde(deserialize_with = "deserialize_some_other_variant")]57 | | Tuple(#[serde(skip_deserializing)] String, u8),6 | |__________________________________________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/with-variant/skip_de_whole_variant.stderr+5 added-3 removed
1error: 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 |44 | #[derive(Deserialize)]5 | ^^^^^^^^^^^46 | / #[serde(deserialize_with = "deserialize_some_unit_variant")]57 | | #[serde(skip_deserializing)]68 | | Unit,7 | |________^89error: aborting due to previous error10
modified:test_suite/tests/ui/with-variant/skip_ser_newtype_field_if.stderr+5 added-4 removed
1error: 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:101error: 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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | / #[serde(serialize_with = "serialize_some_newtype_variant")]57 | | Newtype(#[serde(skip_serializing_if = "always")] String),6 | |____________________________________________________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/with-variant/skip_ser_newtype_field.stderr+5 added-4 removed
1error: 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:101error: 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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | / #[serde(serialize_with = "serialize_some_newtype_variant")]57 | | Newtype(#[serde(skip_serializing)] String),6 | |______________________________________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/with-variant/skip_ser_struct_field_if.stderr+9 added-4 removed
1error: 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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^2 --> $DIR/skip_ser_struct_field_if.rs:6:53 |46 | / #[serde(serialize_with = "serialize_some_newtype_variant")]57 | | Struct {68 | | #[serde(skip_serializing_if = "always")]79 | | f1: String,810 | | f2: u8,911 | | },10 | |_____^1112error: aborting due to previous error13
modified:test_suite/tests/ui/with-variant/skip_ser_struct_field.stderr+9 added-4 removed
1error: 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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^2 --> $DIR/skip_ser_struct_field.rs:6:53 |46 | / #[serde(serialize_with = "serialize_some_other_variant")]57 | | Struct {68 | | #[serde(skip_serializing)]79 | | f1: String,810 | | f2: u8,911 | | },10 | |_____^1112error: aborting due to previous error13
modified:test_suite/tests/ui/with-variant/skip_ser_tuple_field_if.stderr+5 added-4 removed
1error: 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:101error: 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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | / #[serde(serialize_with = "serialize_some_other_variant")]57 | | Tuple(#[serde(skip_serializing_if = "always")] String, u8),6 | |______________________________________________________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/with-variant/skip_ser_tuple_field.stderr+5 added-4 removed
1error: 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:101error: 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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | / #[serde(serialize_with = "serialize_some_other_variant")]57 | | Tuple(#[serde(skip_serializing)] String, u8),6 | |________________________________________________^78error: aborting due to previous error9
modified:test_suite/tests/ui/with-variant/skip_ser_whole_variant.stderr+5 added-3 removed
1error: 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 |44 | #[derive(Serialize)]5 | ^^^^^^^^^46 | / #[serde(serialize_with = "serialize_some_unit_variant")]57 | | #[serde(skip_serializing)]68 | | Unit,7 | |________^89error: aborting due to previous error10