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

Deriving pyclass/pymethods seems to break when inside of a macro-by-example. #4381

Closed
tritoke opened this issue Jul 27, 2024 · 2 comments · Fixed by #4382
Closed

Deriving pyclass/pymethods seems to break when inside of a macro-by-example. #4381

tritoke opened this issue Jul 27, 2024 · 2 comments · Fixed by #4382
Labels

Comments

@tritoke
Copy link

tritoke commented Jul 27, 2024

Bug Description

When placed inside of a macro code that compiles outside of the macro no longer compiles. The bug seems to be specifically around returning an instance of another pyclass from a method on another.

Steps to Reproduce

maturin new repro --bindings pyo3
cd repro
cargo add sha3
cat >src/lib.rs <<'EOF'
∙ use pyo3::prelude::*;

macro_rules! impl_repro {
    ($name_a:ident, $name_b:ident) => {
        #[pyclass]
        struct $name_a {}

        #[pymethods]
        impl $name_a {
            fn finalize(&mut self) -> $name_b {
                $name_b {}
            }
        }

        #[pyclass]
        struct $name_b {}
    };
}

impl_repro!(MyClass1, MyClass2);
EOF
maturin develop --release

Backtrace

error[E0425]: cannot find value `py` in this scope
  --> src/lib.rs:20:23
   |
20 | impl_repro!(MyClass1, MyClass2);
   |                       ^^^^^^^^ not found in this scope

error[E0425]: cannot find value `ret` in this scope
  --> src/lib.rs:20:23
   |
20 | impl_repro!(MyClass1, MyClass2);
   |                       ^^^^^^^^ not found in this scope


### Your operating system and version

Arch Linux (kernel version 6.9.9)

### Your Python version (`python --version`)

Python 3.12.4

### Your Rust version (`rustc --version`)

 rustc 1.80.0 (051478957 2024-07-21)

### Your PyO3 version

0.20.0

### How did you install python? Did you use a virtualenv?

python is installed via pacman
maturin is installed in a venv:
python -m venv .env
source .env/bin/activate
pip install maturin

### Additional Info

The original context for this is effectively attempting to get around the no-generics for pyclasses by templating out an implementation for a set of XOF functions where each hasher has a "Shaker" which absorbs data in and a "Sponge" which produces data, and you must finalize a Shaker to get a Sponge so I want to have finalize reset the state of the Shaker and return a Sponge object.
@tritoke tritoke added the bug label Jul 27, 2024
@Icxolu
Copy link
Contributor

Icxolu commented Jul 27, 2024

I can't reproduce that on 0.20 (or 0.21), but I do see it on 0.22. (Did you perhaps mistype the version?)

The 0.22 case that I could reproduce is a tricky one 😅. It seems like this was introduced by #4220, which improved some error messages by changing the Spans of our generated code. Together with macro_rules hygiene these now seem to get different syntax contexts attached, which makes them different from the compilers perspective in this specific context. PR fixing will follow shortly.

@tritoke
Copy link
Author

tritoke commented Jul 27, 2024

Ah yeah i did it was 0.22, I will use 0.21 for now, thanks for finding the bug :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants