DIY minimum supported Rust version marker

Rust/Cargo will get support for specifying a Minimum Supported Rust Version eventually, but what about the 40-something existing releases that won't support it?

The problem is that without MSRV support, old Rust versions trip up on some random syntax detail and tell the user that the code is broken (it isn't) or that it requires a nightly version of Rust (it doesn't). It gives a bad impression, and leaves users confused.

I wanted to display a clear upgrade message. So here's a hack: a syntax construct that only newer Rust can parse, and old versions of Rust display the comment about the need upgrade, without mentioning any nightly features. Because these are parse errors, they will display before all other errors.

This one works in 1.42 (currently stable), and displays error in all older versions:

#[cfg(feature = "this-is-only-a-version-check")] trait x {
/**!
Your Rust version is too old. You must upgrade to the latest version.  Please install the latest version from https://rustup.rs or run `rustup update`                                  */
  default
fn x() {}}

This one works in 1.43 (next release), and displays error in all older versions:

#[cfg(feature = "this-is-only-a-version-check")]
type x /* Your Rust version is too old. You must upgrade to the latest version.

Please install the latest version from https://rustup.rs or run `rustup update`   */:x;
8 Likes

If you want a more clear error message, why not check the Rust version in the build script and output an error? This way, there are no rustc's compile errors, and you can also print the minimal required version. Crates like version_check make this quite easy.

build.rs and launching another instance of rustc seems heavy to me, especially if every crate did it.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.