Skip to content

Commit 08053c4

Browse files
committed
Rust 1.78.0 announcement
1 parent 4587ed7 commit 08053c4

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

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

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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 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).
74+
75+
### Asserting `unsafe` preconditions
76+
77+
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 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.
78+
79+
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.
80+
81+
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, _which may or may not cause noticeable ill effect otherwise_, but the debug assertion can now catch it:
82+
83+
```rust
84+
fn main() {
85+
let slice: &[u8] = &[1, 2, 3, 4, 5];
86+
let slice16: &[u16] = unsafe {
87+
let ptr = slice.as_ptr();
88+
let i = usize::from(ptr as usize & 1 == 0);
89+
std::slice::from_raw_parts(ptr.add(i) as *const u16, 2)
90+
};
91+
dbg!(slice16);
92+
}
93+
```
94+
95+
```
96+
thread 'main' panicked at library/core/src/panicking.rs:220:5:
97+
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`
98+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
99+
thread caused non-unwinding panic. aborting.
100+
```
101+
102+
### Deterministic realignment
103+
104+
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.
105+
106+
- [`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.
107+
108+
- [`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.
109+
110+
### Stabilized APIs
111+
112+
- [`impl Read for &Stdin`](https://doc.rust-lang.org/stable/std/io/struct.Stdin.html#impl-Read-for-%26Stdin)
113+
- [Accept non `'static` lifetimes for several `std::error::Error` related implementations](https://github.com/rust-lang/rust/pull/113833/)
114+
- [Make `impl<Fd: AsFd>` impl take `?Sized`](https://github.com/rust-lang/rust/pull/114655/)
115+
- [`impl From<TryReserveError> for io::Error`](https://doc.rust-lang.org/stable/std/io/struct.Error.html#impl-From%3CTryReserveError%3E-for-Error)
116+
117+
These APIs are now stable in const contexts:
118+
119+
- [`Barrier::new()`](https://doc.rust-lang.org/stable/std/sync/struct.Barrier.html#method.new)
120+
121+
### Compatibility notes
122+
123+
* 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:
124+
- `x86_64-pc-windows-msvc`
125+
- `i686-pc-windows-msvc`
126+
- `x86_64-pc-windows-gnu`
127+
- `i686-pc-windows-gnu`
128+
- `x86_64-pc-windows-gnullvm`
129+
- `i686-pc-windows-gnullvm`
130+
131+
### Other changes
132+
133+
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).
134+
135+
## Contributors to 1.78.0
136+
137+
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)