Skip to content

Failure to extract borrowed class references in experimental-async function #5681

@davidhewitt

Description

@davidhewitt

The following code does not compile with the experimental-async feature enabled:

#[pyclass]
struct Value(i32);

#[pymethods]
impl Value {
    #[new]
    fn new(x: i32) -> Self {
        Self(x)
    }
}

#[pyfunction]
async fn add_two_values(obj: &Value, obj2: &Value) -> Value {
    Value(obj.0 + obj2.0)
}

The error is:

error[E0521]: borrowed data escapes outside of function
   --> tests/test_coroutine.rs:373:5
    |
373 |     #[pyfunction]
    |     ^^^^^^^^^^^^^
    |     |
    |     `py` is a reference that is only valid in the function body
    |     `py` escapes the function body here
    |     lifetime `'py` defined here
    |     argument requires that `'py` must outlive `'static`
    |
    = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0716]: temporary value dropped while borrowed
   --> tests/test_coroutine.rs:373:5
    |
373 |     #[pyfunction]
    |     ^^^^^^^^^^^^-
    |     |           |
    |     |           temporary value is freed at the end of this statement
    |     creates a temporary value which is freed while still in use
    |     argument requires that borrow lasts for `'static`
    |
    = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions