Skip to content

Commit c05f10d

Browse files
Merge pull request #1321 from cuviper/rust-1.78.0
Rust 1.78.0 announcement
2 parents d13d88e + 728932d commit c05f10d

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

posts/2024-05-02-Rust-1.78.0.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
layout: post
3+
title: "Announcing Rust 1.78.0"
4+
author: The Rust Release Team
5+
release: true
6+
---
7+
8+
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.
9+
10+
If you have a previous version of Rust installed via `rustup`, you can get 1.78.0 with:
11+
12+
```console
13+
$ rustup update stable
14+
```
15+
16+
If you don't have it already, you can [get `rustup`](https://www.rust-lang.org/install.html) from the appropriate page on our website, and check out the [detailed release notes for 1.78.0](https://doc.rust-lang.org/nightly/releases.html#version-1780-2024-05-02).
17+
18+
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](https://github.com/rust-lang/rust/issues/new/choose) any bugs you might come across!
19+
20+
## What's in 1.78.0 stable
21+
22+
### Diagnostic attributes
23+
24+
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.
25+
26+
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](https://github.com/rust-lang/rust/pull/119888/):
27+
28+
```rust
29+
#[diagnostic::on_unimplemented(
30+
message = "My Message for `ImportantTrait<{A}>` is not implemented for `{Self}`",
31+
label = "My Label",
32+
note = "Note 1",
33+
note = "Note 2"
34+
)]
35+
trait ImportantTrait<A> {}
36+
37+
fn use_my_trait(_: impl ImportantTrait<i32>) {}
38+
39+
fn main() {
40+
use_my_trait(String::new());
41+
}
42+
```
43+
44+
Previously, the compiler would give a builtin error like this:
45+
46+
```
47+
error[E0277]: the trait bound `String: ImportantTrait<i32>` is not satisfied
48+
--> src/main.rs:12:18
49+
|
50+
12 | use_my_trait(String::new());
51+
| ------------ ^^^^^^^^^^^^^ the trait `ImportantTrait<i32>` is not implemented for `String`
52+
| |
53+
| required by a bound introduced by this call
54+
|
55+
```
56+
57+
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.)
58+
59+
```
60+
error[E0277]: My Message for `ImportantTrait<i32>` is not implemented for `String`
61+
--> src/main.rs:12:18
62+
|
63+
12 | use_my_trait(String::new());
64+
| ------------ ^^^^^^^^^^^^^ My Label
65+
| |
66+
| required by a bound introduced by this call
67+
|
68+
= help: the trait `ImportantTrait<i32>` is not implemented for `String`
69+
= note: Note 1
70+
= note: Note 2
71+
```
72+
73+
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:
74+
75+
```rust
76+
#[diagnostic::on_unimplemented(
77+
message = "the size for values of type `{Self}` cannot be known at compilation time",
78+
label = "doesn't have a size known at compile-time"
79+
)]
80+
pub trait Sized {}
81+
```
82+
83+
For more information, see the reference section on [the `diagnostic` tool attribute namespace](https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace).
84+
85+
### Asserting `unsafe` preconditions
86+
87+
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.
88+
89+
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.
90+
91+
For example, [`slice::from_raw_parts`](https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html) 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:
92+
93+
```rust
94+
fn main() {
95+
let slice: &[u8] = &[1, 2, 3, 4, 5];
96+
let ptr = slice.as_ptr();
97+
98+
// Create an offset from `ptr` that will always be one off from `u16`'s correct alignment
99+
let i = usize::from(ptr as usize & 1 == 0);
100+
101+
let slice16: &[u16] = unsafe { std::slice::from_raw_parts(ptr.add(i).cast::<u16>(), 2) };
102+
dbg!(slice16);
103+
}
104+
```
105+
106+
```
107+
thread 'main' panicked at library/core/src/panicking.rs:220:5:
108+
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`
109+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
110+
thread caused non-unwinding panic. aborting.
111+
```
112+
113+
### Deterministic realignment
114+
115+
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.
116+
117+
- [`pointer::align_offset`](https://doc.rust-lang.org/std/primitive.pointer.html#method.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.
118+
119+
- [`slice::align_to`](https://doc.rust-lang.org/std/primitive.slice.html#method.align_to) and [`slice::align_to_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.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.
120+
121+
### Stabilized APIs
122+
123+
- [`impl Read for &Stdin`](https://doc.rust-lang.org/stable/std/io/struct.Stdin.html#impl-Read-for-%26Stdin)
124+
- [Accept non `'static` lifetimes for several `std::error::Error` related implementations](https://github.com/rust-lang/rust/pull/113833/)
125+
- [Make `impl<Fd: AsFd>` impl take `?Sized`](https://github.com/rust-lang/rust/pull/114655/)
126+
- [`impl From<TryReserveError> for io::Error`](https://doc.rust-lang.org/stable/std/io/struct.Error.html#impl-From%3CTryReserveError%3E-for-Error)
127+
128+
These APIs are now stable in const contexts:
129+
130+
- [`Barrier::new()`](https://doc.rust-lang.org/stable/std/sync/struct.Barrier.html#method.new)
131+
132+
### Compatibility notes
133+
134+
* As [previously announced](https://blog.rust-lang.org/2024/02/26/Windows-7.html), Rust 1.78 has increased its minimum requirement to Windows 10 for the following targets:
135+
- `x86_64-pc-windows-msvc`
136+
- `i686-pc-windows-msvc`
137+
- `x86_64-pc-windows-gnu`
138+
- `i686-pc-windows-gnu`
139+
- `x86_64-pc-windows-gnullvm`
140+
- `i686-pc-windows-gnullvm`
141+
* Rust 1.78 has upgraded its bundled LLVM to version 18, completing the announced [`u128`/`i128` ABI change](https://blog.rust-lang.org/2024/03/30/i128-layout-update.html) 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.
142+
143+
### Other changes
144+
145+
Check out everything that changed in [Rust](https://github.com/rust-lang/rust/releases/tag/1.78.0), [Cargo](https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-178-2024-05-02), and [Clippy](https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-178).
146+
147+
## Contributors to 1.78.0
148+
149+
Many people came together to create Rust 1.78.0. We couldn't have done it without all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.78.0/)

0 commit comments

Comments
 (0)