Announcing Rust 1.78.0

https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html

The Rust team is happy to announce a new version of Rust, 1.78.0. Rust is a programming language empowering everyone to build reliable and efficient software.

If you have a previous version of Rust installed via rustup, you can get 1.78.0 with:

$ rustup update stable

If you don't have it already, you can get rustup from the appropriate page on our website, and check out the detailed release notes for 1.78.0.

If you'd like to help us out by testing future releases, you might consider updating locally to use the beta channel (rustup default beta) or the nightly channel (rustup default nightly). Please report any bugs you might come across!

What's in 1.78.0 stable

Diagnostic attributes

Rust now supports a #[diagnostic] attribute namespace to influence compiler error messages. These are treated as hints which the compiler is not required to use, and it is also not an error to provide a diagnostic that the compiler doesn't recognize. This flexibility allows source code to provide diagnostics even when they're not supported by all compilers, whether those are different versions or entirely different implementations.

With this namespace comes the first supported attribute, #[diagnostic::on_unimplemented], which can be placed on a trait to customize the message when that trait is required but hasn't been implemented on a type. Consider the example given in the stabilization pull request:

#[diagnostic::on_unimplemented(
    message = "My Message for `ImportantTrait<{A}>` is not implemented for `{Self}`",
    label = "My Label",
    note = "Note 1",
    note = "Note 2"
)]
trait ImportantTrait<A> {}
fn use_my_trait(_: impl ImportantTrait<i32>) {}
fn main() {
    use_my_trait(String::new());
}

Previously, the compiler would give a builtin error like this:

error[E0277]: the trait bound `String: ImportantTrait<i32>` is not satisfied
  --> src/main.rs:12:18
   |
12 |     use_my_trait(String::new());
   |     ------------ ^^^^^^^^^^^^^ the trait `ImportantTrait<i32>` is not implemented for `String`
   |     |
   |     required by a bound introduced by this call
   |

With #[diagnostic::on_unimplemented], its custom message fills the primary error line, and its custom label is placed on the source output. The original label is still written as help output, and any custom notes are written as well. (These exact details are subject to change.)

error[E0277]: My Message for `ImportantTrait<i32>` is not implemented for `String`
  --> src/main.rs:12:18
   |
12 |     use_my_trait(String::new());
   |     ------------ ^^^^^^^^^^^^^ My Label
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `ImportantTrait<i32>` is not implemented for `String`
   = note: Note 1
   = note: Note 2

For trait authors, this kind of diagnostic is more useful if you can provide a better hint than just talking about the missing implementation itself. For example, this is an abridged sample from the standard library:

#[diagnostic::on_unimplemented(
    message = "the size for values of type `{Self}` cannot be known at compilation time",
    label = "doesn't have a size known at compile-time"
)]
pub trait Sized {}

For more information, see the reference section on the diagnostic tool attribute namespace.

Asserting unsafe preconditions

The Rust standard library has a number of assertions for the preconditions of unsafe functions, but historically they have only been enabled in #[cfg(debug_assertions)] builds of the standard library to avoid affecting release performance. However, since the standard library is usually compiled and distributed in release mode, most Rust developers weren't ever executing these checks at all.

Now, the condition for these assertions is delayed until code generation, so they will be checked depending on the user's own setting for debug assertions -- enabled by default in debug and test builds. This change helps users catch undefined behavior in their code, though the details of how much is checked are generally not stable.

For example, slice::from_raw_parts requires an aligned non-null pointer. The following use of a purposely-misaligned pointer has undefined behavior, and while if you were unlucky it may have appeared to "work" in the past, the debug assertion can now catch it:

fn main() {
    let slice: &[u8] = &[1, 2, 3, 4, 5];
    let ptr = slice.as_ptr();
    // Create an offset from `ptr` that will always be one off from `u16`'s correct alignment
    let i = usize::from(ptr as usize & 1 == 0);
    let slice16: &[u16] = unsafe { std::slice::from_raw_parts(ptr.add(i).cast::<u16>(), 2) };
    dbg!(slice16);
}
thread 'main' panicked at library/core/src/panicking.rs:220:5:
unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting.

Deterministic realignment

