Rust 1.41.1 发布公告

2020年2月27日 · Rust 发布团队

Rust 团队发布了 Rust 的新补丁版本 1.41.1。 Rust 是一种编程语言,旨在让每个人都能构建可靠且高效的软件。

如果您之前通过 rustup 安装了 Rust,则获取 Rust 1.41.1 非常简单,只需运行以下命令:

$ rustup update stable

如果您还没有安装,您可以从我们网站的相应页面获取 rustup

1.41.1 稳定版中的内容

Rust 1.41.1 解决了 Rust 1.41.0 中引入的两个关键回归问题:一个与静态生命周期相关的健全性漏洞,以及一个导致段错误的错误编译。这些回归问题不会影响早期的 Rust 版本,我们建议 Rust 1.41.0 的用户尽快升级。 此外,此版本还解决了与 'staticCopy 实现之间的交互相关的另一个问题,该问题可追溯到 Rust 1.0。

检查 static 项时的健全性漏洞

在 Rust 1.41.0 中,由于 static 值的内部表示的一些更改,借用检查器意外地允许了一些不健全的程序。具体来说,借用检查器不会检查 static 项是否具有正确的类型。 这反过来又允许将具有小于 'static 生命周期 的临时变量赋值给 static 变量。

static mut MY_STATIC: &'static u8 = &0;

fn main() {
    let my_temporary = 42;
    unsafe {
        // Erroneously allowed in 1.41.0:
        MY_STATIC = &my_temporary;
    }
}

这个问题在 1.41.1 中得到解决,程序将无法编译

error[E0597]: `my_temporary` does not live long enough
 --> src/main.rs:6:21
  |
6 |         MY_STATIC = &my_temporary;
  |         ------------^^^^^^^^^^^^^
  |         |           |
  |         |           borrowed value does not live long enough
  |         assignment requires that `my_temporary` is borrowed for `'static`
7 |     }
8 | }
  | - `my_temporary` dropped here while still borrowed

您可以在issue #69114修复此问题的 PR中了解有关此错误的更多信息。

Copy 实现中遵守 'static 生命周期

自 Rust 1.0 以来,以下错误程序一直在编译

#[derive(Clone)]
struct Foo<'a>(&'a u32);
impl Copy for Foo<'static> {}

fn main() {
    let temporary = 2;
    let foo = (Foo(&temporary),);
    drop(foo.0); // Accessing a part of `foo` is necessary.
    drop(foo.0); // Indexing an array would also work.
}

在 Rust 1.41.1 中,此问题通过与上述相同的 PR修复。 现在编译程序会产生以下错误

error[E0597]: `temporary` does not live long enough
  --> src/main.rs:7:20
   |
7  |     let foo = (Foo(&temporary),);
   |                    ^^^^^^^^^^ borrowed value does not live long enough
8  |     drop(foo.0);
   |          ----- copying this value requires that
   |                `temporary` is borrowed for `'static`
9  |     drop(foo.0);
10 | }
   | - `temporary` dropped here while still borrowed

出现此错误是因为对于某个 'aFoo<'a> 仅在 'a: 'static 时才实现 Copy。 然而,具有某个生命周期 '0temporary 变量的生命周期不长于 'static,因此 Foo<'0> 不是 Copy,因此第二次使用 drop 应该是一个错误。

错误编译的边界检查导致段错误

在少数情况下,使用 Rust 1.41.0 编译的程序会忽略内存分配代码中的边界检查。 如果提供了超出边界的值,这将导致段错误。 错误编译的根本原因是 LLVM 优化过程中的更改,该更改在 LLVM 9 中引入,并在 LLVM 10 中还原。

Rust 1.41.0 使用了 LLVM 9 的快照,因此我们将还原操作 cherry-pick 到 Rust 1.41.1 中,从而解决了错误编译的问题。您可以在 issue #69225 中了解有关此错误的更多信息

1.41.1 的贡献者

许多人共同努力创建了 Rust 1.41.1。 没有你们大家,我们不可能完成这项工作。感谢!