Unwrap `syn::*::Group` in a few places

This commit is contained in:
scalexm 2021-06-25 00:12:38 +02:00
parent e8b9a991d3
commit 20fa398fdb
2 changed files with 11 additions and 7 deletions

View File

@ -4,6 +4,7 @@ use crate::{
attributes::FromPyWithAttribute, attributes::FromPyWithAttribute,
method::{FnArg, FnSpec}, method::{FnArg, FnSpec},
pyfunction::Argument, pyfunction::Argument,
utils::unwrap_ty_group,
}; };
use proc_macro2::{Span, TokenStream}; use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned}; use quote::{quote, quote_spanned};
@ -258,7 +259,7 @@ fn impl_arg_param(
} }
}; };
return if let syn::Type::Reference(tref) = arg.optional.as_ref().unwrap_or(&ty) { return if let syn::Type::Reference(tref) = unwrap_ty_group(arg.optional.unwrap_or(&ty)) {
let (tref, mut_) = preprocess_tref(tref, self_); let (tref, mut_) = preprocess_tref(tref, self_);
let (target_ty, borrow_tmp) = if arg.optional.is_some() { let (target_ty, borrow_tmp) = if arg.optional.is_some() {
// Get Option<&T> from Option<PyRef<T>> // Get Option<&T> from Option<PyRef<T>>

View File

@ -29,12 +29,8 @@ macro_rules! ensure_spanned {
} }
/// Check if the given type `ty` is `pyo3::Python`. /// Check if the given type `ty` is `pyo3::Python`.
pub fn is_python(mut ty: &syn::Type) -> bool { pub fn is_python(ty: &syn::Type) -> bool {
while let syn::Type::Group(group) = ty { match unwrap_ty_group(ty) {
// Macros can create invisible delimiters around types.
ty = &*group.elem;
}
match ty {
syn::Type::Path(typath) => typath syn::Type::Path(typath) => typath
.path .path
.segments .segments
@ -124,3 +120,10 @@ pub fn unwrap_group(mut expr: &syn::Expr) -> &syn::Expr {
} }
expr expr
} }
pub fn unwrap_ty_group(mut ty: &syn::Type) -> &syn::Type {
while let syn::Type::Group(g) = ty {
ty = &*g.elem;
}
ty
}