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

Need to be able to create structs via macro_rules #832

Merged
merged 4 commits into from
Jun 24, 2021

Conversation

gilescope
Copy link
Contributor

(Currently only possible using tt escape type which doesn't do any processing.)

There's quite a few places I think we need to update but using pyclass as an example to agree the right way to do things. When you pass the argument via macro_rules unless it's the escape sequence type of tt then the Expr::Path seems to get wrapped in an Expr::Group whatever type I choose for the variable I'm passing in: ident, path, expr.

(btw I notice you can't match on box which might make the code a tad nicer.)

@gilescope
Copy link
Contributor Author

(The test fails to compile without the patch which is why there's no assertions in it)

pyo3-derive-backend/src/pyclass.rs Outdated Show resolved Hide resolved
tests/test_macros.rs Outdated Show resolved Hide resolved
@kngwyu
Copy link
Member

kngwyu commented Mar 24, 2020

Thanks 👍

@programmerjake
Copy link
Contributor

Encountered this issue myself.

One suggestion: maybe there should be an unwrap_group function that unwraps a group or returns the input unchanged:

// pseudo-code, probably needs some derefs and other misc. cleanup
fn unwrap_group(expr: &syn::Expr) -> &syn::Expr {
    if let syn::Expr::Group(g) = expr {
        &*g.expr
    }
    expr
}

that way, the match cases can be simplified:

            "name" => match unwrap_group(&**right) {
                syn::Expr::Path(exp) if exp.path.segments.len() == 1 => {
                    self.name = Some(exp.clone().into());
                }
                _ => expected!("type name (e.g., Name)"),
            },

Base automatically changed from master to main March 16, 2021 22:09
@davidhewitt davidhewitt mentioned this pull request Apr 19, 2021
7 tasks
@scalexm
Copy link
Contributor

scalexm commented Apr 22, 2021

Btw, I would suggest a while let Group(g) = expr approach rather than unwrapping a single layer. Not sure if that could actually happen but it’s harmless.

@davidhewitt
Copy link
Member

👍 also my parsing improvements in #1567 might resolve a lot of the problems here. I plan to retest this once that PR is merged.

Giles Cope and others added 3 commits June 24, 2021 08:40
@davidhewitt
Copy link
Member

As this branch is very old, I just rebased it and will merge it if tests are green 🤞

@scalexm
Copy link
Contributor

scalexm commented Jun 24, 2021

More of these problems started to appear with Rust 1.53 (at first I thought it was a compiler regression, but actually it’s just the compiler adding a few more groups and pyo3 not handling them correctly).

I think we should use unwrap_group quite liberally: I have found a few more places where we need it. Once this is merged, I’ll open a follow-up PR.

@messense messense changed the title WIP: Need to be able to create structs via macro_rules Need to be able to create structs via macro_rules Jun 24, 2021
@messense messense merged commit 3bf283d into PyO3:main Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants