Skip to content

Commit 03c7556

Browse files
committed
fix attr-macro eating mutability modifiers
1 parent 115b190 commit 03c7556

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

proptest-macro/src/property_test/codegen/test_body.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ pub(super) fn body(
3030
// shorthand struct initialization.
3131

3232
match pat {
33-
Pat::Ident(_) => quote!(#field_name,),
33+
// We need to make sure to handle any mutability modifiers here, i.e. if the user wrote
34+
// `mut x: i32`, we have to generate `mut x`, not `x: mut x`
35+
//
36+
// See https://github.com/proptest-rs/proptest/issues/601
37+
Pat::Ident(i) => match i.mutability {
38+
Some(mutability) => quote!(#mutability #field_name,),
39+
None => quote!(#field_name,),
40+
},
3441
_ => quote!(#field_name: #pat,),
3542
}
3643
});

proptest-macro/src/property_test/utils.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use core::mem::replace;
2-
3-
use syn::{
4-
punctuated::Punctuated, AttrStyle, Attribute, Expr, FnArg, ItemFn, Meta,
5-
PatType,
6-
};
1+
use syn::{AttrStyle, Attribute, Expr, FnArg, ItemFn, Meta, PatType};
72

83
/// A parsed argument, with an optional custom strategy
94
pub struct Argument {
@@ -15,7 +10,7 @@ pub struct Argument {
1510
///
1611
/// Panics on any invalid function
1712
pub fn strip_args(mut f: ItemFn) -> (ItemFn, Vec<Argument>) {
18-
let args = replace(&mut f.sig.inputs, Punctuated::new());
13+
let args = std::mem::take(&mut f.sig.inputs);
1914
let args = args
2015
.into_iter()
2116
.map(|arg| match arg {

proptest/tests/attr_macro.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// Regression for https://github.com/proptest-rs/proptest/issues/601
2+
#[cfg(feature = "attr-macro")]
3+
#[proptest::property_test]
4+
fn attr_macro_does_not_clobber_mutability(mut x: i32, (mut y, _z): (i32, i32)) {
5+
let _suppress_unused_warning = x == y;
6+
7+
x = 0;
8+
y = 0;
9+
assert_eq!(x, y);
10+
}
11+

0 commit comments

Comments
 (0)