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

Properly handle mutability for awaited futures #239

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 33 additions & 1 deletion rstest_macros/src/refident.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Provide `RefIdent` and `MaybeIdent` traits that give a shortcut to extract identity reference
/// (`syn::Ident` struct).
use proc_macro2::Ident;
use syn::{FnArg, Pat, PatType, Type};
use syn::{FnArg, Pat, PatType, Token, Type};

pub trait RefIdent {
/// Return the reference to ident if any
Expand Down Expand Up @@ -86,3 +86,35 @@ impl MaybeIdent for crate::parse::Attribute {
}
}
}

pub trait MaybeMutability {
fn maybe_mutability(&self) -> Option<Token![mut]>;
borchero marked this conversation as resolved.
Show resolved Hide resolved
}

impl MaybeMutability for FnArg {
fn maybe_mutability(&self) -> Option<Token![mut]> {
match self {
FnArg::Typed(PatType { pat, .. }) => match pat.as_ref() {
Pat::Ident(ident) => ident.mutability,
_ => None,
},
_ => None,
}
}
}

pub trait SetMutability {
fn set_mutability(&mut self, mutability: Option<Token![mut]>);
}

impl SetMutability for FnArg {
fn set_mutability(&mut self, mutability: Option<Token![mut]>) {
match self {
FnArg::Typed(PatType { pat, .. }) => match pat.as_mut() {
Pat::Ident(ident) => ident.mutability = mutability,
_ => {}
},
_ => {}
};
}
}
11 changes: 6 additions & 5 deletions rstest_macros/src/render/apply_argumets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use syn::{parse_quote, FnArg, Generics, Ident, ItemFn, Lifetime, Signature, Type

use crate::{
parse::{arguments::ArgumentsInfo, future::MaybeFutureImplType},
refident::MaybeIdent,
refident::{MaybeIdent, MaybeMutability, SetMutability},
};

pub(crate) trait ApplyArgumets<R: Sized = ()> {
Expand Down Expand Up @@ -63,13 +63,13 @@ impl ApplyArgumets for ItemFn {
.sig
.inputs
.iter()
.filter_map(|a| a.maybe_ident())
.filter(|&a| arguments.is_future_await(a))
.cloned();
.filter_map(|a| a.maybe_ident().map(|i| (i, a.maybe_mutability())))
.filter(|(a, _)| arguments.is_future_await(a))
.map(|(a, m)| quote::quote! { let #m #a = #a.await; });
let orig_block_impl = self.block.clone();
self.block = parse_quote! {
{
#(let #awaited_args = #awaited_args.await;)*
#(#awaited_args)*
#orig_block_impl
borchero marked this conversation as resolved.
Show resolved Hide resolved
}
};
Expand All @@ -90,6 +90,7 @@ impl ImplFutureArg for FnArg {
*ty = parse_quote! {
impl std::future::Future<Output = #ty>
};
self.set_mutability(None);
lifetime
}
None => None,
Expand Down
Loading