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

Invalid macro rewrite in PySequenceProtocol (0.6.0) #421

Closed
althonos opened this issue Mar 29, 2019 · 1 comment
Closed

Invalid macro rewrite in PySequenceProtocol (0.6.0) #421

althonos opened this issue Mar 29, 2019 · 1 comment
Labels

Comments

@althonos
Copy link
Member

🐛 Bug Reports

The signature of __setitem__ for PySequenceProtocol implementations is not rewritten the right way, and the wrong argument is replaced. See example.

🌍 Environment

  • Your operating system and version: Linux (Archlinux)
  • Your python version: 3.7
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: pacman -S python
  • Your rust version (rustc --version): rustc 1.35.0-nightly (94fd04589 2019-03-21)
  • Are you using the latest pyo3 version? Have you tried using latest master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")? Tried with both v0.6.0 and master.

💥 Reproducing

Minimal example:

#[macro_use]
extern crate pyo3;
use pyo3::prelude::*;
use pyo3::PySequenceProtocol;

#[pyclass]
pub struct MyList {
    elems: Vec<u8>
}

#[pyproto]
impl PySequenceProtocol for MyList {
  fn __setitem__(&mut self, key: isize, item: u8) -> PyResult<()> {
    self.elems[key as usize] = item;
  }
}

When trying to compile, the compiler will fail with:

error[E0053]: method `__setitem__` has an incompatible type for trait
  --> src/lib.rs:15:3
   |
15 |   fn __setitem__(&mut self, key: isize, item: u8) -> PyResult<()> {
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected isize, found associated type
   |
   = note: expected type `fn(&'p mut MyList, isize, <MyList as pyo3::sequence::PySequenceSetItemProtocol<'p>>::Value) -> <MyList as pyo3::sequence::PySequenceSetItemProtocol<'p>>::Result`
              found type `fn(&'p mut MyList, <MyList as pyo3::sequence::PySequenceSetItemProtocol<'p>>::Value, u8) -> <MyList as pyo3::sequence::PySequenceSetItemProtocol<'p>>::Result`

After running cargo expand, the actual signature generated by the pyproto macro is:

impl<'p> PySequenceProtocol<'p> for MyList {
    fn __setitem__(
        &'p mut self,
        key: <MyList as pyo3::class::sequence::PySequenceSetItemProtocol<'p>>::Value,
        item: u8,
    ) -> <MyList as pyo3::class::sequence::PySequenceSetItemProtocol<'p>>::Result {
        self.elems[key as usize] = item;
    }
}

where it should be (if I'm not mistaken):

impl<'p> PySequenceProtocol<'p> for MyList {
    fn __setitem__(
        &'p mut self,
        key: isize,
        item: <MyList as pyo3::class::sequence::PySequenceSetItemProtocol<'p>>::Value,
    ) -> <MyList as pyo3::class::sequence::PySequenceSetItemProtocol<'p>>::Result {
        self.elems[key as usize] = item;
    }
}
@althonos
Copy link
Member Author

althonos commented Apr 2, 2019

Closed by #423.

@althonos althonos closed this as completed Apr 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants