Skip to content

Edition 2024 now reports: argument requires that '1 must outlive 'static #133529

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

Closed
zh-jq-b opened this issue Nov 27, 2024 · 6 comments
Closed

Edition 2024 now reports: argument requires that '1 must outlive 'static #133529

zh-jq-b opened this issue Nov 27, 2024 · 6 comments
Labels
A-async-await Area: Async & Await A-edition-2024 Area: The 2024 edition A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. requires-nightly This issue requires a nightly compiler in some way.

Comments

@zh-jq-b
Copy link

zh-jq-b commented Nov 27, 2024

I'm trying to test edition 2024 here by using branch https://github.com/bytedance/g3/tree/edition-2024, now some of the code reports

argument requires that `'1` must outlive `'static`

The toolchain version is

cargo 1.85.0-nightly (4c39aaff6 2024-11-25)

Code

The code is here: https://github.com/bytedance/g3, branch edition-2024.

cargo +nightly build -p g3-ctl

will give error at line https://github.com/bytedance/g3/blob/40d1e2e5946e701deb0a26d2e8342120a2fb89f5/lib/g3-ctl/src/opts.rs#L92

   Compiling g3-ctl v0.1.0 (/code/rust/g3/lib/g3-ctl)
error[E0521]: borrowed data escapes outside of method
  --> lib/g3-ctl/src/opts.rs:92:22
   |
86 |         &self,
   |         -----
   |         |
   |         `self` is a reference that is only valid in the method body
   |         let's call the lifetime of this reference `'1`
87 |         daemon_name: &'static str,
   |         ----------- `daemon_name` declared here, outside of the method body
...
92 |         let stream = self.connect_to_daemon(daemon_name).await?;
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                      |
   |                      `self` escapes the method body here
   |                      argument requires that `'1` must outlive `'static`

For more information about this error, try `rustc --explain E0521`.
error: could not compile `g3-ctl` (lib) due to 1 previous error

And

cargo +nightly build -p g3-daemon

will give error at line https://github.com/bytedance/g3/blob/40d1e2e5946e701deb0a26d2e8342120a2fb89f5/lib/g3-daemon/src/control/local/mod.rs#L174

   Compiling g3-daemon v0.2.0 (/code/rust/g3/lib/g3-daemon)
error[E0521]: borrowed data escapes outside of associated function
   --> lib/g3-daemon/src/control/local/mod.rs:174:26
    |
168 |         daemon_name: &str,
    |         -----------  - let's call the lifetime of this reference `'1`
    |         |
    |         `daemon_name` is a reference that is only valid in the associated function body
...
174 |         let mut stream = LocalControllerImpl::connect_to_daemon(daemon_name, daemon_group).await?;
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                          |
    |                          `daemon_name` escapes the associated function body here
    |                          argument requires that `'1` must outlive `'static`

error[E0521]: borrowed data escapes outside of associated function
   --> lib/g3-daemon/src/control/local/mod.rs:174:26
    |
169 |         daemon_group: &str,
    |         ------------  - let's call the lifetime of this reference `'2`
    |         |
    |         `daemon_group` is a reference that is only valid in the associated function body
...
174 |         let mut stream = LocalControllerImpl::connect_to_daemon(daemon_name, daemon_group).await?;
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                          |
    |                          `daemon_group` escapes the associated function body here
    |                          argument requires that `'2` must outlive `'static`

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

Version it worked on

Edition 2021

@zh-jq-b zh-jq-b added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Nov 27, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 27, 2024
@apiraino apiraino added requires-nightly This issue requires a nightly compiler in some way. A-edition-2024 Area: The 2024 edition labels Nov 27, 2024
@fmease fmease added A-async-await Area: Async & Await E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example labels Nov 27, 2024
@fmease
Copy link
Member

fmease commented Nov 27, 2024

I presume you didn't run cargo fix --edition --workspace beforehand?

@fmease
Copy link
Member

fmease commented Nov 27, 2024

I'm pretty sure these anyhow::Result<impl AsyncRead + AsyncWrite> need to be turned into anyhow::Result<impl AsyncRead + AsyncWrite + use<>> to prevent the input lifetimes from getting captured.

If you did run cargo fix --edition --workspace and it did not add the + use<>, then that's a bug with the migration lints, otherwise it's a user error (you generally need to migrate to the next edition in some way, not just change the edition field).

@fmease fmease added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. S-needs-info Status: The issue lacks details necessary to triage or act on it. A-async-await Area: Async & Await and removed A-async-await Area: Async & Await E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 27, 2024
@fmease
Copy link
Member

fmease commented Nov 27, 2024

I've cloned your project and executed cargo fix --edition --workspace. The migration changed 282 files (1438 insertions, 1451 deletions) and it did change the return type of async fn connect_to_daemon to anyhow::Result<impl AsyncRead + AsyncWrite + use<>> from anyhow::Result<impl AsyncRead + AsyncWrite> contrary to your edition-2024 branch.

@fmease
Copy link
Member

fmease commented Nov 27, 2024

After the cargo fix --edition --workspace, I completed the migration by cherry-picking the new commit from your edition-2024 branch which actually sets the edition to "2024" among other things. Lastly, I ran cargo check and cargo check --workspace on the freshly migrated project both of which succeeded.

Closing as user error.

Please see also https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2024
@fmease fmease removed C-bug Category: This is a bug. I-prioritize Issue: Indicates that prioritization has been requested for this issue. regression-untriaged Untriaged performance or correctness regression. S-needs-info Status: The issue lacks details necessary to triage or act on it. labels Nov 27, 2024
@zh-jq-b
Copy link
Author

zh-jq-b commented Nov 28, 2024

I presume you didn't run cargo fix --edition --workspace beforehand?

I did run this but found too many if let -> match changes that I don't want, so I revert the fix and try to change the code manually.

Though it's a user error, the error message given by the compiler can be improved?

@zh-jq-b
Copy link
Author

zh-jq-b commented Nov 28, 2024

I finished the migration by changing to the new impl Trait lifetime rules,
The necessary changes is only

19 files changed, 48 insertions(+), 37 deletions(-)

Is it possible to add a --necessary option to cargo fix --edition --workspace to only do required changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-edition-2024 Area: The 2024 edition A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

No branches or pull requests

4 participants