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

Upgrade to Rust Edition 2024 #17967

Merged
merged 37 commits into from
Feb 24, 2025
Merged

Conversation

bushrat011899
Copy link
Contributor

@bushrat011899 bushrat011899 commented Feb 21, 2025

Objective

Solution

Testing

  • CI

Summary of Changes

Documentation Indentation

When using lists in documentation, proper indentation is now linted for. This means subsequent lines within the same list item must start at the same indentation level as the item.

/* Valid */
/// - Item 1
///   Run-on sentence.
/// - Item 2
struct Foo;

/* Invalid */
/// - Item 1
///     Run-on sentence.
/// - Item 2
struct Foo;

Implicit ! to () Conversion

! (the never return type, returned by panic!, etc.) no longer implicitly converts to (). This is particularly painful for systems with todo! or panic! statements, as they will no longer be functions returning () (or Result<()>), making them invalid systems for functions like add_systems. The ideal fix would be to accept functions returning ! (or rather, not returning), but this is blocked on the stabilisation of the ! type itself, which is not done.

The "simple" fix would be to add an explicit -> () to system signatures (e.g., || { todo!() } becomes || -> () { todo!() }). However, this is also banned, as there is an existing lint which (IMO, incorrectly) marks this as an unnecessary annotation.

So, the "fix" (read: workaround) is to put these kinds of || -> ! { ... } closuers into variables and give the variable an explicit type (e.g., fn()).

// Valid
let system: fn() = || todo!("Not implemented yet!");
app.add_systems(..., system);

// Invalid
app.add_systems(..., || todo!("Not implemented yet!"));

Temporary Variable Lifetimes

The order in which temporary variables are dropped has changed. The simple fix here is usually to just assign temporaries to a named variable before use.

gen is a keyword

We can no longer use the name gen as it is reserved for a future generator syntax. This involved replacing uses of the name gen with r#gen (the raw-identifier syntax).

Formatting has changed

Use statements have had the order of imports changed, causing a substantial +/-3,000 diff when applied. For now, I have opted-out of this change by amending rustfmt.toml

style_edition = "2021"

This preserves the original formatting for now, reducing the size of this PR. It would be a simple followup to update this to 2024 and run cargo fmt.

New use<> Opt-Out Syntax

Lifetimes are now implicitly included in RPIT types. There was a handful of instances where it needed to be added to satisfy the borrow checker, but there may be more cases where it should be added to avoid breakages in user code.

MyUnitStruct { .. } is an invalid pattern

Previously, you could match against unit structs (and unit enum variants) with a { .. } destructuring. This is no longer valid.

Pretty much every use of ref and mut are gone

Pattern binding has changed to the point where these terms are largely unused now. They still serve a purpose, but it is far more niche now.

iter::repeat(...).take(...) is bad

New lint recommends using the more explicit iter::repeat_n(..., ...) instead.

Migration Guide

The lifetimes of functions using return-position impl-trait (RPIT) are likely more conservative than they had been previously. If you encounter lifetime issues with such a function, please create an issue to investigate the addition of + use<...>.

Notes

  • Check the individual commits for a clearer breakdown for what actually changed.

Copy link
Contributor

You added a new example but didn't add metadata for it. Please update the root Cargo.toml file.

@bushrat011899
Copy link
Contributor Author

I've marked this PR as controversial because there is active debate around whether this should be done crate-by-crate, or some other approach. I don't believe the goal of using Rust 2024 is controversial at all.

Co-Authored-By: François Mockers <francois.mockers@vleue.com>
@mockersf mockersf removed the X-Controversial There is active debate or serious implications around merging this PR label Feb 22, 2025
@mockersf
Copy link
Member

removing the controversial tag, this PR is already done and almost ready, let's not block on that as we don't have an SME matching this PR

Co-Authored-By: François Mockers <francois.mockers@vleue.com>
Copy link
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

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

Looks good! And today I learned about repeat_n and div_ceil.

panic!("Observer triggered after being despawned.")
})
.id();
let system: fn(Trigger<OnAdd, A>) = |_| {
Copy link
Contributor

Choose a reason for hiding this comment

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

I still think these would be clearer as named functions. Converting to a fn pointer adds a layer of indirection and wouldn't work if it took system params.

... But we don't have params here, and we hardly need to worry about an extra indirect jump when calling a function that always panics, so it's really not important. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed! I'm just going to leave it as-is because I definitely would prefer that we actually support |...| -> ! closures as systems. It'll make the eventual PR that adds that support more explicit in its benefits too.

@bushrat011899 bushrat011899 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Help The author needs help finishing this PR. labels Feb 22, 2025
@mockersf
Copy link
Member

@bushrat011899 this has some conflicts from #17840. Could you resolve them?

@bushrat011899
Copy link
Contributor Author

@mockersf Merged and CI is green!

@mockersf mockersf added this pull request to the merge queue Feb 23, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 23, 2025
@bushrat011899
Copy link
Contributor Author

@mockersf Issue was caused by the use of #[no_mangle] within #[bevy_main] on mobile platforms, as in Rust Edition 2024, no_mangle is unsafe. I've added a safety comment but left a TODO around potentially making bevy_main itself unsafe (if that's even possible) or trying to strengthen its guards to make it safe to call.

I don't have a setup locally for checking this resolves the issue in CI, but it should be clear to merge now.

@mockersf mockersf added this pull request to the merge queue Feb 24, 2025
Merged via the queue into bevyengine:main with commit 5241e09 Feb 24, 2025
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Cross-Cutting Impacts the entire engine C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide M-Needs-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrade to Rust edition 2024
4 participants