Skip to content

Commit d803f7f

Browse files
authored
store the FnArg ident as a Cow instead of a reference (#4157)
This allow also storing idents that were generated as part of the macro instead of only ones the were present in the source code. This is needed for example in complex enum tuple variants.
1 parent 72be1cd commit d803f7f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

pyo3-macros-backend/src/method.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::fmt::Display;
23

34
use proc_macro2::{Span, TokenStream};
@@ -18,7 +19,7 @@ use crate::{
1819

1920
#[derive(Clone, Debug)]
2021
pub struct RegularArg<'a> {
21-
pub name: &'a syn::Ident,
22+
pub name: Cow<'a, syn::Ident>,
2223
pub ty: &'a syn::Type,
2324
pub from_py_with: Option<FromPyWithAttribute>,
2425
pub default_value: Option<syn::Expr>,
@@ -28,14 +29,14 @@ pub struct RegularArg<'a> {
2829
/// Pythons *args argument
2930
#[derive(Clone, Debug)]
3031
pub struct VarargsArg<'a> {
31-
pub name: &'a syn::Ident,
32+
pub name: Cow<'a, syn::Ident>,
3233
pub ty: &'a syn::Type,
3334
}
3435

3536
/// Pythons **kwarg argument
3637
#[derive(Clone, Debug)]
3738
pub struct KwargsArg<'a> {
38-
pub name: &'a syn::Ident,
39+
pub name: Cow<'a, syn::Ident>,
3940
pub ty: &'a syn::Type,
4041
}
4142

@@ -61,7 +62,7 @@ pub enum FnArg<'a> {
6162
}
6263

6364
impl<'a> FnArg<'a> {
64-
pub fn name(&self) -> &'a syn::Ident {
65+
pub fn name(&self) -> &syn::Ident {
6566
match self {
6667
FnArg::Regular(RegularArg { name, .. }) => name,
6768
FnArg::VarArgs(VarargsArg { name, .. }) => name,
@@ -98,7 +99,10 @@ impl<'a> FnArg<'a> {
9899
..
99100
}) = self
100101
{
101-
*self = Self::VarArgs(VarargsArg { name, ty });
102+
*self = Self::VarArgs(VarargsArg {
103+
name: name.clone(),
104+
ty,
105+
});
102106
Ok(self)
103107
} else {
104108
bail_spanned!(self.name().span() => "args cannot be optional")
@@ -113,7 +117,10 @@ impl<'a> FnArg<'a> {
113117
..
114118
}) = self
115119
{
116-
*self = Self::KwArgs(KwargsArg { name, ty });
120+
*self = Self::KwArgs(KwargsArg {
121+
name: name.clone(),
122+
ty,
123+
});
117124
Ok(self)
118125
} else {
119126
bail_spanned!(self.name().span() => "kwargs must be Option<_>")
@@ -159,7 +166,7 @@ impl<'a> FnArg<'a> {
159166
}
160167

161168
Ok(Self::Regular(RegularArg {
162-
name: ident,
169+
name: Cow::Borrowed(ident),
163170
ty: &cap.ty,
164171
from_py_with,
165172
default_value: None,

pyo3-macros-backend/src/pyclass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ fn complex_enum_struct_variant_new<'a>(
11531153

11541154
for field in &variant.fields {
11551155
args.push(FnArg::Regular(RegularArg {
1156-
name: field.ident,
1156+
name: Cow::Borrowed(field.ident),
11571157
ty: field.ty,
11581158
from_py_with: None,
11591159
default_value: None,

0 commit comments

Comments
 (0)