Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always declare locals as mutable in derived impls #185

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Fix a bug where using a trait that accepts `#[darling(attributes(...))]` without specifying any attributes would emit code that did not compile. [#183](https://github.com/TedDriggs/darling/issues/183)

## v0.14.0 (April 13, 2022)

- **BREAKING CHANGE:** Remove many trait impls from `util::Flag`.
Expand Down
15 changes: 1 addition & 14 deletions core/src/codegen/attr_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ pub trait ExtractAttribute {
/// A set of mutable declarations for all members of the implementing type.
fn local_declarations(&self) -> TokenStream;

/// A set of immutable declarations for all members of the implementing type.
/// This is used in the case where a deriving struct handles no attributes and therefore can
/// never change its default state.
fn immutable_declarations(&self) -> TokenStream;

/// Gets the list of attribute names that should be parsed by the extractor.
fn attr_names(&self) -> &PathList;

Expand All @@ -32,17 +27,9 @@ pub trait ExtractAttribute {
/// Gets the core from-meta-item loop that should be used on matching attributes.
fn core_loop(&self) -> TokenStream;

fn declarations(&self) -> TokenStream {
if !self.attr_names().is_empty() {
self.local_declarations()
} else {
self.immutable_declarations()
}
}

/// Generates the main extraction loop.
fn extractor(&self) -> TokenStream {
let declarations = self.declarations();
let declarations = self.local_declarations();

let will_parse_any = !self.attr_names().is_empty();
let will_fwd_any = self
Expand Down
17 changes: 4 additions & 13 deletions core/src/codegen/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a> Field<'a> {
}

pub fn as_declaration(&'a self) -> Declaration<'a> {
Declaration(self, !self.skip)
Declaration(self)
}

pub fn as_match(&'a self) -> MatchArm<'a> {
Expand All @@ -61,28 +61,19 @@ impl<'a> UsesTypeParams for Field<'a> {
}

/// An individual field during variable declaration in the generated parsing method.
pub struct Declaration<'a>(&'a Field<'a>, bool);

impl<'a> Declaration<'a> {
/// Creates a new declaration with the given field and mutability.
pub fn new(field: &'a Field<'a>, mutable: bool) -> Self {
Declaration(field, mutable)
}
}
pub struct Declaration<'a>(&'a Field<'a>);

impl<'a> ToTokens for Declaration<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let field: &Field = self.0;
let ident = field.ident;
let ty = field.ty;

let mutable = if self.1 { quote!(mut) } else { quote!() };

tokens.append_all(if field.multiple {
// This is NOT mutable, as it will be declared mutable only temporarily.
quote!(let #mutable #ident: #ty = ::darling::export::Default::default();)
quote!(let mut #ident: #ty = ::darling::export::Default::default();)
} else {
quote!(let #mutable #ident: (bool, ::darling::export::Option<#ty>) = (false, None);)
quote!(let mut #ident: (bool, ::darling::export::Option<#ty>) = (false, None);)
});
}
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/codegen/from_attributes_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ impl<'a> ExtractAttribute for FromAttributesImpl<'a> {
self.base.local_declarations()
}

fn immutable_declarations(&self) -> TokenStream {
self.base.immutable_declarations()
}

fn attr_names(&self) -> &PathList {
self.attr_names
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/codegen/from_derive_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ impl<'a> ExtractAttribute for FromDeriveInputImpl<'a> {
fn local_declarations(&self) -> TokenStream {
self.base.local_declarations()
}

fn immutable_declarations(&self) -> TokenStream {
self.base.immutable_declarations()
}
}

impl<'a> OuterFromImpl<'a> for FromDeriveInputImpl<'a> {
Expand Down
4 changes: 0 additions & 4 deletions core/src/codegen/from_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ impl<'a> ExtractAttribute for FromFieldImpl<'a> {
fn local_declarations(&self) -> TokenStream {
self.base.local_declarations()
}

fn immutable_declarations(&self) -> TokenStream {
self.base.immutable_declarations()
}
}

impl<'a> OuterFromImpl<'a> for FromFieldImpl<'a> {
Expand Down
4 changes: 0 additions & 4 deletions core/src/codegen/from_type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ impl<'a> ExtractAttribute for FromTypeParamImpl<'a> {
fn local_declarations(&self) -> TokenStream {
self.base.local_declarations()
}

fn immutable_declarations(&self) -> TokenStream {
self.base.immutable_declarations()
}
}

impl<'a> OuterFromImpl<'a> for FromTypeParamImpl<'a> {
Expand Down
4 changes: 0 additions & 4 deletions core/src/codegen/from_variant_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ impl<'a> ExtractAttribute for FromVariantImpl<'a> {
self.base.local_declarations()
}

fn immutable_declarations(&self) -> TokenStream {
self.base.immutable_declarations()
}

fn attr_names(&self) -> &PathList {
self.attr_names
}
Expand Down
13 changes: 1 addition & 12 deletions core/src/codegen/trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::{Generics, Ident, WherePredicate};
use crate::ast::{Data, Fields};
use crate::codegen::{
error::{ErrorCheck, ErrorDeclaration},
field, DefaultExpression, Field, FieldsGen, PostfixTransform, Variant,
DefaultExpression, Field, FieldsGen, PostfixTransform, Variant,
};
use crate::usage::{CollectTypeParams, IdentSet, Purpose};

Expand Down Expand Up @@ -97,17 +97,6 @@ impl<'a> TraitImpl<'a> {
}
}

/// Generate immutable variable declarations for all fields.
pub(in crate::codegen) fn immutable_declarations(&self) -> TokenStream {
if let Data::Struct(ref vd) = self.data {
let vdr = vd.as_ref().map(|f| field::Declaration::new(f, false));
let decls = vdr.fields.as_slice();
quote!(#(#decls)*)
} else {
quote!()
}
}

pub(in crate::codegen) fn post_transform_call(&self) -> Option<TokenStream> {
self.post_transform.map(|pt| quote!(#pt))
}
Expand Down
28 changes: 28 additions & 0 deletions tests/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,31 @@ fn verify_skipped_field_not_required() {
}
);
}

/// This test verifies that a skipped field will still prefer an explicit default
/// over the default that would come from its field type. It would be incorrect for
/// `Defaulting::from_derive_input` to fail here, and it would be wrong for the value
/// of `dolor` to be `None`.
#[test]
fn verify_default_supersedes_from_none() {
fn default_dolor() -> Option<u8> {
Some(2)
}

#[derive(Debug, PartialEq, Eq, FromDeriveInput)]
#[darling(attributes(skip_test))]
pub struct Defaulting {
#[darling(skip, default = "default_dolor")]
dolor: Option<u8>,
}

let di = parse_quote! {
#[skip_test]
struct Baz;
};

assert_eq!(
Defaulting::from_derive_input(&di).unwrap(),
Defaulting { dolor: Some(2) }
)
}