Skip to content

Recursive enum type does not know its name (minor) #5420

@ChristopherRabotin

Description

@ChristopherRabotin

Bug Description

Recursive enum types were introduced in #4694 (thanks!). They work when explicitly referencing the enum's name, but when it's referred as Self PyO3 cannot recognize it.

For example, the works fine:

#[pyclass]
enum TestRecursiveEnum {
    WithRec { my_field: Py<TestRecursiveEnum> },
}

But this leads to a compilation error:

#[pyclass]
enum TestRecursiveEnum {
    WithRec { my_field: Py<Self> },
}
error[E0308]: mismatched types
  --> anise-py/src/astro.rs:37:15
   |
37 |     WithRec { my_field: Py<Self> },
   |               ^^^^^^^^ expected `Py<TestRecursiveEnum>`, found `Py<TestRecursiveEnum_WithRec>`
   |
   = note: expected struct `pyo3::Py<TestRecursiveEnum>`
              found struct `pyo3::Py<TestRecursiveEnum_WithRec>`

error[E0308]: mismatched types
    --> anise-py/src/astro.rs:37:15
     |
35   | #[pyclass]
     | ---------- arguments to this function are incorrect
36   | enum TestRecursiveEnum {
37   |     WithRec { my_field: Py<Self> },
     |               ^^^^^^^^ expected `&Py<TestRecursiveEnum_WithRec>`, found `&Py<TestRecursiveEnum>`
     |
     = note: expected reference `&pyo3::Py<TestRecursiveEnum_WithRec>`
                found reference `&pyo3::Py<TestRecursiveEnum>`
note: associated function defined here
    --> /home/chris/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.25.1/src/impl_/pyclass.rs:1440:12
     |
1440 |     pub fn convert_field<'a, 'py, T>(obj: &'a T, py: Python<'py>) -> PyResult<Py<PyAny>>
     |            ^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `anise-py` (lib) due to 2 previous errors

Steps to Reproduce

  1. Define a recursive type with Self in the field (cf. description)
  2. Build

Your operating system and version

Linux, Opensuse Tumbleweed

Your Python version (python --version)

3.11.12

Your Rust version (rustc --version)

1.88

Your PyO3 version

0.25.1

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

Virtualenv

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions