Skip to content

Commit c6a4351

Browse files
committed
upgrade syn to v2
https://github.com/dtolnay/syn/releases/tag/2.0.0 syn v2 is already two years old this should speed up compilation in projects using this crate
1 parent 2e4495f commit c6a4351

File tree

8 files changed

+215
-170
lines changed

8 files changed

+215
-170
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.6.42
9+
- Fix `QueryBuilder` for Microsoft SQL Server: https://github.com/sqlpage/sqlx-oldapi/issues/11
10+
- Add support for Microsoft SQL Server DateTime columns in sqlx macros: macros https://github.com/sqlpage/sqlx-oldapi/issues/16
11+
812
## 0.6.41
913
- Upgrade rustls to 0.23
1014
- Provide detailed error messages on TLS connection issues

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ url = { version = "2.2.2", default-features = false }
8585

8686
[dependencies.syn]
8787
# This is basically default features plus "full" but if they add more defaults later then we don't need to enable those.
88-
version = "1.0.109"
88+
version = "2.0.101"
8989
default-features = false
9090
features = ["full", "parsing", "printing", "derive", "clone-impls", "proc-macro"]
9191

sqlx-macros/src/derives/attributes.rs

Lines changed: 104 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use quote::{quote, quote_spanned};
33
use syn::punctuated::Punctuated;
44
use syn::spanned::Spanned;
55
use syn::token::Comma;
6-
use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Variant};
6+
use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, Variant, Expr, LitStr, Token, Path};
77