The standard library has a few functions that change the alignment of pointers and slices, but they previously had caveats that made them difficult to rely on in practice, if you followed their documentation precisely. Those caveats primarily existed as a hedge against const evaluation, but they're only stable for non-const use anyway. They are now promised to have consistent runtime behavior according to their actual inputs.

  • pointer::align_offset computes the offset needed to change a pointer to the given alignment. It returns usize::MAX if that is not possible, but it was previously permitted to always return usize::MAX, and now that behavior is removed.

  • slice::align_to and slice::align_to_mut both transmute slices to an aligned middle slice and the remaining unaligned head and tail slices. These methods now promise to return the largest possible middle part, rather than allowing the implementation to return something less optimal like returning everything as the head slice.

Stabilized APIs

These APIs are now stable in const contexts:

Compatibility notes

  • As previously announced, Rust 1.78 has increased its minimum requirement to Windows 10 for the following targets:
    • x86_64-pc-windows-msvc
    • i686-pc-windows-msvc
    • x86_64-pc-windows-gnu
    • i686-pc-windows-gnu
    • x86_64-pc-windows-gnullvm
    • i686-pc-windows-gnullvm
  • Rust 1.78 has upgraded its bundled LLVM to version 18, completing the announced u128/i128 ABI change for x86-32 and x86-64 targets. Distributors that use their own LLVM older than 18 may still face the calling convention bugs mentioned in that post.

Other changes

Check out everything that changed in Rust, Cargo, and Clippy.

Contributors to 1.78.0

Many people came together to create Rust 1.78.0. We couldn't have done it without all of you. Thanks!

{
"by": "thunderbong",
"descendants": 0,
"id": 40244883,
"score": 2,
"time": 1714718886,
"title": "Announcing Rust 1.78.0",
"type": "story",
"url": "https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html"
}
{
"author": "May 2, 2024 · The Rust Release Team",
"date": "2024-05-02T12:00:00.000Z",
"description": "Empowering everyone to build reliable and efficient software.",
"image": "https://www.rust-lang.org/static/images/rust-social-wide.jpg",
"logo": "https://logo.clearbit.com/rust-lang.org",
"publisher": "Rust Foundation",
"title": "Announcing Rust 1.78.0 | Rust Blog",
"url": "https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html"
}
{
"url": "https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html",
"title": "Announcing Rust 1.78.0 | Rust Blog",
"description": "The Rust team is happy to announce a new version of Rust, 1.78.0. Rust is a programming language empowering everyone to build reliable and efficient software. If you have a previous version of Rust installed...",
"links": [
"https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html"
],
"image": "https://www.rust-lang.org/static/images/rust-social-wide.jpg",
"content": "<div>\n <p>The Rust team is happy to announce a new version of Rust, 1.78.0. Rust is a programming language empowering everyone to build reliable and efficient software.</p>\n<p>If you have a previous version of Rust installed via <code>rustup</code>, you can get 1.78.0 with:</p>\n<pre><code>$ rustup update stable\n</code></pre>\n<p>If you don't have it already, you can <a target=\"_blank\" href=\"https://www.rust-lang.org/install.html\">get <code>rustup</code></a> from the appropriate page on our website, and check out the <a target=\"_blank\" href=\"https://doc.rust-lang.org/nightly/releases.html#version-1780-2024-05-02\">detailed release notes for 1.78.0</a>.</p>\n<p>If you'd like to help us out by testing future releases, you might consider updating locally to use the beta channel (<code>rustup default beta</code>) or the nightly channel (<code>rustup default nightly</code>). Please <a target=\"_blank\" href=\"https://github.com/rust-lang/rust/issues/new/choose\">report</a> any bugs you might come across!</p>\n<h2>What's in 1.78.0 stable</h2>\n<h3>Diagnostic attributes</h3>\n<p>Rust now supports a <code>#[diagnostic]</code> attribute namespace to influence compiler error messages. These are treated as hints which the compiler is not <em>required</em> to use, and it is also not an error to provide a diagnostic that the compiler doesn't recognize. This flexibility allows source code to provide diagnostics even when they're not supported by all compilers, whether those are different versions or entirely different implementations.</p>\n<p>With this namespace comes the first supported attribute, <code>#[diagnostic::on_unimplemented]</code>, which can be placed on a trait to customize the message when that trait is required but hasn't been implemented on a type. Consider the example given in the <a target=\"_blank\" href=\"https://github.com/rust-lang/rust/pull/119888/\">stabilization pull request</a>:</p>\n<pre><code>#[diagnostic::on_unimplemented(\n message = \"My Message for `ImportantTrait&lt;{A}&gt;` is not implemented for `{Self}`\",\n label = \"My Label\",\n note = \"Note 1\",\n note = \"Note 2\"\n)]\ntrait ImportantTrait&lt;A&gt; {}\nfn use_my_trait(_: impl ImportantTrait&lt;i32&gt;) {}\nfn main() {\n use_my_trait(String::new());\n}\n</code></pre>\n<p>Previously, the compiler would give a builtin error like this:</p>\n<pre><code>error[E0277]: the trait bound `String: ImportantTrait&lt;i32&gt;` is not satisfied\n --&gt; src/main.rs:12:18\n |\n12 | use_my_trait(String::new());\n | ------------ ^^^^^^^^^^^^^ the trait `ImportantTrait&lt;i32&gt;` is not implemented for `String`\n | |\n | required by a bound introduced by this call\n |\n</code></pre>\n<p>With <code>#[diagnostic::on_unimplemented]</code>, its custom message fills the primary error line, and its custom label is placed on the source output. The original label is still written as help output, and any custom notes are written as well. (These exact details are subject to change.)</p>\n<pre><code>error[E0277]: My Message for `ImportantTrait&lt;i32&gt;` is not implemented for `String`\n --&gt; src/main.rs:12:18\n |\n12 | use_my_trait(String::new());\n | ------------ ^^^^^^^^^^^^^ My Label\n | |\n | required by a bound introduced by this call\n |\n = help: the trait `ImportantTrait&lt;i32&gt;` is not implemented for `String`\n = note: Note 1\n = note: Note 2\n</code></pre>\n<p>For trait authors, this kind of diagnostic is more useful if you can provide a better hint than just talking about the missing implementation itself. For example, this is an abridged sample from the standard library:</p>\n<pre><code>#[diagnostic::on_unimplemented(\n message = \"the size for values of type `{Self}` cannot be known at compilation time\",\n label = \"doesn't have a size known at compile-time\"\n)]\npub trait Sized {}\n</code></pre>\n<p>For more information, see the reference section on <a target=\"_blank\" href=\"https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace\">the <code>diagnostic</code> tool attribute namespace</a>.</p>\n<h3>Asserting <code>unsafe</code> preconditions</h3>\n<p>The Rust standard library has a number of assertions for the preconditions of <code>unsafe</code> functions, but historically they have only been enabled in <code>#[cfg(debug_assertions)]</code> builds of the standard library to avoid affecting release performance. However, since the standard library is usually compiled and distributed in release mode, most Rust developers weren't ever executing these checks at all.</p>\n<p>Now, the condition for these assertions is delayed until code generation, so they will be checked depending on the user's own setting for debug assertions -- enabled by default in debug and test builds. This change helps users catch undefined behavior in their code, though the details of how much is checked are generally not stable.</p>\n<p>For example, <a target=\"_blank\" href=\"https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html\"><code>slice::from_raw_parts</code></a> requires an aligned non-null pointer. The following use of a purposely-misaligned pointer has undefined behavior, and while if you were unlucky it may have <em>appeared</em> to \"work\" in the past, the debug assertion can now catch it:</p>\n<pre><code>fn main() {\n let slice: &amp;[u8] = &amp;[1, 2, 3, 4, 5];\n let ptr = slice.as_ptr();\n // Create an offset from `ptr` that will always be one off from `u16`'s correct alignment\n let i = usize::from(ptr as usize &amp; 1 == 0);\n let slice16: &amp;[u16] = unsafe { std::slice::from_raw_parts(ptr.add(i).cast::&lt;u16&gt;(), 2) };\n dbg!(slice16);\n}\n</code></pre>\n<pre><code>thread 'main' panicked at library/core/src/panicking.rs:220:5:\nunsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\nthread caused non-unwinding panic. aborting.\n</code></pre>\n<h3>Deterministic realignment</h3>\n<p>The standard library has a few functions that change the alignment of pointers and slices, but they previously had caveats that made them difficult to rely on in practice, if you followed their documentation precisely. Those caveats primarily existed as a hedge against <code>const</code> evaluation, but they're only stable for non-<code>const</code> use anyway. They are now promised to have consistent runtime behavior according to their actual inputs.</p>\n<ul>\n<li>\n<p><a target=\"_blank\" href=\"https://doc.rust-lang.org/std/primitive.pointer.html#method.align_offset\"><code>pointer::align_offset</code></a> computes the offset needed to change a pointer to the given alignment. It returns <code>usize::MAX</code> if that is not possible, but it was previously permitted to <em>always</em> return <code>usize::MAX</code>, and now that behavior is removed.</p>\n</li>\n<li>\n<p><a target=\"_blank\" href=\"https://doc.rust-lang.org/std/primitive.slice.html#method.align_to\"><code>slice::align_to</code></a> and <a target=\"_blank\" href=\"https://doc.rust-lang.org/std/primitive.slice.html#method.align_to_mut\"><code>slice::align_to_mut</code></a> both transmute slices to an aligned middle slice and the remaining unaligned head and tail slices. These methods now promise to return the largest possible middle part, rather than allowing the implementation to return something less optimal like returning everything as the head slice.</p>\n</li>\n</ul>\n<h3>Stabilized APIs</h3>\n<ul>\n<li><a target=\"_blank\" href=\"https://doc.rust-lang.org/stable/std/io/struct.Stdin.html#impl-Read-for-%26Stdin\"><code>impl Read for &amp;Stdin</code></a></li>\n<li><a target=\"_blank\" href=\"https://github.com/rust-lang/rust/pull/113833/\">Accept non <code>'static</code> lifetimes for several <code>std::error::Error</code> related implementations</a></li>\n<li><a target=\"_blank\" href=\"https://github.com/rust-lang/rust/pull/114655/\">Make <code>impl&lt;Fd: AsFd&gt;</code> impl take <code>?Sized</code></a></li>\n<li><a target=\"_blank\" href=\"https://doc.rust-lang.org/stable/std/io/struct.Error.html#impl-From%3CTryReserveError%3E-for-Error\"><code>impl From&lt;TryReserveError&gt; for io::Error</code></a></li>\n</ul>\n<p>These APIs are now stable in const contexts:</p>\n<ul>\n<li><a target=\"_blank\" href=\"https://doc.rust-lang.org/stable/std/sync/struct.Barrier.html#method.new\"><code>Barrier::new()</code></a></li>\n</ul>\n<h3>Compatibility notes</h3>\n<ul>\n<li>As <a target=\"_blank\" href=\"https://blog.rust-lang.org/2024/02/26/Windows-7.html\">previously announced</a>, Rust 1.78 has increased its minimum requirement to Windows 10 for the following targets:\n<ul>\n<li><code>x86_64-pc-windows-msvc</code></li>\n<li><code>i686-pc-windows-msvc</code></li>\n<li><code>x86_64-pc-windows-gnu</code></li>\n<li><code>i686-pc-windows-gnu</code></li>\n<li><code>x86_64-pc-windows-gnullvm</code></li>\n<li><code>i686-pc-windows-gnullvm</code></li>\n</ul>\n</li>\n<li>Rust 1.78 has upgraded its bundled LLVM to version 18, completing the announced <a target=\"_blank\" href=\"https://blog.rust-lang.org/2024/03/30/i128-layout-update.html\"><code>u128</code>/<code>i128</code> ABI change</a> for x86-32 and x86-64 targets. Distributors that use their own LLVM older than 18 may still face the calling convention bugs mentioned in that post.</li>\n</ul>\n<h3>Other changes</h3>\n<p>Check out everything that changed in <a target=\"_blank\" href=\"https://github.com/rust-lang/rust/releases/tag/1.78.0\">Rust</a>, <a target=\"_blank\" href=\"https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-178-2024-05-02\">Cargo</a>, and <a target=\"_blank\" href=\"https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-178\">Clippy</a>.</p>\n<h2>Contributors to 1.78.0</h2>\n<p>Many people came together to create Rust 1.78.0. We couldn't have done it without all of you. <a target=\"_blank\" href=\"https://thanks.rust-lang.org/rust/1.78.0/\">Thanks!</a></p>\n </div>",
"author": "@rustlang",
"favicon": "https://blog.rust-lang.org/images/favicon.svg",
"source": "blog.rust-lang.org",
"published": "",
"ttr": 205,
"type": "website"
}