Skip to content

chore: switch to Rust 2024 edition #1269

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

Merged
merged 6 commits into from
May 5, 2025
Merged

Conversation

DavSanchez
Copy link
Contributor

@DavSanchez DavSanchez commented Apr 30, 2025

What this PR does / why we need it

Updates the project to use Rust 2024, which includes some syntax and semantic breaking changes but will allow us to use additional new features such as let_chains in Rust 1.88.

The codebase was migrated automatically using cargo fix --edition --all-features so there might be changes done conservatively, like the match expressions replacing if lets in dafa43c. Where appropriate, I'll try to rework the syntax for it to be more clear before applying the final cargo fmt that will change a lot of files.

Checklist

[Place an '[x]' (no spaces) in all applicable fields. Please remove unrelated fields.]

  • Provided a meaningful title following conventional commit style.
  • Included a detailed description for the Pull Request.
  • Documentation under docs is aligned with the change.
  • Follows guidelines for Pull Requests in CONTRIBUTING.md.
  • Follows log level guidelines.

@DavSanchez DavSanchez changed the title Chore/rust 2024 edition chore: use Rust 2024 edition Apr 30, 2025
@DavSanchez DavSanchez force-pushed the chore/rust-2024-edition branch 2 times, most recently from b349801 to eb62183 Compare May 2, 2025 08:36
@@ -50,7 +50,7 @@ pub struct LoggingConfig {
impl LoggingConfig {
/// Returns the configured filter according to the corresponding fields. The filter will also allow
/// any span whose level doesn't exceed [SPAN_ATTRIBUTES_MAX_LEVEL].
pub fn filter(&self) -> Result<impl Filter<Registry>, LoggingConfigError> {
pub fn filter(&self) -> Result<impl Filter<Registry> + use<>, LoggingConfigError> {
Copy link
Contributor Author

@DavSanchez DavSanchez May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've read some documentation and blog posts. I'm still a bit puzzled. Was this added by the migrator? Is it extremely necessary or the function would still work if we removed the use bound, but using it is more correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it was added automatically by the migrator. In Rust 2024 if you remove this the project stops compiling due to borrowed data escaping some functions in the tracing_layers module. Let me retrieve the exact lints to give more context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry, I can do that on my computer and check it out. I was curious if it was automatically done and potentially not required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ cargo build
# ...
error[E0521]: borrowed data escapes outside of function
  --> agent-control/src/instrumentation/tracing_layers/file.rs:29:25
   |
18 |       config: &LoggingConfig,
   |       ------  - let's call the lifetime of this reference `'1`
   |       |
   |       `config` is a reference that is only valid in the function body
...
29 |               let layer = tracing_subscriber::fmt::layer()
   |  _________________________^
30 | |                 .with_writer(file_writer)
31 | |                 .with_ansi(false) // Disable colors for file
32 | |                 .with_target(target)
...  |
35 | |                 .with_filter(config.filter()?)
36 | |                 .boxed();
   | |                        ^
   | |                        |
   | |________________________`config` escapes the function body here
   |                          argument requires that `'1` must outlive `'static`

error[E0521]: borrowed data escapes outside of function
  --> agent-control/src/instrumentation/tracing_layers/stdout.rs:12:17
   |
8  |   pub fn stdout(config: &LoggingConfig) -> Result<LayerBox, LoggingConfigError> {
   |                 ------  - let's call the lifetime of this reference `'1`
   |                 |
   |                 `config` is a reference that is only valid in the function body
...
12 |       let layer = tracing_subscriber::fmt::layer()
   |  _________________^
13 | |         .with_writer(std::io::stdout)
14 | |         .with_ansi(config.format.ansi_colors)
15 | |         .with_target(target)
...  |
18 | |         .with_filter(config.filter()?)
19 | |         .boxed();
   | |                ^
   | |                |
   | |________________`config` escapes the function body here
   |                  argument requires that `'1` must outlive `'static`

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

So now apparently we have to make explicit that the type impl Filter<Registry> doesn't capture any lifetimes of the &LoggingConfig/&self. I will try to search for more info to better clarify how this changed in Rust 2024.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the exact change related to "impl traits in return position (RPIT)": https://doc.rust-lang.org/nightly/edition-guide/rust-2024/rpit-lifetime-capture.html

In Rust 2024, all in-scope generic parameters, including lifetime parameters, are implicitly captured when the use<..> bound is not present.

@DavSanchez DavSanchez force-pushed the chore/rust-2024-edition branch from eb62183 to 02cb7c9 Compare May 2, 2025 09:12
@DavSanchez DavSanchez force-pushed the chore/rust-2024-edition branch from 02cb7c9 to 6ba44fd Compare May 2, 2025 09:30
@DavSanchez DavSanchez changed the title chore: use Rust 2024 edition chore: switch to Rust 2024 edition May 2, 2025
@DavSanchez DavSanchez force-pushed the chore/rust-2024-edition branch from 26e7c4d to 087f008 Compare May 2, 2025 10:46
@DavSanchez DavSanchez force-pushed the chore/rust-2024-edition branch from 087f008 to 11ccbb3 Compare May 2, 2025 11:41
@DavSanchez DavSanchez marked this pull request as ready for review May 2, 2025 11:41
@DavSanchez DavSanchez requested a review from a team as a code owner May 2, 2025 11:41
@DavSanchez DavSanchez merged commit 00bba4f into main May 5, 2025
28 checks passed
@DavSanchez DavSanchez deleted the chore/rust-2024-edition branch May 5, 2025 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants