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

Upgrade to syn 2.0 #442

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/salsa-2022-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -10,5 +10,5 @@ proc-macro = true
heck = "0.4"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ["full", "extra-traits", "visit-mut"] }
syn = { version = "2.0", features = ["full", "extra-traits", "visit-mut"] }
eyre = "0.6.5"
10 changes: 5 additions & 5 deletions components/salsa-2022-macros/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -4,9 +4,9 @@ pub(crate) struct Configuration {
pub(crate) key_ty: syn::Type,
pub(crate) value_ty: syn::Type,
pub(crate) cycle_strategy: CycleRecoveryStrategy,
pub(crate) backdate_fn: syn::ImplItemMethod,
pub(crate) execute_fn: syn::ImplItemMethod,
pub(crate) recover_fn: syn::ImplItemMethod,
pub(crate) backdate_fn: syn::ImplItemFn,
pub(crate) execute_fn: syn::ImplItemFn,
pub(crate) recover_fn: syn::ImplItemFn,
}

impl Configuration {
@@ -56,7 +56,7 @@ impl quote::ToTokens for CycleRecoveryStrategy {

/// Returns an appropriate definition for `should_backdate_value` depending on
/// whether this value is memoized or not.
pub(crate) fn should_backdate_value_fn(should_backdate: bool) -> syn::ImplItemMethod {
pub(crate) fn should_backdate_value_fn(should_backdate: bool) -> syn::ImplItemFn {
if should_backdate {
parse_quote! {
fn should_backdate_value(v1: &Self::Value, v2: &Self::Value) -> bool {
@@ -74,7 +74,7 @@ pub(crate) fn should_backdate_value_fn(should_backdate: bool) -> syn::ImplItemMe

/// Returns an appropriate definition for `recover_from_cycle` for cases where
/// the cycle recovery is panic.
pub(crate) fn panic_cycle_recovery_fn() -> syn::ImplItemMethod {
pub(crate) fn panic_cycle_recovery_fn() -> syn::ImplItemFn {
parse_quote! {
fn recover_from_cycle(
_db: &salsa::function::DynDb<Self>,
10 changes: 5 additions & 5 deletions components/salsa-2022-macros/src/input.rs
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ impl InputStruct {
let field_tys: Vec<_> = self.all_field_tys();
let field_clones: Vec<_> = self.all_fields().map(SalsaField::is_clone_field).collect();
let get_field_names: Vec<_> = self.all_get_field_names();
let field_getters: Vec<syn::ImplItemMethod> = field_indices.iter().zip(&get_field_names).zip(&field_vises).zip(&field_tys).zip(&field_clones).map(|((((field_index, get_field_name), field_vis), field_ty), is_clone_field)|
let field_getters: Vec<syn::ImplItemFn> = field_indices.iter().zip(&get_field_names).zip(&field_vises).zip(&field_tys).zip(&field_clones).map(|((((field_index, get_field_name), field_vis), field_ty), is_clone_field)|
if !*is_clone_field {
parse_quote! {
#field_vis fn #get_field_name<'db>(self, __db: &'db #db_dyn_ty) -> &'db #field_ty
@@ -106,7 +106,7 @@ impl InputStruct {

// setters
let set_field_names = self.all_set_field_names();
let field_setters: Vec<syn::ImplItemMethod> = field_indices.iter()
let field_setters: Vec<syn::ImplItemFn> = field_indices.iter()
.zip(&set_field_names)
.zip(&field_vises)
.zip(&field_tys)
@@ -126,7 +126,7 @@ impl InputStruct {
let constructor_name = self.constructor_name();
let singleton = self.0.is_isingleton();

let constructor: syn::ImplItemMethod = if singleton {
let constructor: syn::ImplItemFn = if singleton {
parse_quote! {
/// Creates a new singleton input
///
@@ -160,7 +160,7 @@ impl InputStruct {
};

if singleton {
let get: syn::ImplItemMethod = parse_quote! {
let get: syn::ImplItemFn = parse_quote! {
#[track_caller]
pub fn get(__db: &#db_dyn_ty) -> Self {
let (__jar, __runtime) = <_ as salsa::storage::HasJar<#jar_ty>>::jar(__db);
@@ -169,7 +169,7 @@ impl InputStruct {
}
};

let try_get: syn::ImplItemMethod = parse_quote! {
let try_get: syn::ImplItemFn = parse_quote! {
#[track_caller]
pub fn try_get(__db: &#db_dyn_ty) -> Option<Self> {
let (__jar, __runtime) = <_ as salsa::storage::HasJar<#jar_ty>>::jar(__db);
4 changes: 2 additions & 2 deletions components/salsa-2022-macros/src/interned.rs
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ impl InternedStruct {
let db_dyn_ty = self.db_dyn_ty();
let jar_ty = self.jar_ty();

let field_getters: Vec<syn::ImplItemMethod> = self
let field_getters: Vec<syn::ImplItemFn> = self
.all_fields()
.map(|field| {
let field_name = field.name();
@@ -119,7 +119,7 @@ impl InternedStruct {
let field_tys = self.all_field_tys();
let data_ident = self.data_ident();
let constructor_name = self.constructor_name();
let new_method: syn::ImplItemMethod = parse_quote! {
let new_method: syn::ImplItemFn = parse_quote! {
#vis fn #constructor_name(
db: &#db_dyn_ty,
#(#field_names: #field_tys,)*
4 changes: 1 addition & 3 deletions components/salsa-2022-macros/src/jar.rs
Original file line number Diff line number Diff line change
@@ -158,9 +158,7 @@ fn generate_fields(input: &ItemStruct) -> FieldsUnnamed {
span: f.brace_token.span,
},
syn::Fields::Unnamed(f) => f.paren_token,
syn::Fields::Unit => syn::token::Paren {
span: input.ident.span(),
},
syn::Fields::Unit => syn::token::Paren(input.ident.span()),
};

FieldsUnnamed {
4 changes: 2 additions & 2 deletions components/salsa-2022-macros/src/salsa_struct.rs
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@
.struct_item
.attrs
.iter()
.filter(|attr| !attr.path.is_ident("derive"))
.filter(|attr| !attr.path().is_ident("derive"))
.collect();

parse_quote! {
@@ -305,7 +305,7 @@
let ident_string = ident.to_string();

// `::salsa::debug::helper::SalsaDebug` will use `DebugWithDb` or fallbak to `Debug`
let fields = self

Check warning on line 308 in components/salsa-2022-macros/src/salsa_struct.rs

GitHub Actions / Test (nightly, true)

useless conversion to the same type: `impl std::iter::Iterator<Item = &salsa_struct::SalsaField>`

Check warning on line 308 in components/salsa-2022-macros/src/salsa_struct.rs

GitHub Actions / Test (stable, false)

useless conversion to the same type: `impl std::iter::Iterator<Item = &salsa_struct::SalsaField>`

Check warning on line 308 in components/salsa-2022-macros/src/salsa_struct.rs

GitHub Actions / Test (beta, false)

useless conversion to the same type: `impl std::iter::Iterator<Item = &salsa_struct::SalsaField>`
.all_fields()
.into_iter()
.map(|field| -> TokenStream {
@@ -405,7 +405,7 @@
if BANNED_FIELD_NAMES.iter().any(|n| *n == field_name_str) {
return Err(syn::Error::new(
field_name.span(),
&format!(

Check warning on line 408 in components/salsa-2022-macros/src/salsa_struct.rs

GitHub Actions / Test (nightly, true)

the borrowed expression implements the required traits

Check warning on line 408 in components/salsa-2022-macros/src/salsa_struct.rs

GitHub Actions / Test (stable, false)

the borrowed expression implements the required traits

Check warning on line 408 in components/salsa-2022-macros/src/salsa_struct.rs

GitHub Actions / Test (beta, false)

the borrowed expression implements the required traits
"the field name `{}` is disallowed in salsa structs",
field_name_str
),
@@ -426,7 +426,7 @@
// Scan the attributes and look for the salsa attributes:
for attr in &field.attrs {
for (fa, func) in FIELD_OPTION_ATTRIBUTES {
if attr.path.is_ident(fa) {
if attr.path().is_ident(fa) {
func(attr, &mut result);
}
}
35 changes: 20 additions & 15 deletions components/salsa-2022-macros/src/tracked_fn.rs
Original file line number Diff line number Diff line change
@@ -110,22 +110,21 @@
.iter_mut()
.filter_map(|item| {
let item_method = match item {
syn::ImplItem::Method(item_method) => item_method,
syn::ImplItem::Fn(item_method) => item_method,
_ => return None,
};
let salsa_tracked_attr = item_method.attrs.iter().position(|attr| {
let path = &attr.path.segments;
let path = &attr.path().segments;
path.len() == 2
&& path[0].arguments == syn::PathArguments::None
&& path[0].ident == "salsa"
&& path[1].arguments == syn::PathArguments::None
&& path[1].ident == "tracked"
})?;
let salsa_tracked_attr = item_method.attrs.remove(salsa_tracked_attr);
let inner_args = if !salsa_tracked_attr.tokens.is_empty() {
salsa_tracked_attr.parse_args()
} else {
Ok(FnArgs::default())
let inner_args = match salsa_tracked_attr.meta {
syn::Meta::Path(_) => Ok(FnArgs::default()),
syn::Meta::List(_) | syn::Meta::NameValue(_) => salsa_tracked_attr.parse_args(),
};
let inner_args = match inner_args {
Ok(inner_args) => inner_args,
@@ -141,7 +140,7 @@
))
})
// Collate all the errors so we can display them all at once
.fold(Ok(Vec::new()), |mut acc, res| {

Check warning on line 143 in components/salsa-2022-macros/src/tracked_fn.rs

GitHub Actions / Test (nightly, true)

usage of `Iterator::fold` on a type that implements `Try`

Check warning on line 143 in components/salsa-2022-macros/src/tracked_fn.rs

GitHub Actions / Test (stable, false)

usage of `Iterator::fold` on a type that implements `Try`

Check warning on line 143 in components/salsa-2022-macros/src/tracked_fn.rs

GitHub Actions / Test (beta, false)

usage of `Iterator::fold` on a type that implements `Try`
match (&mut acc, res) {
(Ok(extra_impls), Ok(impls)) => extra_impls.push(impls),
(Ok(_), Err(err)) => acc = Err(err),
@@ -185,7 +184,7 @@
fn tracked_method(
outer_args: &ImplArgs,
mut args: FnArgs,
item_method: &mut syn::ImplItemMethod,
item_method: &mut syn::ImplItemFn,
self_type: &syn::TypePath,
name: &str,
) -> syn::Result<TokenStream> {
@@ -633,7 +632,7 @@
args: &FnArgs,
item_fn: &syn::ItemFn,
config_ty: &syn::Type,
) -> syn::Result<syn::ImplItemMethod> {
) -> syn::Result<syn::ImplItemFn> {
// The setter has *always* the same signature as the original:
// but it takes a value arg and has no return type.
let jar_ty = args.jar_ty();
@@ -654,7 +653,7 @@
let value_arg = syn::Ident::new("__value", item_fn.sig.output.span());
setter_sig.inputs.push(parse_quote!(#value_arg: #value_ty));
setter_sig.output = ReturnType::Default;
Ok(syn::ImplItemMethod {
Ok(syn::ImplItemFn {
attrs: vec![],
vis: item_fn.vis.clone(),
defaultness: None,
@@ -685,7 +684,7 @@
fn set_lru_capacity_fn(
args: &FnArgs,
config_ty: &syn::Type,
) -> syn::Result<Option<syn::ImplItemMethod>> {
) -> syn::Result<Option<syn::ImplItemFn>> {
if args.lru.is_none() {
return Ok(None);
}
@@ -707,7 +706,7 @@
args: &FnArgs,
item_fn: &syn::ItemFn,
config_ty: &syn::Type,
) -> syn::Result<Option<syn::ImplItemMethod>> {
) -> syn::Result<Option<syn::ImplItemFn>> {
if args.specify.is_none() {
return Ok(None);
}
@@ -722,7 +721,7 @@
let value_arg = syn::Ident::new("__value", item_fn.sig.output.span());
setter_sig.inputs.push(parse_quote!(#value_arg: #value_ty));
setter_sig.output = ReturnType::Default;
Ok(Some(syn::ImplItemMethod {
Ok(Some(syn::ImplItemFn {
attrs: vec![],
vis: item_fn.vis.clone(),
defaultness: None,
@@ -740,13 +739,19 @@
/// Given a function def tagged with `#[return_ref]`, modifies `fn_sig` so that
/// it returns an `&Value` instead of `Value`. May introduce a name for the
/// database lifetime if required.
fn make_fn_return_ref(mut fn_sig: &mut syn::Signature) -> syn::Result<()> {
fn make_fn_return_ref(fn_sig: &mut syn::Signature) -> syn::Result<()> {
// An input should be a `&dyn Db`.
// We need to ensure it has a named lifetime parameter.
let (db_lifetime, _) = db_lifetime_and_ty(fn_sig)?;

let (right_arrow, elem) = match fn_sig.output.clone() {
ReturnType::Default => (syn::Token![->](fn_sig.paren_token.span), parse_quote!(())),
ReturnType::Default => (
syn::Token![->]([
fn_sig.paren_token.span.open(),
fn_sig.paren_token.span.close(),
]),
parse_quote!(()),
),
ReturnType::Type(rarrow, ty) => (rarrow, ty),
};

@@ -783,7 +788,7 @@
let ident = syn::Ident::new("__db", and_token_span);
func.generics.params.insert(
0,
syn::LifetimeDef {
syn::LifetimeParam {
attrs: vec![],
lifetime: syn::Lifetime {
apostrophe: and_token_span,
4 changes: 2 additions & 2 deletions components/salsa-2022-macros/src/tracked_struct.rs
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ impl TrackedStruct {
let id_field_tys: Vec<_> = self.id_fields().map(SalsaField::ty).collect();
let id_field_vises: Vec<_> = self.id_fields().map(SalsaField::vis).collect();
let id_field_clones: Vec<_> = self.id_fields().map(SalsaField::is_clone_field).collect();
let id_field_getters: Vec<syn::ImplItemMethod> = id_field_indices.iter().zip(&id_field_get_names).zip(&id_field_tys).zip(&id_field_vises).zip(&id_field_clones).map(|((((field_index, field_get_name), field_ty), field_vis), is_clone_field)|
let id_field_getters: Vec<syn::ImplItemFn> = id_field_indices.iter().zip(&id_field_get_names).zip(&id_field_tys).zip(&id_field_vises).zip(&id_field_clones).map(|((((field_index, field_get_name), field_ty), field_vis), is_clone_field)|
if !*is_clone_field {
parse_quote! {
#field_vis fn #field_get_name<'db>(self, __db: &'db #db_dyn_ty) -> &'db #field_ty
@@ -123,7 +123,7 @@ impl TrackedStruct {
.value_fields()
.map(SalsaField::is_clone_field)
.collect();
let value_field_getters: Vec<syn::ImplItemMethod> = value_field_indices.iter().zip(&value_field_get_names).zip(&value_field_tys).zip(&value_field_vises).zip(&value_field_clones).map(|((((field_index, field_get_name), field_ty), field_vis), is_clone_field)|
let value_field_getters: Vec<syn::ImplItemFn> = value_field_indices.iter().zip(&value_field_get_names).zip(&value_field_tys).zip(&value_field_vises).zip(&value_field_clones).map(|((((field_index, field_get_name), field_ty), field_vis), is_clone_field)|
if !*is_clone_field {
parse_quote! {
#field_vis fn #field_get_name<'db>(self, __db: &'db #db_dyn_ty) -> &'db #field_ty
2 changes: 1 addition & 1 deletion components/salsa-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -14,4 +14,4 @@ proc-macro = true
heck = "0.4"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ["full", "extra-traits"] }
syn = { version = "2.0", features = ["full", "extra-traits"] }
3 changes: 2 additions & 1 deletion components/salsa-macros/src/database_storage.rs
Original file line number Diff line number Diff line change
@@ -220,7 +220,8 @@ struct QueryGroupList {

impl Parse for QueryGroupList {
fn parse(input: ParseStream) -> syn::Result<Self> {
let query_groups: PunctuatedQueryGroups = input.parse_terminated(QueryGroup::parse)?;
let query_groups: PunctuatedQueryGroups =
input.parse_terminated(QueryGroup::parse, Token![,])?;
Ok(QueryGroupList { query_groups })
}
}
23 changes: 17 additions & 6 deletions components/salsa-macros/src/query_group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::convert::TryFrom;
use std::{convert::TryFrom, iter::FromIterator};

use crate::parenthesized::Parenthesized;
use heck::ToUpperCamelCase;
@@ -36,7 +36,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
// Decompose the trait into the corresponding queries.
let mut queries = vec![];
for item in input.items {
if let TraitItem::Method(method) = item {
if let TraitItem::Fn(method) = item {
let query_name = method.sig.ident.to_string();

let mut storage = QueryStorage::Memoized;
@@ -277,7 +277,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
specific durability instead of the default of
`Durability::LOW`. You can use `Durability::MAX`
to promise that its value will never change again.
See `{fn_name}` for details.
*Note:* Setting values will trigger cancellation
@@ -681,13 +681,24 @@ impl TryFrom<syn::Attribute> for SalsaAttr {
type Error = syn::Attribute;

fn try_from(attr: syn::Attribute) -> Result<SalsaAttr, syn::Attribute> {
if is_not_salsa_attr_path(&attr.path) {
if is_not_salsa_attr_path(attr.path()) {
return Err(attr);
}

let span = attr.span();
let name = attr.path.segments[1].ident.to_string();
let tts = attr.tokens.into();
let name = attr.path().segments[1].ident.to_string();
let tts = match attr.meta {
syn::Meta::Path(path) => path.into_token_stream(),
syn::Meta::List(ref list) => {
let tts = list
.into_token_stream()
.into_iter()
.skip(attr.path().to_token_stream().into_iter().count());
proc_macro2::TokenStream::from_iter(tts)
}
syn::Meta::NameValue(nv) => nv.into_token_stream(),
}
.into();

Ok(SalsaAttr { name, tts, span })
}
Loading