88
macro_rules! assert_attribute {
99
($e:expr, $err:expr, $input:expr) => {
@@ -83,89 +83,92 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContai
8383

8484
for attr in input
8585
.iter()
86-
.filter(|a| a.path.is_ident("sqlx") || a.path.is_ident("repr"))
86+
.filter(|a| a.path().is_ident("sqlx") || a.path().is_ident("repr"))
8787
{
88-
let meta = attr
89-
.parse_meta()
90-
.map_err(|e| syn::Error::new_spanned(attr, e))?;
91-
match meta {
88+
match &attr.meta {
9289
Meta::List(list) if list.path.is_ident("sqlx") => {
93-
for value in list.nested.iter() {
94-
match value {
95-
NestedMeta::Meta(meta) => match meta {
96-
Meta::Path(p) if p.is_ident("transparent") => {
97-
try_set!(transparent, true, value)
90+
let nested_metas = list.parse_args_with(Punctuated::<Meta, syn::token::Comma>::parse_terminated)?;
91+
for meta_item in nested_metas {
92+
match meta_item {
93+
Meta::Path(p) if p.is_ident("transparent") => {
94+
try_set!(transparent, true, p)
95+
}
96+
Meta::NameValue(mnv) if mnv.path.is_ident("rename_all") => {
97+
if let Expr::Lit(expr_lit) = &mnv.value {
98+
if let Lit::Str(val_str) = &expr_lit.lit {
99+
let val = match &*val_str.value() {
100+
"lowercase" => RenameAll::LowerCase,
101+
"snake_case" => RenameAll::SnakeCase,
102+
"UPPERCASE" => RenameAll::UpperCase,
103+
"SCREAMING_SNAKE_CASE" => RenameAll::ScreamingSnakeCase,
104+
"kebab-case" => RenameAll::KebabCase,
105+
"camelCase" => RenameAll::CamelCase,
106+
"PascalCase" => RenameAll::PascalCase,
107+
_ => fail!(val_str, "unexpected value for rename_all"),
108+
};
109+
try_set!(rename_all, val, &mnv.path)
110+
} else {
111+
fail!(expr_lit, "expected string literal for rename_all")
112+
}
113+
} else {
114+
fail!(&mnv.value, "expected literal expression for rename_all")
98115
}
99-
100-
Meta::NameValue(MetaNameValue {
101-
path,
102-
lit: Lit::Str(val),
103-
..
104-
}) if path.is_ident("rename_all") => {
105-
let val = match &*val.value() {
106-
"lowercase" => RenameAll::LowerCase,
107-
"snake_case" => RenameAll::SnakeCase,
108-
"UPPERCASE" => RenameAll::UpperCase,
109-
"SCREAMING_SNAKE_CASE" => RenameAll::ScreamingSnakeCase,
110-
"kebab-case" => RenameAll::KebabCase,
111-
"camelCase" => RenameAll::CamelCase,
112-
"PascalCase" => RenameAll::PascalCase,
113-
_ => fail!(meta, "unexpected value for rename_all"),
114-
};
115-
116-
try_set!(rename_all, val, value)
116+
}
117+
Meta::NameValue(mnv) if mnv.path.is_ident("type_name") => {
118+
if let Expr::Lit(expr_lit) = &mnv.value {
119+
if let Lit::Str(val_str) = &expr_lit.lit {
120+
try_set!(
121+
type_name,
122+
TypeName {
123+
val: val_str.value(),
124+
span: val_str.span(),
125+
deprecated_rename: false
126+
},
127+
&mnv.path
128+
)
129+
} else {
130+
fail!(expr_lit, "expected string literal for type_name")
131+
}
132+
} else {
133+
fail!(&mnv.value, "expected literal expression for type_name")
117134
}
118-
119-
Meta::NameValue(MetaNameValue {
120-
path,
121-
lit: Lit::Str(val),
122-
..
123-
}) if path.is_ident("type_name") => {
124-
try_set!(
125-
type_name,
126-
TypeName {
127-
val: val.value(),
128-
span: value.span(),
129-
deprecated_rename: false
130-
},
131-
value
132-
)
135+
}
136+
Meta::NameValue(mnv) if mnv.path.is_ident("rename") => {
137+
if let Expr::Lit(expr_lit) = &mnv.value {
138+
if let Lit::Str(val_str) = &expr_lit.lit {
139+
try_set!(
140+
type_name,
141+
TypeName {
142+
val: val_str.value(),
143+
span: val_str.span(),
144+
deprecated_rename: true
145+
},
146+
&mnv.path
147+
)
148+
} else {
149+
fail!(expr_lit, "expected string literal for rename")
150+
}
151+
} else {
152+
fail!(&mnv.value, "expected literal expression for rename")
133153
}
134-
135-
Meta::NameValue(MetaNameValue {
136-
path,
137-
lit: Lit::Str(val),
138-
..
139-
}) if path.is_ident("rename") => {
140-
try_set!(
141-
type_name,
142-
TypeName {
143-
val: val.value(),
144-
span: value.span(),
145-
deprecated_rename: true
146-
},
147-
value
148-
)
149-
}
150-
151-
u => fail!(u, "unexpected attribute"),
152-
},
153-
u => fail!(u, "unexpected attribute"),
154+
}
155+
u => fail!(u, "unexpected attribute inside sqlx(...)"),
154156
}
155157
}
156158
}
157159
Meta::List(list) if list.path.is_ident("repr") => {
158-
if list.nested.len() != 1 {
159-
fail!(&list.nested, "expected one value")
160+
let nested_metas = list.parse_args_with(Punctuated::<Meta, syn::token::Comma>::parse_terminated)?;
161+
if nested_metas.len() != 1 {
162+
fail!(&list.path, "expected one value for repr")
160163
}
161-
match list.nested.first().unwrap() {
162-
NestedMeta::Meta(Meta::Path(p)) if p.get_ident().is_some() => {
163-
try_set!(repr, p.get_ident().unwrap().clone(), list);
164+
match nested_metas.first().unwrap() {
165+
Meta::Path(p) if p.get_ident().is_some() => {
166+
try_set!(repr, p.get_ident().unwrap().clone(), &list.path);
164167
}
165-
u => fail!(u, "unexpected value"),
168+
u => fail!(u, "unexpected value for repr"),
166169
}
167170
}
168-
_ => {}
171+
_ => { /* Not an attribute we are interested in, or not a list */ }
169172
}
170173
}
171174

@@ -183,30 +186,36 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttri
183186
let mut try_from = None;
184187
let mut flatten = false;
185188

186-
for attr in input.iter().filter(|a| a.path.is_ident("sqlx")) {
187-
let meta = attr
188-
.parse_meta()
189-
.map_err(|e| syn::Error::new_spanned(attr, e))?;
190-
191-
if let Meta::List(list) = meta {
192-
for value in list.nested.iter() {
193-
match value {
194-
NestedMeta::Meta(meta) => match meta {
195-
Meta::NameValue(MetaNameValue {
196-
path,
197-
lit: Lit::Str(val),
198-
..
199-
}) if path.is_ident("rename") => try_set!(rename, val.value(), value),
200-
Meta::NameValue(MetaNameValue {
201-
path,
202-
lit: Lit::Str(val),
203-
..
204-
}) if path.is_ident("try_from") => try_set!(try_from, val.parse()?, value),
205-
Meta::Path(path) if path.is_ident("default") => default = true,
206-
Meta::Path(path) if path.is_ident("flatten") => flatten = true,
207-
u => fail!(u, "unexpected attribute"),
208-
},
209-
u => fail!(u, "unexpected attribute"),
189+
for attr in input.iter().filter(|a| a.path().is_ident("sqlx")) {
190+
if let Meta::List(list) = &attr.meta {
191+
let nested_metas = list.parse_args_with(Punctuated::<Meta, syn::token::Comma>::parse_terminated)?;
192+
for meta_item in nested_metas {
193+
match meta_item {
194+
Meta::NameValue(mnv) if mnv.path.is_ident("rename") => {
195+
if let Expr::Lit(expr_lit) = &mnv.value {
196+
if let Lit::Str(val_str) = &expr_lit.lit {
197+
try_set!(rename, val_str.value(), &mnv.path)
198+
} else {
199+
fail!(expr_lit, "expected string literal for rename")
200+
}
201+
} else {
202+
fail!(&mnv.value, "expected literal expression for rename")
203+
}
204+
}
205+
Meta::NameValue(mnv) if mnv.path.is_ident("try_from") => {
206+
if let Expr::Lit(expr_lit) = &mnv.value {
207+
if let Lit::Str(val_str) = &expr_lit.lit {
208+
try_set!(try_from, val_str.parse()?, &mnv.path)
209+
} else {
210+
fail!(expr_lit, "expected string literal for try_from")
211+
}
212+
} else {
213+
fail!(&mnv.value, "expected literal expression for try_from")
214+
}
215+
}
216+
Meta::Path(path) if path.is_ident("default") => default = true,
217+
Meta::Path(path) if path.is_ident("flatten") => flatten = true,
218+
u => fail!(u, "unexpected attribute inside sqlx(...)"),
210219
}
211220
}
212221
}

sqlx-macros/src/derives/encode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syn::punctuated::Punctuated;
99
use syn::token::Comma;
1010
use syn::{
1111
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Expr, Field, Fields, FieldsNamed,
12-
FieldsUnnamed, Lifetime, LifetimeDef, Stmt, Variant,
12+
FieldsUnnamed, Lifetime, LifetimeParam, Stmt, Variant,
1313
};
1414

1515
pub fn expand_derive_encode(input: &DeriveInput) -> syn::Result<TokenStream> {
@@ -66,7 +66,7 @@ fn expand_derive_encode_transparent(
6666
let mut generics = generics.clone();
6767
generics
6868
.params
69-
.insert(0, LifetimeDef::new(lifetime.clone()).into());
69+
.insert(0, LifetimeParam::new(lifetime.clone()).into());
7070

7171
generics
7272
.params

sqlx-macros/src/lib.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ use proc_macro::TokenStream;
1212

1313
use quote::quote;
1414

15+
use syn::parse::{Parse, ParseStream};
16+
use syn::{parse_macro_input, DeriveInput, ItemFn, LitStr, Token, Meta};
17+
use syn::punctuated::Punctuated;
18+
1519
type Error = Box<dyn std::error::Error>;
1620

1721
type Result<T> = std::result::Result<T, Error>;
@@ -27,6 +31,14 @@ mod test_attr;
2731
#[cfg(feature = "migrate")]
2832
mod migrate;
2933

34+
struct ArgsParser(Punctuated<Meta, Token![,]>);
35+
36+
impl Parse for ArgsParser {
37+
fn parse(input: ParseStream) -> syn::Result<Self> {
38+
Ok(ArgsParser(Punctuated::<Meta, Token![,]>::parse_terminated(input)?))
39+
}
40+
}
41+
3042
#[proc_macro]
3143
pub fn expand_query(input: TokenStream) -> TokenStream {
3244
let input = syn::parse_macro_input!(input as query::QueryMacroInput);
@@ -101,19 +113,12 @@ pub fn migrate(input: TokenStream) -> TokenStream {
101113
}
102114

103115
#[proc_macro_attribute]
104-
pub fn test(args: TokenStream, input: TokenStream) -> TokenStream {
105-
let args = syn::parse_macro_input!(args as syn::AttributeArgs);
106-
let input = syn::parse_macro_input!(input as syn::ItemFn);
107-
108-
match test_attr::expand(args, input) {
109-
Ok(ts) => ts.into(),
110-
Err(e) => {
111-
if let Some(parse_err) = e.downcast_ref::<syn::Error>() {
112-
parse_err.to_compile_error().into()
113-
} else {
114-
let msg = e.to_string();
115-
quote!(::std::compile_error!(#msg)).into()
116-
}
117-
}
118-
}
116+
pub fn test(
117+
args: proc_macro::TokenStream,
118+
input: proc_macro::TokenStream,
119+
) -> proc_macro::TokenStream {
120+
let args = parse_macro_input!(args as ArgsParser).0;
121+
let input = parse_macro_input!(input as ItemFn);
122+
123+
test_attr::expand(args, input)
119124
}

sqlx-macros/src/query/args.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use proc_macro2::TokenStream;
55
use quote::{format_ident, quote, quote_spanned};
66
use sqlx_core::describe::Describe;
77
use syn::spanned::Spanned;
8-
use syn::{Expr, ExprCast, ExprGroup, ExprType, Type};
8+
use syn::{Expr, ExprCast, ExprGroup, Type};
99

1010
/// Returns a tokenstream which typechecks the arguments passed to the macro
1111
/// and binds them to `DB::Arguments` with the ident `query_args`.
@@ -118,7 +118,6 @@ fn get_type_override(expr: &Expr) -> Option<&Type> {
118118
match expr {
119119
Expr::Group(group) => get_type_override(&group.expr),
120120
Expr::Cast(cast) => Some(&cast.ty),
121-
Expr::Type(ascription) => Some(&ascription.ty),
122121
_ => None,
123122
}
124123
}
@@ -135,7 +134,8 @@ fn strip_wildcard(expr: Expr) -> Expr {
135134
expr: Box::new(strip_wildcard(*expr)),
136135
}),
137136
// type ascription syntax is experimental so we always strip it
138-
Expr::Type(ExprType { expr, .. }) => *expr,
137+
// In syn v2, Expr::Type and ExprType are removed.
138+
// Expr::Type(ExprType { expr, .. }) => *expr,
139139
// we want to retain casts if they semantically matter
140140
Expr::Cast(ExprCast {
141141
attrs,

0 commit comments

Comments
 (0)