Skip to content

Commit

Permalink
Merge pull request #1821 from dtolnay/ungroup
Browse files Browse the repository at this point in the history
Look inside of None-delimited groups when examining types
  • Loading branch information
dtolnay committed May 30, 2020
2 parents 31fe82a + c45a809 commit ef16c81
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions serde_derive/src/bound.rs
Expand Up @@ -5,7 +5,7 @@ use syn::punctuated::{Pair, Punctuated};
use syn::visit::{self, Visit};

use internals::ast::{Container, Data};
use internals::attr;
use internals::{attr, ungroup};

use proc_macro2::Span;

Expand Down Expand Up @@ -114,7 +114,7 @@ pub fn with_bound(
}
impl<'ast> Visit<'ast> for FindTyParams<'ast> {
fn visit_field(&mut self, field: &'ast syn::Field) {
if let syn::Type::Path(ty) = &field.ty {
if let syn::Type::Path(ty) = ungroup(&field.ty) {
if let Some(Pair::Punctuated(t, _)) = ty.path.segments.pairs().next() {
if self.all_type_params.contains(&t.ident) {
self.associated_type_usage.push(ty);
Expand Down
4 changes: 2 additions & 2 deletions serde_derive/src/de.rs
Expand Up @@ -8,7 +8,7 @@ use bound;
use dummy;
use fragment::{Expr, Fragment, Match, Stmts};
use internals::ast::{Container, Data, Field, Style, Variant};
use internals::{attr, Ctxt, Derive};
use internals::{attr, ungroup, Ctxt, Derive};
use pretend;

use std::collections::BTreeSet;
Expand Down Expand Up @@ -77,7 +77,7 @@ fn precondition(cx: &Ctxt, cont: &Container) {
fn precondition_sized(cx: &Ctxt, cont: &Container) {
if let Data::Struct(_, fields) = &cont.data {
if let Some(last) = fields.last() {
if let syn::Type::Slice(_) = *last.ty {
if let syn::Type::Slice(_) = ungroup(last.ty) {
cx.error_spanned_by(
cont.original,
"cannot deserialize a dynamically sized struct",
Expand Down
12 changes: 6 additions & 6 deletions serde_derive/src/internals/attr.rs
@@ -1,5 +1,5 @@
use internals::symbol::*;
use internals::Ctxt;
use internals::{ungroup, Ctxt};
use proc_macro2::{Group, Span, TokenStream, TokenTree};
use quote::ToTokens;
use std::borrow::Cow;
Expand Down Expand Up @@ -1737,7 +1737,7 @@ fn is_implicitly_borrowed_reference(ty: &syn::Type) -> bool {
// cow: Cow<'a, str>,
// }
fn is_cow(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
let path = match ty {
let path = match ungroup(ty) {
syn::Type::Path(ty) => &ty.path,
_ => {
return false;
Expand All @@ -1764,7 +1764,7 @@ fn is_cow(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
}

fn is_option(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
let path = match ty {
let path = match ungroup(ty) {
syn::Type::Path(ty) => &ty.path,
_ => {
return false;
Expand Down Expand Up @@ -1811,7 +1811,7 @@ fn is_option(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
// r: &'a str,
// }
fn is_reference(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
match ty {
match ungroup(ty) {
syn::Type::Reference(ty) => ty.mutability.is_none() && elem(&ty.elem),
_ => false,
}
Expand All @@ -1822,14 +1822,14 @@ fn is_str(ty: &syn::Type) -> bool {
}

fn is_slice_u8(ty: &syn::Type) -> bool {
match ty {
match ungroup(ty) {
syn::Type::Slice(ty) => is_primitive_type(&ty.elem, "u8"),
_ => false,
}
}

fn is_primitive_type(ty: &syn::Type, primitive: &str) -> bool {
match ty {
match ungroup(ty) {
syn::Type::Path(ty) => ty.qself.is_none() && is_primitive_path(&ty.path, primitive),
_ => false,
}
Expand Down
4 changes: 2 additions & 2 deletions serde_derive/src/internals/check.rs
@@ -1,6 +1,6 @@
use internals::ast::{Container, Data, Field, Style};
use internals::attr::{Identifier, TagType};
use internals::{Ctxt, Derive};
use internals::{ungroup, Ctxt, Derive};
use syn::{Member, Type};

/// Cross-cutting checks that require looking at more than a single attrs
Expand Down Expand Up @@ -396,7 +396,7 @@ fn member_message(member: &Member) -> String {
}

fn allow_transparent(field: &Field, derive: Derive) -> bool {
if let Type::Path(ty) = field.ty {
if let Type::Path(ty) = ungroup(&field.ty) {
if let Some(seg) = ty.path.segments.last() {
if seg.ident == "PhantomData" {
return false;
Expand Down
9 changes: 9 additions & 0 deletions serde_derive/src/internals/mod.rs
Expand Up @@ -8,8 +8,17 @@ mod case;
mod check;
mod symbol;

use syn::Type;

#[derive(Copy, Clone)]
pub enum Derive {
Serialize,
Deserialize,
}

pub fn ungroup(mut ty: &Type) -> &Type {
while let Type::Group(group) = ty {
ty = &group.elem;
}
ty
}
11 changes: 11 additions & 0 deletions test_suite/tests/test_gen.rs
Expand Up @@ -708,6 +708,17 @@ fn test_gen() {
x: u8,
y: u16,
}

macro_rules! deriving {
($field:ty) => {
#[derive(Deserialize)]
struct MacroRules<'a> {
field: $field,
}
};
}

deriving!(&'a str);
}

//////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit ef16c81

Please sign in to comment.