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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

E0716 "temporary value dropped while borrowed" in #[pyfunction] #3138

Closed
thejcannon opened this issue May 6, 2023 · 1 comment 路 Fixed by #3142
Closed

E0716 "temporary value dropped while borrowed" in #[pyfunction] #3138

thejcannon opened this issue May 6, 2023 · 1 comment 路 Fixed by #3142
Labels

Comments

@thejcannon
Copy link

Bug Description

馃憢 Love PyO3!

Upgrading from 0.16 to 0.18 I'm seeing the following compiler error:

error[E0716]: temporary value dropped while borrowed
    --> src/externs/interface.rs:1024:17
     |
1021 | #[pyfunction]
     | -------------
     | |           |
     | |           temporary value is freed at the end of this statement
     | borrow later used here
...
1024 |   py_scheduler: &'py PyScheduler,
     |                 ^ creates a temporary value which is freed while still in use
     |
     = note: consider using a `let` binding to create a longer lived value

The code in question looks like:

#[pyclass]
struct PyScheduler(Scheduler);

// ...

#[pyfunction]
fn scheduler_metrics<'py>(
  py: Python<'py>,
  py_scheduler: &'py PyScheduler,
  py_session: &'py PySession,
) -> HashMap<&'py str, i64> {
  py_scheduler
    .0
    .core
    .executor
    .enter(|| py.allow_threads(|| py_scheduler.0.metrics(&py_session.0)))
}

Steps to Reproduce

(Try and compile the code)

Backtrace

No response

Your operating system and version

Ubuntu 20

Your Python version (python --version)

3.8

Your Rust version (rustc --version)

rustc 1.69.0 (84c898d65 2023-04-16)

Your PyO3 version

0.18.3

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

apt and no venv

Additional Info

No response

@thejcannon thejcannon added the bug label May 6, 2023
@adamreichold
Copy link
Member

adamreichold commented May 6, 2023

I am able to reproduce and the seems to stem from returning HashMap<&'py str, i64> instead of e.g. HashMap<String, i64>.

bors bot added a commit that referenced this issue May 9, 2023
3142: Do not store return values in locals so that holders benefit from lifetime extension for temporaries. r=davidhewitt a=adamreichold

Closes #3138

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
@bors bors bot closed this as completed in c9f383a May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants