Skip to content

Commit

Permalink
Update to syn 2 and darling 0.20.6 (#308)
Browse files Browse the repository at this point in the history
* Update to syn 2

- Bump crates to 2018 edition; this is needed for generated code to
  produce usable error messages.
- Bump darling dependency to 0.20.6
- Remove support for `field(type = "...")` and leave only `field(ty = "...")`
- Update compiletest outputs
- Allow unquoted block expressions
  • Loading branch information
TedDriggs committed Feb 14, 2024
1 parent 1f0a761 commit 04bcde6
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 152 deletions.
1 change: 1 addition & 0 deletions derive_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = [
"Jan-Erik Rediger <janerik@fnordig.de>",
"Ted Driggs <ted.driggs@outlook.com>",
]
edition = "2018"

description = "Rust macro to automatically implement the builder pattern for arbitrary structs."
repository = "https://github.com/colin-kiegel/rust-derive-builder"
Expand Down
2 changes: 1 addition & 1 deletion derive_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ pub use error::UninitializedFieldError;
pub mod export {
pub mod core {
#[cfg(feature = "alloc")]
pub use alloc::string;
pub use ::alloc::string;
#[cfg(not(feature = "std"))]
pub use core::*;
#[cfg(feature = "std")]
Expand Down
4 changes: 2 additions & 2 deletions derive_builder/tests/builder_field_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::num::ParseIntError;

#[derive(Debug, PartialEq, Default, Builder, Clone)]
pub struct Lorem {
#[builder(field(ty = "Option<usize>", build = "self.ipsum.unwrap_or(42) + 1"))]
#[builder(field(ty = "Option<usize>", build = self.ipsum.unwrap_or(42) + 1))]
ipsum: usize,

#[builder(setter(into), field(ty = "String", build = "self.dolor.parse()?"))]
#[builder(setter(into), field(ty = "String", build = self.dolor.parse()?))]
dolor: u32,
}

Expand Down
5 changes: 0 additions & 5 deletions derive_builder/tests/compile-fail/builder_field_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ pub struct Lorem {
)]
ipsum: usize,

// Both `ty` and `type` are temporarily allowed to ease the transition
// to syn 2.0, but they are mutually exclusive.
#[builder(field(ty = "usize", type = "usize"))]
dolor: usize,

// `default` is incompatible with `field.ty`, even without `field.build`
#[builder(default = "2", field(ty = "usize"))]
sit: usize,
Expand Down
22 changes: 8 additions & 14 deletions derive_builder/tests/compile-fail/builder_field_custom.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@ error: #[builder(default)] and #[builder(field(build="..."))] cannot be used tog
8 | default = "1",
| ^^^

error: duplicate field - `type` is a deprecated alias for `ty`.
--> tests/compile-fail/builder_field_custom.rs:15:35
error: #[builder(default)] and #[builder(field(ty="..."))] cannot be used together
--> tests/compile-fail/builder_field_custom.rs:14:25
|
15 | #[builder(field(ty = "usize", type = "usize"))]
| ^^^^

error: #[builder(default)] and #[builder(field(type="..."))] cannot be used together
--> tests/compile-fail/builder_field_custom.rs:19:25
|
19 | #[builder(default = "2", field(ty = "usize"))]
14 | #[builder(default = "2", field(ty = "usize"))]
| ^^^

error: #[builder(default)] and #[builder(field(build="..."))] cannot be used together
--> tests/compile-fail/builder_field_custom.rs:23:25
--> tests/compile-fail/builder_field_custom.rs:18:25
|
23 | #[builder(default = "3", field(ty = "usize", build = "self.ipsum + 42"))]
18 | #[builder(default = "3", field(ty = "usize", build = "self.ipsum + 42"))]
| ^^^

error: #[builder(default)] and #[builder(field(type="..."))] cannot be used together
--> tests/compile-fail/builder_field_custom.rs:23:25
error: #[builder(default)] and #[builder(field(ty="..."))] cannot be used together
--> tests/compile-fail/builder_field_custom.rs:18:25
|
23 | #[builder(default = "3", field(ty = "usize", build = "self.ipsum + 42"))]
18 | #[builder(default = "3", field(ty = "usize", build = "self.ipsum + 42"))]
| ^^^
41 changes: 33 additions & 8 deletions derive_builder/tests/compile-fail/crate_root.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ error[E0433]: failed to resolve: could not find `export` in `empty`
| ^^^^^^^ not found in `empty::export::core::option`
|
= note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this enum
help: consider importing one of these items
|
5 | use core::option::Option;
|
5 | use derive_builder::export::core::option::Option;
|
5 | use serde::__private::Option;
|
5 | use std::option::Option;
|

error[E0433]: failed to resolve: could not find `export` in `empty`
--> tests/compile-fail/crate_root.rs:7:10
Expand All @@ -25,10 +31,16 @@ error[E0433]: failed to resolve: could not find `export` in `empty`
| ^^^^^^^ not found in `empty::export::core::clone`
|
= note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this trait
help: consider importing one of these items
|
5 | use core::clone::Clone;
|
5 | use derive_builder::export::core::clone::Clone;
|
5 | use serde::__private::Clone;
|
5 | use std::clone::Clone;
|

error[E0433]: failed to resolve: could not find `export` in `empty`
--> tests/compile-fail/crate_root.rs:7:10
Expand All @@ -39,14 +51,15 @@ error[E0433]: failed to resolve: could not find `export` in `empty`
= note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing one of these items
|
5 | use derive_builder::export::core::fmt::Result;
5 | use core::fmt::Result;
|
5 | use derive_builder::export::core::io::Result;
5 | use core::result::Result;
|
5 | use derive_builder::export::core::result::Result;
5 | use derive_builder::export::core::fmt::Result;
|
5 | use derive_builder::export::core::thread::Result;
5 | use derive_builder::export::core::io::Result;
|
and 9 other candidates

error[E0433]: failed to resolve: could not find `export` in `empty`
--> tests/compile-fail/crate_root.rs:7:10
Expand All @@ -55,10 +68,16 @@ error[E0433]: failed to resolve: could not find `export` in `empty`
| ^^^^^^^ not found in `empty::export::core::convert`
|
= note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this trait
help: consider importing one of these items
|
5 | use core::convert::Into;
|
5 | use derive_builder::export::core::convert::Into;
|
5 | use serde::__private::Into;
|
5 | use std::convert::Into;
|

error[E0433]: failed to resolve: could not find `UninitializedFieldError` in `empty`
--> tests/compile-fail/crate_root.rs:7:10
Expand All @@ -79,10 +98,16 @@ error[E0433]: failed to resolve: could not find `export` in `empty`
| ^^^^^^^ not found in `empty::export::core::default`
|
= note: this error originates in the derive macro `Builder` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this trait
help: consider importing one of these items
|
5 | use core::default::Default;
|
5 | use derive_builder::export::core::default::Default;
|
5 | use serde::__private::Default;
|
5 | use std::default::Default;
|

error[E0412]: cannot find type `UninitializedFieldError` in module `empty`
--> tests/compile-fail/crate_root.rs:7:10
Expand Down
8 changes: 4 additions & 4 deletions derive_builder_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = [
"Jan-Erik Rediger <janerik@fnordig.de>",
"Ted Driggs <ted.driggs@outlook.com>",
]

edition = "2018"
description = "Internal helper library for the derive_builder crate."
repository = "https://github.com/colin-kiegel/rust-derive-builder"
documentation = "https://docs.rs/derive_builder_core"
Expand All @@ -21,10 +21,10 @@ clippy = []
lib_has_std = []

[dependencies]
darling = "0.14.0"
darling = "0.20.6"
proc-macro2 = "1.0.37"
quote = "1.0.18"
syn = { version = "1.0.91", features = ["full", "extra-traits"] }
quote = "1.0.35"
syn = { version = "2.0.15", features = ["full", "extra-traits"] }

[dev-dependencies]
pretty_assertions = "1.2.1"
18 changes: 15 additions & 3 deletions derive_builder_core/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::convert::TryFrom;

use proc_macro2::TokenStream;
use proc_macro2::{Span, TokenStream};
use quote::ToTokens;
use syn::{self, spanned::Spanned, Block, LitStr};

Expand All @@ -16,6 +16,10 @@ impl BlockContents {
pub fn is_empty(&self) -> bool {
self.0.stmts.is_empty()
}

pub fn span(&self) -> Span {
self.0.span()
}
}

impl ToTokens for BlockContents {
Expand All @@ -39,7 +43,7 @@ impl From<syn::Expr> for BlockContents {
fn from(v: syn::Expr) -> Self {
Self(Block {
brace_token: syn::token::Brace(v.span()),
stmts: vec![syn::Stmt::Expr(v)],
stmts: vec![syn::Stmt::Expr(v, None)],
})
}
}
Expand All @@ -57,6 +61,14 @@ impl darling::FromMeta for BlockContents {
Err(darling::Error::unexpected_lit_type(value))
}
}

fn from_expr(expr: &syn::Expr) -> darling::Result<Self> {
if let syn::Expr::Lit(lit) = expr {
Self::from_value(&lit.lit)
} else {
Ok(Self::from(expr.clone()))
}
}
}

#[cfg(test)]
Expand All @@ -71,7 +83,7 @@ mod test {
}

#[test]
#[should_panic(expected = r#"lex error"#)]
#[should_panic(expected = r#"cannot parse"#)]
fn block_invalid_token_trees() {
parse("let x = 2; { x+1").unwrap();
}
Expand Down
9 changes: 3 additions & 6 deletions derive_builder_core/src/build_method.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use std::borrow::Cow;

use doc_comment_from;
use proc_macro2::{Span, TokenStream};
use quote::{ToTokens, TokenStreamExt};
use syn;
use syn::spanned::Spanned;
use BuilderPattern;
use Initializer;
use DEFAULT_STRUCT_NAME;

use crate::DefaultExpression;
use crate::{
doc_comment_from, BuilderPattern, DefaultExpression, Initializer, DEFAULT_STRUCT_NAME,
};

/// Initializer for the struct fields in the build method, implementing
/// `quote::ToTokens`.
Expand Down
11 changes: 4 additions & 7 deletions derive_builder_core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ use quote::{format_ident, ToTokens, TokenStreamExt};
use syn::punctuated::Punctuated;
use syn::{self, Path, TraitBound, TraitBoundModifier, TypeParamBound};

use doc_comment_from;
use BuildMethod;
use BuilderField;
use BuilderPattern;
use DeprecationNotes;
use Setter;
use crate::{
doc_comment_from, BuildMethod, BuilderField, BuilderPattern, DeprecationNotes, Setter,
};

const ALLOC_NOT_ENABLED_ERROR: &str = r#"`alloc` is disabled within 'derive_builder', consider one of the following:
* enable feature `alloc` on 'dervie_builder' if a `global_allocator` is present
Expand Down Expand Up @@ -858,7 +855,7 @@ mod tests {

add_simple_foo_builder(&mut result);

result.append_all(quote!(compile_error! { #ALLOC_NOT_ENABLED_ERROR }));
result.append_all(quote!(::core::compile_error! { #ALLOC_NOT_ENABLED_ERROR }));

result
}
Expand Down
1 change: 0 additions & 1 deletion derive_builder_core/src/builder_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::borrow::Cow;

use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt};
use syn;

/// Field for the builder struct, implementing `quote::ToTokens`.
///
Expand Down
16 changes: 7 additions & 9 deletions derive_builder_core/src/default_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ impl DefaultExpression {
}
}

pub fn span(&self) -> Span {
match self {
DefaultExpression::Explicit(block) => block.span(),
DefaultExpression::Trait => Span::call_site(),
}
}

#[cfg(test)]
pub fn explicit<I: Into<BlockContents>>(content: I) -> Self {
DefaultExpression::Explicit(content.into())
Expand All @@ -39,15 +46,6 @@ impl darling::FromMeta for DefaultExpression {
}
}

impl syn::spanned::Spanned for DefaultExpression {
fn span(&self) -> Span {
match self {
DefaultExpression::Explicit(block) => block.span(),
DefaultExpression::Trait => Span::call_site(),
}
}
}

/// Wrapper for `DefaultExpression`
struct DefaultExpressionWithCrateRoot<'a> {
crate_root: &'a syn::Path,
Expand Down
1 change: 0 additions & 1 deletion derive_builder_core/src/deprecation_notes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use proc_macro2::{Span, TokenStream};
use quote::{ToTokens, TokenStreamExt};
use syn;

/// Deprecation notes we want to emit to the user, implementing
/// `quote::ToTokens`.
Expand Down
5 changes: 1 addition & 4 deletions derive_builder_core/src/initializer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use proc_macro2::{Span, TokenStream};
use quote::{ToTokens, TokenStreamExt};
use syn;
use BuilderPattern;
use DEFAULT_STRUCT_NAME;

use crate::{change_span, BlockContents, DefaultExpression};
use crate::{change_span, BlockContents, BuilderPattern, DefaultExpression, DEFAULT_STRUCT_NAME};

/// Initializer for the target struct fields, implementing `quote::ToTokens`.
///
Expand Down

0 comments on commit 04bcde6

Please sign in to comment.