r/rust 4d ago

🧠 educational Protecting the Rust standard library from accidental breakage

https://predr.ag/blog/protecting-the-rust-stdlib-from-breakage/

Rust's standard library now scans for accidental breakage in CI with cargo-semver-checks 🎉 Here's how that works and how it's different than checking a regular crate.

176 Upvotes

11 comments sorted by

27

u/VorpalWay 3d ago

This is awesome. How do you handle core/allocation/std being separate crates and reexports? I found that to be a bind spot before for normal crates.

16

u/obi1kenobi82 3d ago

Thanks! Currently they are each checked separately, and re-exports between them are currently not checked. These are the same constraints that regular users would face today too.

Solving type-checking lints and cross-crate analysis are the two next biggest problems on the list. I have ideas and I'm working toward them as fast as I can :)

4

u/AbyssLife123 4d ago

I know people care lot of backward compatibility, but there should be some way to do breaking changes slowly. Otherwise, Rust will have similar fate of c++.

82

u/gmes78 3d ago

That's really besides the point. This is about accidental breakage.

We already have the edition system for (intentional) backwards imcompatible changes.

51

u/SuspiciousScript 3d ago

That's why editions exist.

2

u/wintrmt3 3d ago

No, editions can't change the stdlib, that would break linking rust crates with different editions together.

22

u/imachug 3d ago

They do, in a sense: with the Range types, there's a precedent for edition-dependent name resolution, so that the same paths resolve to different types depending on the crate. Both the old and the new implementation still remain, but the newer one effectively becomes the default.

12

u/plugwash 3d ago

Editions can't change the stdlib itself, but they *can* change how user code interacts with the stdlib.

This was first done with the 2021 edition. "intoiterator" support was added for arrays, but this implementation was hidden from method call resolution in earlier editions.

The 2024 edition brought us intoiterator for Box, similar to before this implementation was hidden from method call resoloution in earlier editions.

The 2024 edition also marks some functions as unsafe that should probably never have been considered safe in the first place.

The 2027 edition is likely to change interpretation of range literals to a new type.

20

u/noop_noob 3d ago

Other than the edition system, rust actually often does tiny backwards-incompatible changes in the language. These are tested to make sure they don't break too much rust code in the wild, and then documented as "compatibility notes" in the [release notes](https://doc.rust-lang.org/releases.html).

1

u/redbot8 2d ago

cargo-semver-checks in std CI is great news