Skip to content
Myr

Merge pull request #2405 from dtolnay/syn

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Update to syn 2

authored by David Tolnay · committed by GitHub · parents 9d87851, 32f0d00 · Browse files · 12 files+199 added−199 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

12 files+199 added−199 removed

  • tests:+8 added−8 removed
  • other:+191 added−191 removed

12 files+199 added−199 removed

modified:.github/workflows/ci.yml+2 added-2 removed
modified:serde_derive/src/internals/attr.rs+152 added-167 removed
6 unmodified lines
7use std::iter::FromIterator;8use syn;9use syn::parse::ParseStream;10use syn::punctuated::Punctuated;11use syn::Meta::{List, NameValue, Path};12use syn::NestedMeta::{Lit, Meta};13use syn::{Ident, Lifetime};11use syn::{Expr, Ident, Lifetime, Meta, MetaList};1213// This module handles parsing of `#[serde(...)]` attributes. The entrypoints14// are `attr::Container::from_ast`, `attr::Variant::from_ast`, and15// `attr::Field::from_ast`. Each returns an instance of the corresponding
294 unmodified lines
310 .flatten()311 {312 match &meta_item {313 // Parse `#[serde(rename = "foo")]`316 Meta(NameValue(m)) if m.path == RENAME => {317 if let Ok(s) = get_lit_str(cx, RENAME, &m.lit) {314 Meta::NameValue(m) if m.path == RENAME => {315 if let Ok(s) = get_lit_str(cx, RENAME, &m.value) {316 ser_name.set(&m.path, s.value());317 de_name.set(&m.path, s.value());318 }319 }320321 // Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`324 Meta(List(m)) if m.path == RENAME => {325 if let Ok((ser, de)) = get_renames(cx, &m.nested) {326 ser_name.set_opt(&m.path, ser.map(syn::LitStr::value));327 de_name.set_opt(&m.path, de.map(syn::LitStr::value));322 Meta::List(m) if m.path == RENAME => {323 if let Ok((ser, de)) = get_renames(cx, m) {324 ser_name.set_opt(&m.path, ser.as_ref().map(syn::LitStr::value));325 de_name.set_opt(&m.path, de.as_ref().map(syn::LitStr::value));326 }327 }328329 // Parse `#[serde(rename_all = "foo")]`332 Meta(NameValue(m)) if m.path == RENAME_ALL => {333 if let Ok(s) = get_lit_str(cx, RENAME_ALL, &m.lit) {330 Meta::NameValue(m) if m.path == RENAME_ALL => {331 if let Ok(s) = get_lit_str(cx, RENAME_ALL, &m.value) {332 match RenameRule::from_str(&s.value()) {333 Ok(rename_rule) => {334 rename_all_ser_rule.set(&m.path, rename_rule);335 rename_all_de_rule.set(&m.path, rename_rule);
3 unmodified lines
339 }340 }341342 // Parse `#[serde(rename_all(serialize = "foo", deserialize = "bar"))]`345 Meta(List(m)) if m.path == RENAME_ALL => {346 if let Ok((ser, de)) = get_renames(cx, &m.nested) {343 Meta::List(m) if m.path == RENAME_ALL => {344 if let Ok((ser, de)) = get_renames(cx, m) {345 if let Some(ser) = ser {346 match RenameRule::from_str(&ser.value()) {347 Ok(rename_rule) => rename_all_ser_rule.set(&m.path, rename_rule),348 Err(err) => cx.error_spanned_by(ser, err),
8 unmodified lines
357 }358 }359360 // Parse `#[serde(transparent)]`363 Meta(Path(word)) if word == TRANSPARENT => {361 Meta::Path(word) if word == TRANSPARENT => {362 transparent.set_true(word);363 }364365 // Parse `#[serde(deny_unknown_fields)]`368 Meta(Path(word)) if word == DENY_UNKNOWN_FIELDS => {366 Meta::Path(word) if word == DENY_UNKNOWN_FIELDS => {367 deny_unknown_fields.set_true(word);368 }369370 // Parse `#[serde(default)]`373 Meta(Path(word)) if word == DEFAULT => match &item.data {371 Meta::Path(word) if word == DEFAULT => match &item.data {372 syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {373 syn::Fields::Named(_) => {374 default.set(word, Default::Default);375 }
13 unmodified lines
389 }390 },391392 // Parse `#[serde(default = "...")]`395 Meta(NameValue(m)) if m.path == DEFAULT => {396 if let Ok(path) = parse_lit_into_expr_path(cx, DEFAULT, &m.lit) {393 Meta::NameValue(m) if m.path == DEFAULT => {394 if let Ok(path) = parse_lit_into_expr_path(cx, DEFAULT, &m.value) {395 match &item.data {396 syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {397 syn::Fields::Named(_) => {398 default.set(&m.path, Default::Path(path));
15 unmodified lines
414 }415 }416417 // Parse `#[serde(bound = "T: SomeBound")]`420 Meta(NameValue(m)) if m.path == BOUND => {421 if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.lit) {418 Meta::NameValue(m) if m.path == BOUND => {419 if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.value) {420 ser_bound.set(&m.path, where_predicates.clone());421 de_bound.set(&m.path, where_predicates);422 }423 }424425 // Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`428 Meta(List(m)) if m.path == BOUND => {429 if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {426 Meta::List(m) if m.path == BOUND => {427 if let Ok((ser, de)) = get_where_predicates(cx, m) {428 ser_bound.set_opt(&m.path, ser);429 de_bound.set_opt(&m.path, de);430 }431 }432433 // Parse `#[serde(untagged)]`436 Meta(Path(word)) if word == UNTAGGED => match item.data {434 Meta::Path(word) if word == UNTAGGED => match item.data {435 syn::Data::Enum(_) => {436 untagged.set_true(word);437 }438 syn::Data::Struct(syn::DataStruct { struct_token, .. }) => {
6 unmodified lines
445 }446 },447448 // Parse `#[serde(tag = "type")]`451 Meta(NameValue(m)) if m.path == TAG => {452 if let Ok(s) = get_lit_str(cx, TAG, &m.lit) {449 Meta::NameValue(m) if m.path == TAG => {450 if let Ok(s) = get_lit_str(cx, TAG, &m.value) {451 match &item.data {452 syn::Data::Enum(_) => {453 internal_tag.set(&m.path, s.value());454 }
14 unmodified lines
469 }470 }471472 // Parse `#[serde(content = "c")]`475 Meta(NameValue(m)) if m.path == CONTENT => {476 if let Ok(s) = get_lit_str(cx, CONTENT, &m.lit) {473 Meta::NameValue(m) if m.path == CONTENT => {474 if let Ok(s) = get_lit_str(cx, CONTENT, &m.value) {475 match &item.data {476 syn::Data::Enum(_) => {477 content.set(&m.path, s.value());478 }
9 unmodified lines
488 }489 }490491 // Parse `#[serde(from = "Type")]`494 Meta(NameValue(m)) if m.path == FROM => {495 if let Ok(from_ty) = parse_lit_into_ty(cx, FROM, &m.lit) {492 Meta::NameValue(m) if m.path == FROM => {493 if let Ok(from_ty) = parse_lit_into_ty(cx, FROM, &m.value) {494 type_from.set_opt(&m.path, Some(from_ty));495 }496 }497498 // Parse `#[serde(try_from = "Type")]`501 Meta(NameValue(m)) if m.path == TRY_FROM => {502 if let Ok(try_from_ty) = parse_lit_into_ty(cx, TRY_FROM, &m.lit) {499 Meta::NameValue(m) if m.path == TRY_FROM => {500 if let Ok(try_from_ty) = parse_lit_into_ty(cx, TRY_FROM, &m.value) {501 type_try_from.set_opt(&m.path, Some(try_from_ty));502 }503 }504505 // Parse `#[serde(into = "Type")]`508 Meta(NameValue(m)) if m.path == INTO => {509 if let Ok(into_ty) = parse_lit_into_ty(cx, INTO, &m.lit) {506 Meta::NameValue(m) if m.path == INTO => {507 if let Ok(into_ty) = parse_lit_into_ty(cx, INTO, &m.value) {508 type_into.set_opt(&m.path, Some(into_ty));509 }510 }511512 // Parse `#[serde(remote = "...")]`515 Meta(NameValue(m)) if m.path == REMOTE => {516 if let Ok(path) = parse_lit_into_path(cx, REMOTE, &m.lit) {513 Meta::NameValue(m) if m.path == REMOTE => {514 if let Ok(path) = parse_lit_into_path(cx, REMOTE, &m.value) {515 if is_primitive_path(&path, "Self") {516 remote.set(&m.path, item.ident.clone().into());517 } else {518 remote.set(&m.path, path);
1 unmodified line
520 }521 }522523 // Parse `#[serde(field_identifier)]`526 Meta(Path(word)) if word == FIELD_IDENTIFIER => {524 Meta::Path(word) if word == FIELD_IDENTIFIER => {525 field_identifier.set_true(word);526 }527528 // Parse `#[serde(variant_identifier)]`531 Meta(Path(word)) if word == VARIANT_IDENTIFIER => {529 Meta::Path(word) if word == VARIANT_IDENTIFIER => {530 variant_identifier.set_true(word);531 }532533 // Parse `#[serde(crate = "foo")]`536 Meta(NameValue(m)) if m.path == CRATE => {537 if let Ok(path) = parse_lit_into_path(cx, CRATE, &m.lit) {534 Meta::NameValue(m) if m.path == CRATE => {535 if let Ok(path) = parse_lit_into_path(cx, CRATE, &m.value) {536 serde_path.set(&m.path, path);537 }538 }539540 // Parse `#[serde(expecting = "a message")]`543 Meta(NameValue(m)) if m.path == EXPECTING => {544 if let Ok(s) = get_lit_str(cx, EXPECTING, &m.lit) {541 Meta::NameValue(m) if m.path == EXPECTING => {542 if let Ok(s) = get_lit_str(cx, EXPECTING, &m.value) {543 expecting.set(&m.path, s.value());544 }545 }546549 Meta(meta_item) => {547 meta_item => {548 let path = meta_item549 .path()550 .into_token_stream()551 .to_string()552 .replace(' ', "");553 let msg = format!("unknown serde container attribute `{}`", path);554 cx.error_spanned_by(meta_item.path(), msg);555 }558559 Lit(lit) => {560 let msg = "unexpected literal in serde container attribute";561 cx.error_spanned_by(lit, msg);562 }556 }557 }558559 let mut is_packed = false;560 for attr in &item.attrs {568 if attr.path == REPR {561 if attr.path() == REPR {562 let _ = attr.parse_args_with(|input: ParseStream| {563 while let Some(token) = input.parse()? {564 if let TokenTree::Ident(ident) = token {565 is_packed |= ident == "packed";
255 unmodified lines
821 .flatten()822 {823 match &meta_item {824 // Parse `#[serde(rename = "foo")]`832 Meta(NameValue(m)) if m.path == RENAME => {833 if let Ok(s) = get_lit_str(cx, RENAME, &m.lit) {825 Meta::NameValue(m) if m.path == RENAME => {826 if let Ok(s) = get_lit_str(cx, RENAME, &m.value) {827 ser_name.set(&m.path, s.value());828 de_name.set_if_none(s.value());829 de_aliases.insert(&m.path, s.value());830 }831 }832833 // Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`841 Meta(List(m)) if m.path == RENAME => {842 if let Ok((ser, de)) = get_multiple_renames(cx, &m.nested) {843 ser_name.set_opt(&m.path, ser.map(syn::LitStr::value));834 Meta::List(m) if m.path == RENAME => {835 if let Ok((ser, de)) = get_multiple_renames(cx, m) {836 ser_name.set_opt(&m.path, ser.as_ref().map(syn::LitStr::value));837 for de_value in de {838 de_name.set_if_none(de_value.value());839 de_aliases.insert(&m.path, de_value.value());840 }841 }842 }843844 // Parse `#[serde(alias = "foo")]`852 Meta(NameValue(m)) if m.path == ALIAS => {853 if let Ok(s) = get_lit_str(cx, ALIAS, &m.lit) {845 Meta::NameValue(m) if m.path == ALIAS => {846 if let Ok(s) = get_lit_str(cx, ALIAS, &m.value) {847 de_aliases.insert(&m.path, s.value());848 }849 }850851 // Parse `#[serde(rename_all = "foo")]`859 Meta(NameValue(m)) if m.path == RENAME_ALL => {860 if let Ok(s) = get_lit_str(cx, RENAME_ALL, &m.lit) {852 Meta::NameValue(m) if m.path == RENAME_ALL => {853 if let Ok(s) = get_lit_str(cx, RENAME_ALL, &m.value) {854 match RenameRule::from_str(&s.value()) {855 Ok(rename_rule) => {856 rename_all_ser_rule.set(&m.path, rename_rule);857 rename_all_de_rule.set(&m.path, rename_rule);
3 unmodified lines
861 }862 }863864 // Parse `#[serde(rename_all(serialize = "foo", deserialize = "bar"))]`872 Meta(List(m)) if m.path == RENAME_ALL => {873 if let Ok((ser, de)) = get_renames(cx, &m.nested) {865 Meta::List(m) if m.path == RENAME_ALL => {866 if let Ok((ser, de)) = get_renames(cx, m) {867 if let Some(ser) = ser {868 match RenameRule::from_str(&ser.value()) {869 Ok(rename_rule) => rename_all_ser_rule.set(&m.path, rename_rule),870 Err(err) => cx.error_spanned_by(ser, err),
8 unmodified lines
879 }880 }881882 // Parse `#[serde(skip)]`890 Meta(Path(word)) if word == SKIP => {883 Meta::Path(word) if word == SKIP => {884 skip_serializing.set_true(word);885 skip_deserializing.set_true(word);886 }887888 // Parse `#[serde(skip_deserializing)]`896 Meta(Path(word)) if word == SKIP_DESERIALIZING => {889 Meta::Path(word) if word == SKIP_DESERIALIZING => {890 skip_deserializing.set_true(word);891 }892893 // Parse `#[serde(skip_serializing)]`901 Meta(Path(word)) if word == SKIP_SERIALIZING => {894 Meta::Path(word) if word == SKIP_SERIALIZING => {895 skip_serializing.set_true(word);896 }897898 // Parse `#[serde(other)]`906 Meta(Path(word)) if word == OTHER => {899 Meta::Path(word) if word == OTHER => {900 other.set_true(word);901 }902903 // Parse `#[serde(bound = "T: SomeBound")]`911 Meta(NameValue(m)) if m.path == BOUND => {912 if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.lit) {904 Meta::NameValue(m) if m.path == BOUND => {905 if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.value) {906 ser_bound.set(&m.path, where_predicates.clone());907 de_bound.set(&m.path, where_predicates);908 }909 }910911 // Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`919 Meta(List(m)) if m.path == BOUND => {920 if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {912 Meta::List(m) if m.path == BOUND => {913 if let Ok((ser, de)) = get_where_predicates(cx, m) {914 ser_bound.set_opt(&m.path, ser);915 de_bound.set_opt(&m.path, de);916 }917 }918919 // Parse `#[serde(with = "...")]`927 Meta(NameValue(m)) if m.path == WITH => {928 if let Ok(path) = parse_lit_into_expr_path(cx, WITH, &m.lit) {920 Meta::NameValue(m) if m.path == WITH => {921 if let Ok(path) = parse_lit_into_expr_path(cx, WITH, &m.value) {922 let mut ser_path = path.clone();923 ser_path924 .path925 .segments
8 unmodified lines
934 }935 }936937 // Parse `#[serde(serialize_with = "...")]`945 Meta(NameValue(m)) if m.path == SERIALIZE_WITH => {946 if let Ok(path) = parse_lit_into_expr_path(cx, SERIALIZE_WITH, &m.lit) {938 Meta::NameValue(m) if m.path == SERIALIZE_WITH => {939 if let Ok(path) = parse_lit_into_expr_path(cx, SERIALIZE_WITH, &m.value) {940 serialize_with.set(&m.path, path);941 }942 }943944 // Parse `#[serde(deserialize_with = "...")]`952 Meta(NameValue(m)) if m.path == DESERIALIZE_WITH => {953 if let Ok(path) = parse_lit_into_expr_path(cx, DESERIALIZE_WITH, &m.lit) {945 Meta::NameValue(m) if m.path == DESERIALIZE_WITH => {946 if let Ok(path) = parse_lit_into_expr_path(cx, DESERIALIZE_WITH, &m.value) {947 deserialize_with.set(&m.path, path);948 }949 }950951 // Parse `#[serde(borrow)]`959 Meta(Path(word)) if word == BORROW => match &variant.fields {952 Meta::Path(word) if word == BORROW => match &variant.fields {953 syn::Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {954 borrow.set(955 word,956 BorrowAttribute {
8 unmodified lines
965 }966 },967968 // Parse `#[serde(borrow = "'a + 'b")]`976 Meta(NameValue(m)) if m.path == BORROW => match &variant.fields {969 Meta::NameValue(m) if m.path == BORROW => match &variant.fields {970 syn::Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {978 if let Ok(lifetimes) = parse_lit_into_lifetimes(cx, &m.lit) {971 if let Ok(lifetimes) = parse_lit_into_lifetimes(cx, &m.value) {972 borrow.set(973 &m.path,974 BorrowAttribute {975 path: m.path.clone(),
7 unmodified lines
983 cx.error_spanned_by(variant, msg);984 }985 },986994 Meta(meta_item) => {987 meta_item => {988 let path = meta_item989 .path()990 .into_token_stream()991 .to_string()992 .replace(' ', "");993 let msg = format!("unknown serde variant attribute `{}`", path);994 cx.error_spanned_by(meta_item.path(), msg);995 }10031004 Lit(lit) => {1005 let msg = "unexpected literal in serde variant attribute";1006 cx.error_spanned_by(lit, msg);1007 }996 }997 }998999 Variant {
152 unmodified lines
1152 .flatten()1153 {1154 match &meta_item {1155 // Parse `#[serde(rename = "foo")]`1168 Meta(NameValue(m)) if m.path == RENAME => {1169 if let Ok(s) = get_lit_str(cx, RENAME, &m.lit) {1156 Meta::NameValue(m) if m.path == RENAME => {1157 if let Ok(s) = get_lit_str(cx, RENAME, &m.value) {1158 ser_name.set(&m.path, s.value());1159 de_name.set_if_none(s.value());1160 de_aliases.insert(&m.path, s.value());1161 }1162 }11631164 // Parse `#[serde(rename(serialize = "foo", deserialize = "bar"))]`1177 Meta(List(m)) if m.path == RENAME => {1178 if let Ok((ser, de)) = get_multiple_renames(cx, &m.nested) {1179 ser_name.set_opt(&m.path, ser.map(syn::LitStr::value));1165 Meta::List(m) if m.path == RENAME => {1166 if let Ok((ser, de)) = get_multiple_renames(cx, m) {1167 ser_name.set_opt(&m.path, ser.as_ref().map(syn::LitStr::value));1168 for de_value in de {1169 de_name.set_if_none(de_value.value());1170 de_aliases.insert(&m.path, de_value.value());1171 }1172 }1173 }11741175 // Parse `#[serde(alias = "foo")]`1188 Meta(NameValue(m)) if m.path == ALIAS => {1189 if let Ok(s) = get_lit_str(cx, ALIAS, &m.lit) {1176 Meta::NameValue(m) if m.path == ALIAS => {1177 if let Ok(s) = get_lit_str(cx, ALIAS, &m.value) {1178 de_aliases.insert(&m.path, s.value());1179 }1180 }11811182 // Parse `#[serde(default)]`1195 Meta(Path(word)) if word == DEFAULT => {1183 Meta::Path(word) if word == DEFAULT => {1184 default.set(word, Default::Default);1185 }11861187 // Parse `#[serde(default = "...")]`1200 Meta(NameValue(m)) if m.path == DEFAULT => {1201 if let Ok(path) = parse_lit_into_expr_path(cx, DEFAULT, &m.lit) {1188 Meta::NameValue(m) if m.path == DEFAULT => {1189 if let Ok(path) = parse_lit_into_expr_path(cx, DEFAULT, &m.value) {1190 default.set(&m.path, Default::Path(path));1191 }1192 }11931194 // Parse `#[serde(skip_serializing)]`1207 Meta(Path(word)) if word == SKIP_SERIALIZING => {1195 Meta::Path(word) if word == SKIP_SERIALIZING => {1196 skip_serializing.set_true(word);1197 }11981199 // Parse `#[serde(skip_deserializing)]`1212 Meta(Path(word)) if word == SKIP_DESERIALIZING => {1200 Meta::Path(word) if word == SKIP_DESERIALIZING => {1201 skip_deserializing.set_true(word);1202 }12031204 // Parse `#[serde(skip)]`1217 Meta(Path(word)) if word == SKIP => {1205 Meta::Path(word) if word == SKIP => {1206 skip_serializing.set_true(word);1207 skip_deserializing.set_true(word);1208 }12091210 // Parse `#[serde(skip_serializing_if = "...")]`1223 Meta(NameValue(m)) if m.path == SKIP_SERIALIZING_IF => {1224 if let Ok(path) = parse_lit_into_expr_path(cx, SKIP_SERIALIZING_IF, &m.lit) {1211 Meta::NameValue(m) if m.path == SKIP_SERIALIZING_IF => {1212 if let Ok(path) = parse_lit_into_expr_path(cx, SKIP_SERIALIZING_IF, &m.value) {1213 skip_serializing_if.set(&m.path, path);1214 }1215 }12161217 // Parse `#[serde(serialize_with = "...")]`1230 Meta(NameValue(m)) if m.path == SERIALIZE_WITH => {1231 if let Ok(path) = parse_lit_into_expr_path(cx, SERIALIZE_WITH, &m.lit) {1218 Meta::NameValue(m) if m.path == SERIALIZE_WITH => {1219 if let Ok(path) = parse_lit_into_expr_path(cx, SERIALIZE_WITH, &m.value) {1220 serialize_with.set(&m.path, path);1221 }1222 }12231224 // Parse `#[serde(deserialize_with = "...")]`1237 Meta(NameValue(m)) if m.path == DESERIALIZE_WITH => {1238 if let Ok(path) = parse_lit_into_expr_path(cx, DESERIALIZE_WITH, &m.lit) {1225 Meta::NameValue(m) if m.path == DESERIALIZE_WITH => {1226 if let Ok(path) = parse_lit_into_expr_path(cx, DESERIALIZE_WITH, &m.value) {1227 deserialize_with.set(&m.path, path);1228 }1229 }12301231 // Parse `#[serde(with = "...")]`1244 Meta(NameValue(m)) if m.path == WITH => {1245 if let Ok(path) = parse_lit_into_expr_path(cx, WITH, &m.lit) {1232 Meta::NameValue(m) if m.path == WITH => {1233 if let Ok(path) = parse_lit_into_expr_path(cx, WITH, &m.value) {1234 let mut ser_path = path.clone();1235 ser_path1236 .path1237 .segments
8 unmodified lines
1246 }1247 }12481249 // Parse `#[serde(bound = "T: SomeBound")]`1262 Meta(NameValue(m)) if m.path == BOUND => {1263 if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.lit) {1250 Meta::NameValue(m) if m.path == BOUND => {1251 if let Ok(where_predicates) = parse_lit_into_where(cx, BOUND, BOUND, &m.value) {1252 ser_bound.set(&m.path, where_predicates.clone());1253 de_bound.set(&m.path, where_predicates);1254 }1255 }12561257 // Parse `#[serde(bound(serialize = "...", deserialize = "..."))]`1270 Meta(List(m)) if m.path == BOUND => {1271 if let Ok((ser, de)) = get_where_predicates(cx, &m.nested) {1258 Meta::List(m) if m.path == BOUND => {1259 if let Ok((ser, de)) = get_where_predicates(cx, m) {1260 ser_bound.set_opt(&m.path, ser);1261 de_bound.set_opt(&m.path, de);1262 }1263 }12641265 // Parse `#[serde(borrow)]`1278 Meta(Path(word)) if word == BORROW => {1266 Meta::Path(word) if word == BORROW => {1267 if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, field) {1268 borrowed_lifetimes.set(word, borrowable);1269 }1270 }12711272 // Parse `#[serde(borrow = "'a + 'b")]`1285 Meta(NameValue(m)) if m.path == BORROW => {1286 if let Ok(lifetimes) = parse_lit_into_lifetimes(cx, &m.lit) {1273 Meta::NameValue(m) if m.path == BORROW => {1274 if let Ok(lifetimes) = parse_lit_into_lifetimes(cx, &m.value) {1275 if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, field) {1276 for lifetime in &lifetimes {1277 if !borrowable.contains(lifetime) {1278 let msg = format!(
8 unmodified lines
1287 }1288 }12891290 // Parse `#[serde(getter = "...")]`1303 Meta(NameValue(m)) if m.path == GETTER => {1304 if let Ok(path) = parse_lit_into_expr_path(cx, GETTER, &m.lit) {1291 Meta::NameValue(m) if m.path == GETTER => {1292 if let Ok(path) = parse_lit_into_expr_path(cx, GETTER, &m.value) {1293 getter.set(&m.path, path);1294 }1295 }12961297 // Parse `#[serde(flatten)]`1310 Meta(Path(word)) if word == FLATTEN => {1298 Meta::Path(word) if word == FLATTEN => {1299 flatten.set_true(word);1300 }13011314 Meta(meta_item) => {1302 meta_item => {1303 let path = meta_item1304 .path()1305 .into_token_stream()1306 .to_string()1307 .replace(' ', "");1308 let msg = format!("unknown serde field attribute `{}`", path);1309 cx.error_spanned_by(meta_item.path(), msg);1310 }13231324 Lit(lit) => {1325 let msg = "unexpected literal in serde field attribute";1326 cx.error_spanned_by(lit, msg);1327 }1311 }1312 }13131314 // Is skip_deserializing, initialize the field to Default::default() unless a
145 unmodified lines
1460}14611462type SerAndDe<T> = (Option<T>, Option<T>);14631481fn get_ser_and_de<'a, 'b, T, F>(1482 cx: &'b Ctxt,1464fn get_ser_and_de<'c, T, F>(1465 cx: &'c Ctxt,1466 attr_name: Symbol,1484 metas: &'a Punctuated<syn::NestedMeta, Token![,]>,1467 metas: &MetaList,1468 f: F,1486) -> Result<(VecAttr<'b, T>, VecAttr<'b, T>), ()>1469) -> Result<(VecAttr<'c, T>, VecAttr<'c, T>), ()>1470where1488 T: 'a,1489 F: Fn(&Ctxt, Symbol, Symbol, &'a syn::Lit) -> Result<T, ()>,1471 F: Fn(&Ctxt, Symbol, Symbol, &syn::Expr) -> Result<T, ()>,1472{1473 let mut ser_meta = VecAttr::none(cx, attr_name);1474 let mut de_meta = VecAttr::none(cx, attr_name);14751476 let nested = match metas.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated) {1477 Ok(nested) => nested,1478 Err(err) => {1479 cx.syn_error(err);1480 return Err(());1481 }1482 };14831494 for meta in metas {1484 for meta in nested {1485 match meta {1496 Meta(NameValue(meta)) if meta.path == SERIALIZE => {1497 if let Ok(v) = f(cx, attr_name, SERIALIZE, &meta.lit) {1486 Meta::NameValue(meta) if meta.path == SERIALIZE => {1487 if let Ok(v) = f(cx, attr_name, SERIALIZE, &meta.value) {1488 ser_meta.insert(&meta.path, v);1489 }1490 }14911502 Meta(NameValue(meta)) if meta.path == DESERIALIZE => {1503 if let Ok(v) = f(cx, attr_name, DESERIALIZE, &meta.lit) {1492 Meta::NameValue(meta) if meta.path == DESERIALIZE => {1493 if let Ok(v) = f(cx, attr_name, DESERIALIZE, &meta.value) {1494 de_meta.insert(&meta.path, v);1495 }1496 }1497
10 unmodified lines
15081509 Ok((ser_meta, de_meta))1510}15111522fn get_renames<'a>(1523 cx: &Ctxt,1524 items: &'a Punctuated<syn::NestedMeta, Token![,]>,1525) -> Result<SerAndDe<&'a syn::LitStr>, ()> {1512fn get_renames(cx: &Ctxt, items: &MetaList) -> Result<SerAndDe<syn::LitStr>, ()> {1513 let (ser, de) = get_ser_and_de(cx, RENAME, items, get_lit_str2)?;1514 Ok((ser.at_most_one()?, de.at_most_one()?))1515}15161530fn get_multiple_renames<'a>(1517fn get_multiple_renames(1518 cx: &Ctxt,1532 items: &'a Punctuated<syn::NestedMeta, Token![,]>,1533) -> Result<(Option<&'a syn::LitStr>, Vec<&'a syn::LitStr>), ()> {1519 items: &MetaList,1520) -> Result<(Option<syn::LitStr>, Vec<syn::LitStr>), ()> {1521 let (ser, de) = get_ser_and_de(cx, RENAME, items, get_lit_str2)?;1522 Ok((ser.at_most_one()?, de.get()))1523}15241525fn get_where_predicates(1526 cx: &Ctxt,1540 items: &Punctuated<syn::NestedMeta, Token![,]>,1527 items: &MetaList,1528) -> Result<SerAndDe<Vec<syn::WherePredicate>>, ()> {1529 let (ser, de) = get_ser_and_de(cx, BOUND, items, parse_lit_into_where)?;1530 Ok((ser.at_most_one()?, de.at_most_one()?))1531}15321546pub fn get_serde_meta_items(cx: &Ctxt, attr: &syn::Attribute) -> Result<Vec<syn::NestedMeta>, ()> {1547 if attr.path != SERDE {1533pub fn get_serde_meta_items(cx: &Ctxt, attr: &syn::Attribute) -> Result<Vec<Meta>, ()> {1534 if attr.path() != SERDE {1535 return Ok(Vec::new());1536 }15371551 match attr.parse_meta() {1552 Ok(List(meta)) => Ok(meta.nested.into_iter().collect()),1553 Ok(other) => {1554 cx.error_spanned_by(other, "expected #[serde(...)]");1555 Err(())1556 }1538 let nested = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);15391540 match nested {1541 Ok(nested) => Ok(nested.into_iter().collect()),1542 Err(err) => {1543 cx.syn_error(err);1544 Err(())1545 }1546 }1547}15481564fn get_lit_str<'a>(cx: &Ctxt, attr_name: Symbol, lit: &'a syn::Lit) -> Result<&'a syn::LitStr, ()> {1549fn get_lit_str(cx: &Ctxt, attr_name: Symbol, lit: &syn::Expr) -> Result<syn::LitStr, ()> {1550 get_lit_str2(cx, attr_name, attr_name, lit)1551}15521568fn get_lit_str2<'a>(1553fn get_lit_str2(1554 cx: &Ctxt,1555 attr_name: Symbol,1556 meta_item_name: Symbol,1572 lit: &'a syn::Lit,1573) -> Result<&'a syn::LitStr, ()> {1574 if let syn::Lit::Str(lit) = lit {1575 Ok(lit)1576 } else {1577 let msg = format!(1578 "expected serde {} attribute to be a string: `{} = \"...\"`",1579 attr_name, meta_item_name,1580 );1581 cx.error_spanned_by(lit, msg);1582 Err(())1557 expr: &syn::Expr,1558) -> Result<syn::LitStr, ()> {1559 if let Expr::Lit(expr) = expr {1560 if let syn::Lit::Str(lit) = &expr.lit {1561 return Ok(lit.clone());1562 }1563 }1564 let msg = format!(1565 "expected serde {} attribute to be a string: `{} = \"...\"`",1566 attr_name, meta_item_name,1567 );1568 cx.error_spanned_by(expr, msg);1569 Err(())1570}15711586fn parse_lit_into_path(cx: &Ctxt, attr_name: Symbol, lit: &syn::Lit) -> Result<syn::Path, ()> {1572fn parse_lit_into_path(cx: &Ctxt, attr_name: Symbol, lit: &syn::Expr) -> Result<syn::Path, ()> {1573 let string = get_lit_str(cx, attr_name, lit)?;1574 string.parse().map_err(|_| {1575 let msg = format!("failed to parse path: {:?}", string.value());1576 cx.error_spanned_by(lit, msg);
2 unmodified lines
15791580fn parse_lit_into_expr_path(1581 cx: &Ctxt,1582 attr_name: Symbol,1597 lit: &syn::Lit,1583 lit: &syn::Expr,1584) -> Result<syn::ExprPath, ()> {1585 let string = get_lit_str(cx, attr_name, lit)?;1586 string.parse().map_err(|_| {1587 let msg = format!("failed to parse path: {:?}", string.value());
4 unmodified lines
1592fn parse_lit_into_where(1593 cx: &Ctxt,1594 attr_name: Symbol,1595 meta_item_name: Symbol,1610 lit: &syn::Lit,1596 lit: &syn::Expr,1597) -> Result<Vec<syn::WherePredicate>, ()> {1598 let string = get_lit_str2(cx, attr_name, meta_item_name, lit)?;15991600 string
1 unmodified line
1602 .map(Vec::from_iter)1603 .map_err(|err| cx.error_spanned_by(lit, err))1604}16051620fn parse_lit_into_ty(cx: &Ctxt, attr_name: Symbol, lit: &syn::Lit) -> Result<syn::Type, ()> {1606fn parse_lit_into_ty(cx: &Ctxt, attr_name: Symbol, lit: &syn::Expr) -> Result<syn::Type, ()> {1607 let string = get_lit_str(cx, attr_name, lit)?;16081609 string.parse().map_err(|_| {1610 let msg = format!("failed to parse type: {} = {:?}", attr_name, string.value());
2 unmodified lines
1613}16141615// Parses a string literal like "'a + 'b + 'c" containing a nonempty list of1616// lifetimes separated by `+`.1631fn parse_lit_into_lifetimes(cx: &Ctxt, lit: &syn::Lit) -> Result<BTreeSet<syn::Lifetime>, ()> {1617fn parse_lit_into_lifetimes(cx: &Ctxt, lit: &syn::Expr) -> Result<BTreeSet<syn::Lifetime>, ()> {1618 let string = get_lit_str(cx, BORROW, lit)?;16191620 if let Ok(lifetimes) = string.parse_with(|input: ParseStream| {1621 let mut set = BTreeSet::new();
216 unmodified lines
1838 }1839 syn::GenericArgument::Type(ty) => {1840 collect_lifetimes(ty, out);1841 }1856 syn::GenericArgument::Binding(binding) => {1842 syn::GenericArgument::AssocType(binding) => {1843 collect_lifetimes(&binding.ty, out);1844 }1859 syn::GenericArgument::Constraint(_)1860 | syn::GenericArgument::Const(_) => {}1845 _ => {}1846 }1847 }1848 }1849 }
42 unmodified lines
modified:serde_derive/src/internals/receiver.rs+12 added-5 removed
177 unmodified lines
178 PathArguments::AngleBracketed(arguments) => {179 for arg in &mut arguments.args {180 match arg {181 GenericArgument::Type(arg) => self.visit_type_mut(arg),182 GenericArgument::Binding(arg) => self.visit_type_mut(&mut arg.ty),182 GenericArgument::AssocType(arg) => self.visit_type_mut(&mut arg.ty),183 GenericArgument::Lifetime(_)184 | GenericArgument::Constraint(_)185 | GenericArgument::Const(_) => {}184 | GenericArgument::Const(_)185 | GenericArgument::AssocConst(_)186 | GenericArgument::Constraint(_) => {}187 #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]188 _ => {}189 }190 }191 }192 PathArguments::Parenthesized(arguments) => {
14 unmodified lines
207208 fn visit_type_param_bound_mut(&mut self, bound: &mut TypeParamBound) {209 match bound {210 TypeParamBound::Trait(bound) => self.visit_path_mut(&mut bound.path),208 TypeParamBound::Lifetime(_) => {}211 TypeParamBound::Lifetime(_) | TypeParamBound::Verbatim(_) => {}212 #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]213 _ => {}214 }215 }216217 fn visit_generics_mut(&mut self, generics: &mut Generics) {
15 unmodified lines
233 for bound in &mut predicate.bounds {234 self.visit_type_param_bound_mut(bound);235 }236 }232 WherePredicate::Lifetime(_) | WherePredicate::Eq(_) => {}237 WherePredicate::Lifetime(_) => {}238 #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]239 _ => {}240 }241 }242 }243 }
49 unmodified lines
modified:serde_derive/src/bound.rs+13 added-5 removed
198 unmodified lines
199 syn::PathArguments::AngleBracketed(arguments) => {200 for arg in &arguments.args {201 match arg {202 syn::GenericArgument::Type(arg) => self.visit_type(arg),203 syn::GenericArgument::Binding(arg) => self.visit_type(&arg.ty),203 syn::GenericArgument::AssocType(arg) => self.visit_type(&arg.ty),204 syn::GenericArgument::Lifetime(_)205 | syn::GenericArgument::Constraint(_)206 | syn::GenericArgument::Const(_) => {}205 | syn::GenericArgument::Const(_)206 | syn::GenericArgument::AssocConst(_)207 | syn::GenericArgument::Constraint(_) => {}208 #[cfg_attr(209 all(test, exhaustive),210 deny(non_exhaustive_omitted_patterns)211 )]212 _ => {}213 }214 }215 }216 syn::PathArguments::Parenthesized(arguments) => {
14 unmodified lines
231232 fn visit_type_param_bound(&mut self, bound: &'ast syn::TypeParamBound) {233 match bound {234 syn::TypeParamBound::Trait(bound) => self.visit_path(&bound.path),229 syn::TypeParamBound::Lifetime(_) => {}235 syn::TypeParamBound::Lifetime(_) | syn::TypeParamBound::Verbatim(_) => {}236 #[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]237 _ => {}238 }239 }240241 // Type parameter should not be considered used by a macro path.
99 unmodified lines
341}342343pub fn with_lifetime_bound(generics: &syn::Generics, lifetime: &str) -> syn::Generics {344 let bound = syn::Lifetime::new(lifetime, Span::call_site());337 let def = syn::LifetimeDef {345 let def = syn::LifetimeParam {346 attrs: Vec::new(),347 lifetime: bound.clone(),348 colon_token: None,349 bounds: Punctuated::new(),
65 unmodified lines
modified:serde_derive/src/de.rs+10 added-10 removed
242 unmodified lines
243 BorrowedLifetimes::Static => syn::Lifetime::new("'static", Span::call_site()),244 }245 }246247 fn de_lifetime_def(&self) -> Option<syn::LifetimeDef> {247 fn de_lifetime_param(&self) -> Option<syn::LifetimeParam> {248 match self {249 BorrowedLifetimes::Borrowed(bounds) => Some(syn::LifetimeDef {249 BorrowedLifetimes::Borrowed(bounds) => Some(syn::LifetimeParam {250 attrs: Vec::new(),251 lifetime: syn::Lifetime::new("'de", Span::call_site()),252 colon_token: None,253 bounds: bounds.iter().cloned().collect(),
2756 unmodified lines
30103011impl<'a> ToTokens for DeImplGenerics<'a> {3012 fn to_tokens(&self, tokens: &mut TokenStream) {3013 let mut generics = self.0.generics.clone();3014 if let Some(de_lifetime) = self.0.borrowed.de_lifetime_def() {3014 if let Some(de_lifetime) = self.0.borrowed.de_lifetime_param() {3015 generics.params = Some(syn::GenericParam::Lifetime(de_lifetime))3016 .into_iter()3017 .chain(generics.params)3018 .collect();
26 unmodified lines
3045 generics.params = Some(syn::GenericParam::Lifetime(place_lifetime))3046 .into_iter()3047 .chain(generics.params)3048 .collect();3049 if let Some(de_lifetime) = self.0.borrowed.de_lifetime_def() {3049 if let Some(de_lifetime) = self.0.borrowed.de_lifetime_param() {3050 generics.params = Some(syn::GenericParam::Lifetime(de_lifetime))3051 .into_iter()3052 .chain(generics.params)3053 .collect();
16 unmodified lines
30703071impl<'a> ToTokens for DeTypeGenerics<'a> {3072 fn to_tokens(&self, tokens: &mut TokenStream) {3073 let mut generics = self.0.generics.clone();3074 if self.0.borrowed.de_lifetime_def().is_some() {3075 let def = syn::LifetimeDef {3074 if self.0.borrowed.de_lifetime_param().is_some() {3075 let def = syn::LifetimeParam {3076 attrs: Vec::new(),3077 lifetime: syn::Lifetime::new("'de", Span::call_site()),3078 colon_token: None,3079 bounds: Punctuated::new(),
16 unmodified lines
3096 .into_iter()3097 .chain(generics.params)3098 .collect();30993100 if self.0.borrowed.de_lifetime_def().is_some() {3101 let def = syn::LifetimeDef {3100 if self.0.borrowed.de_lifetime_param().is_some() {3101 let def = syn::LifetimeParam {3102 attrs: Vec::new(),3103 lifetime: syn::Lifetime::new("'de", Span::call_site()),3104 colon_token: None,3105 bounds: Punctuated::new(),
15 unmodified lines
3121 }3122}31233124#[cfg(feature = "deserialize_in_place")]3125fn place_lifetime() -> syn::LifetimeDef {3126 syn::LifetimeDef {3125fn place_lifetime() -> syn::LifetimeParam {3126 syn::LifetimeParam {3127 attrs: Vec::new(),3128 lifetime: syn::Lifetime::new("'place", Span::call_site()),3129 colon_token: None,3130 bounds: Punctuated::new(),
16 unmodified lines
modified:serde_derive/Cargo.toml+1 added-1 removed
22 unmodified lines
2324[dependencies]25proc-macro2 = "1.0"26quote = "1.0"27syn = "1.0.104"27syn = "2.0"2829[dev-dependencies]30serde = { version = "1.0", path = "../serde" }31
2 unmodified lines
modified:serde_derive_internals/Cargo.toml+1 added-1 removed
modified:test_suite/tests/ui/malformed/cut_off.stderr+1 added-1 removed
1error: unexpected end of input, expected literal1error: unexpected end of input, expected an expression2 --> tests/ui/malformed/cut_off.rs:4:173 |44 | #[serde(rename =)]5 | ^
modified:test_suite/tests/ui/malformed/not_list.stderr+4 added-4 removed
1error: expected #[serde(...)]1error: expected attribute arguments in parentheses: #[serde(...)]2 --> tests/ui/malformed/not_list.rs:4:33 |44 | #[serde]5 | ^^^^^67error: expected #[serde(...)]8 --> tests/ui/malformed/not_list.rs:5:37error: expected parentheses: #[serde(...)]8 --> tests/ui/malformed/not_list.rs:5:99 |105 | #[serde = "?"]11 | ^^^^^^^^^^^11 | ^
modified:test_suite/tests/ui/unexpected-literal/container.stderr+1 added-1 removed
1error: unexpected literal in serde container attribute1error: expected path2 --> tests/ui/unexpected-literal/container.rs:4:93 |44 | #[serde("literal")]5 | ^^^^^^^^^
modified:test_suite/tests/ui/unexpected-literal/field.stderr+1 added-1 removed
1error: unexpected literal in serde field attribute1error: expected path2 --> tests/ui/unexpected-literal/field.rs:5:133 |45 | #[serde("literal")]5 | ^^^^^^^^^
modified:test_suite/tests/ui/unexpected-literal/variant.stderr+1 added-1 removed
1error: unexpected literal in serde variant attribute1error: expected path2 --> tests/ui/unexpected-literal/variant.rs:5:133 |45 | #[serde("literal")]5 | ^^^^^^^^^