Skip to content

Commit

Permalink
Upgrade to syn 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lnicola committed Jun 30, 2023
1 parent d4a94fb commit 5258374
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 43 deletions.
2 changes: 1 addition & 1 deletion components/salsa-2022-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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>,
Expand Down
10 changes: 5 additions & 5 deletions components/salsa-2022-macros/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
///
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions components/salsa-2022-macros/src/interned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,)*
Expand Down
4 changes: 1 addition & 3 deletions components/salsa-2022-macros/src/jar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions components/salsa-2022-macros/src/salsa_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl<A: AllowedOptions> SalsaStruct<A> {
.struct_item
.attrs
.iter()
.filter(|attr| !attr.path.is_ident("derive"))
.filter(|attr| !attr.path().is_ident("derive"))
.collect();

parse_quote! {
Expand Down Expand Up @@ -426,7 +426,7 @@ impl SalsaField {
// 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);
}
}
Expand Down
35 changes: 20 additions & 15 deletions components/salsa-2022-macros/src/tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,21 @@ pub(crate) fn tracked_impl(
.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,
Expand Down Expand Up @@ -185,7 +184,7 @@ impl crate::options::AllowedOptions for TrackedImpl {
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> {
Expand Down Expand Up @@ -633,7 +632,7 @@ fn setter_fn(
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();
Expand All @@ -654,7 +653,7 @@ fn setter_fn(
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,
Expand Down Expand Up @@ -685,7 +684,7 @@ fn setter_fn(
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);
}
Expand All @@ -707,7 +706,7 @@ fn specify_fn(
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);
}
Expand All @@ -722,7 +721,7 @@ fn specify_fn(
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,
Expand All @@ -740,13 +739,19 @@ fn specify_fn(
/// 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),
};

Expand Down Expand Up @@ -783,7 +788,7 @@ fn db_lifetime_and_ty(func: &mut syn::Signature) -> syn::Result<(syn::Lifetime,
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,
Expand Down
4 changes: 2 additions & 2 deletions components/salsa-2022-macros/src/tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/salsa-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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 })
}
}
Expand Down
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 })
}
Expand Down

0 comments on commit 5258374

Please sign in to comment.