Merge pull request #1696 from scalexm/unwrap

Unwrap `syn::*::Group` in a few places
This commit is contained in:
David Hewitt 2021-06-29 22:12:49 +01:00 committed by GitHub
commit 542a6124b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 9 deletions

View File

@ -4,6 +4,7 @@ use crate::{
attributes::FromPyWithAttribute,
method::{FnArg, FnSpec},
pyfunction::Argument,
utils::unwrap_ty_group,
};
use proc_macro2::{Span, TokenStream};
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 (target_ty, borrow_tmp) = if arg.optional.is_some() {
// 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`.
pub fn is_python(mut ty: &syn::Type) -> bool {
while let syn::Type::Group(group) = ty {
// Macros can create invisible delimiters around types.
ty = &*group.elem;
}
match ty {
pub fn is_python(ty: &syn::Type) -> bool {
match unwrap_ty_group(ty) {
syn::Type::Path(typath) => typath
.path
.segments
@ -124,3 +120,10 @@ pub fn unwrap_group(mut expr: &syn::Expr) -> &syn::Expr {
}
expr
}
pub fn unwrap_ty_group(mut ty: &syn::Type) -> &syn::Type {
while let syn::Type::Group(g) = ty {
ty = &*g.elem;
}
ty
}

View File

@ -733,7 +733,7 @@ fn test_raw_idents() {
#[pyclass]
struct Issue1505 {}
macro_rules! issue_1505 {
macro_rules! pymethods {
(
#[pymethods]
impl $ty: ty {
@ -747,7 +747,7 @@ macro_rules! issue_1505 {
};
}
issue_1505!(
pymethods!(
#[pymethods]
impl Issue1505 {
fn issue_1505(&self, _py: Python<'_>) {}
@ -819,3 +819,13 @@ issue_1506!(
}
}
);
#[pyclass]
struct Issue1696 {}
pymethods!(
#[pymethods]
impl Issue1696 {
fn issue_1696(&self, _x: &InstanceMethod) {}
}
);