Rust 1.100 の !: Default is not satisfied と box パターンの直し方

PR
Rust 1.100 の !: Default is not satisfied と box パターンの直し方
この記事は約22分で読めます。

Rust を 1.100 に上げて cargo run すると、「error[E0277]: the trait bound !: Default is not satisfied」で止まることがあります。当たるのは、Cargo.toml の edition が 2015・2018・2021 のコードです。"2024" なら、fallback は前から ! です。エラーが「box patterns have been removed」なら、下の「box patterns have been removed は、cfg で外したコードでも出る」の節を見てください。

never 型(!)は、届かないコードを表す型です。! から読み替えた先の型が決まらないとき、コンパイラは代わりの型(fallback)を使います。この仕組みが never type fallback です。Edition Guide によると、! は常に届かないコードを表すので、どんな型に読み替えても安全です。Rust 1.100.0(2026年11月12日)から、この fallback がエディションを問わず ! になりました(2015・2018・2021 では、これまで () でした)。下の見本の make()?; のように結果を捨てると、T が何の型か決まりません。そのときコンパイラが入れる型が、これまでは ()、1.100 からは ! です。! には Default が無いので、E0277 になります。

直し方は、型が決まらずに ! になった所へ、() と型を書くことです(make()?; を make::<()>()?; に)。1.99.1 でも 1.100.0 でも通りました(試した結果)。どこで落ちたかは、エラーの --> の行で見ます。下の見本では --> src/main.rs:6:5 で、自分のコードです。依存の crate(ライブラリ)の中を指しているときは、直しがもう出ているかもしれないので、まず依存の crate を上げてみます。

2026年11月28日時点で、1.100.1 が 2026年11月27日に出ています。上げるなら 1.100.1 です。1.100.1 の変更(RELEASES.md)は、Iterator の実装があるのに、無いと見なされる退行(前の版で通っていた物が通らなくなる不具合)の直しで、never 型には触れていません。公式の告知は rustup update stable で上げると書いています。入った版は rustc --version で見ます(The Rust Programming Language)。

rustup update stable
rustc --version

rustc --version が前の版のままなら、rustup default で既定のツールチェーンを見ます(→ 試した環境)。この記事の出力は 1.100.0 の物で、1.100.1 では流していません。

手元で流した見本のうち、1.99.1 で通り 1.100.0 で落ちたのは、#![allow(dependency_on_unit_never_type_fallback)] で lint(コンパイラの注意)を黙らせていたコードと、#[cfg(any())] で外した関数の中の box パターンの 2 つでした(試した結果。Linux・edition 2021)。lint を黙らせていない同じコードは、1.99.1 でも別の文(「this function depends on never type fallback being ()」)で止まっていました。見本と出力は、次の 2 つの節にあります。

この記事でわかること
  • 1.100 で、never 型の fallback がエディションを問わず ! になった(2024 は前から)
  • 「!: Default is not satisfied」のエラーは、型を () と書けば通る(1.99.1・1.100.0 とも。試した結果)
  • 1.99.1 で通り 1.100.0 で落ちたのは、lint を黙らせていたコードと、cfg で外した box パターン(手元で流した 2 つ)
  • cfg で外したままの box パターンは、box を取れば通る。ビルドに入れると型が合わない
  • 上げるなら 1.100.1(手元で流したのは 1.100.0)

the trait bound !: Default is not satisfied は何? never 型の fallback が ! に変わった

公式の blog は、この変更を次のように書いています。

This release changes the "never type fallback" from () to ! in editions before 2024 (i.e. 2015, 2018, and 2021), similarly to what has already been done in edition 2024. This should largely be unnoticeable, but can affect code in some rare cases (for example when ignoring a result of a function returning a generic parameter).

訳すと「このリリースは、2024 より前のエディション(2015・2018・2021)で、never type fallback を () から ! に変える。2024 ですでにそうなっているのと同じ。ほとんど気づかないはずだが、まれに影響する(たとえば、型引数を返す関数の結果を捨てるとき)」です。続けて、コンパイラが届かないコードを前より多く見つけることもある、とあります。

never 型は 1.100 で stable になり、Infallible は ! の別名になりました。blog は、Infallible を変えるために、この互換性を壊す変更を入れたと書いています。Edition Guide は、1.100 からエディションを問わず ! に fallback し、移行を扱っていた lint dependency_on_unit_never_type_fallback も取り除かれた、と書いています。

見本 A は、型引数を返す関数の結果を ? で受けて捨てる形です。Cargo.toml は次のとおりです(edition = "2021")。

[package]
name = "never"
version = "0.1.0"
edition = "2021"

src/main.rs は次のとおりです。

fn make<T: Default>() -> Result<T, ()> {
    Ok(T::default())
}

fn run() -> Result<(), ()> {
    make()?;
    Ok(())
}

fn main() {
    run().unwrap();
    println!("ok");
}

見本 B は、見本 A の頭に #![allow(dependency_on_unit_never_type_fallback)] の 1 行と空行を足した物です。直した形は、見本 A の 6 行目を make::<()>()?; に変えた物です。試した結果(2026年11月28日・Docker の Linux・edition 2021)は、次のとおりです。

見本1.99.11.100.0
見本 A(そのまま)落ちた(lint)落ちた(E0277)
見本 B(#![allow] あり)通った(warning)落ちた(E0277)
直した形(make::<()>()?;)通った通った

表の文は、次のとおりです。1.99.1 の見本 A は「this function depends on never type fallback being ()」で止まりました。1.99.1 の見本 B は、未来の版で拒まれるという warning が出て通りました。1.100.0 は「error[E0277]: the trait bound !: Default is not satisfied」で落ち、見本 B では lint が消えたという warning も出ました。

1.100.0 の見本 A の出力は、次のとおりです(終了コード 101)。

   Compiling never v0.1.0 (/app)
error[E0277]: the trait bound `!: Default` is not satisfied
 --> src/main.rs:6:5
  |
6 |     make()?;
  |     ^^^^^^ the trait `Default` is not implemented for `!`
  |
  = note: this error might have been caused by changes to Rust's type-inference algorithm (see issue #148922 <https://github.com/rust-lang/rust/issues/148922> for more information)
  = help: you might have intended to use the type `()` here instead
note: required by a bound in `make`
 --> src/main.rs:1:12
  |
1 | fn make<T: Default>() -> Result<T, ()> {
  |            ^^^^^^^ required by this bound in `make`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `never` (bin "never") due to 1 previous error
exit=101

1.100.0 の見本 B の出力は、次のとおりです(終了コード 101)。

   Compiling never_allow v0.1.0 (/app)
warning: lint `dependency_on_unit_never_type_fallback` has been removed: the code warned by this lint no longer compiles
 --> src/main.rs:1:10
  |
1 | #![allow(dependency_on_unit_never_type_fallback)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(renamed_and_removed_lints)]` on by default

error[E0277]: the trait bound `!: Default` is not satisfied
 --> src/main.rs:8:5
  |
8 |     make()?;
  |     ^^^^^^ the trait `Default` is not implemented for `!`
  |
  = note: this error might have been caused by changes to Rust's type-inference algorithm (see issue #148922 <https://github.com/rust-lang/rust/issues/148922> for more information)
  = help: you might have intended to use the type `()` here instead
note: required by a bound in `make`
 --> src/main.rs:3:12
  |
3 | fn make<T: Default>() -> Result<T, ()> {
  |            ^^^^^^^ required by this bound in `make`

For more information about this error, try `rustc --explain E0277`.
warning: `never_allow` (bin "never_allow") generated 1 warning
error: could not compile `never_allow` (bin "never_allow") due to 1 previous error; 1 warning emitted
exit=101

見本 B の warning の文は、「lint dependency_on_unit_never_type_fallback has been removed: the code warned by this lint no longer compiles」です。訳すと「lint dependency_on_unit_never_type_fallback は取り除かれた。この lint が警告していたコードは、もうコンパイルできない」です。lint を #![allow] で黙らせても、1.100 では落ちるのを止められません。ただし、黙らせていた全部のコードが落ちるかは、見本 B 1 つでしか試していません。

依存の crate が never 型で落ちるときの文は、blog にあります。

If a dependency is failing due to issues with the never type, consider updating it, as a fix might have already been released for it.

訳すと「依存の crate が never 型の問題で落ちているなら、更新を考える。直しがもう出ているかもしれない」です。依存の crate の中のコードは、試していません。

1.99.1 では別の文で止まっていた

見本 A は、1.99.1 でも落ちました(終了コード 101)。deny(既定でエラー)の lint dependency_on_unit_never_type_fallback で止まり、help は make::<()>()?; を案内しています。1.99.1 の出力は、次のとおりです。

   Compiling never v0.1.0 (/app)
error: this function depends on never type fallback being `()`
 --> src/main.rs:5:1
  |
5 | fn run() -> Result<(), ()> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: specify the types explicitly
note: in edition 2024, the requirement `!: Default` will fail
 --> src/main.rs:6:5
  |
6 |     make()?;
  |     ^^^^^^
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
  = note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
  = note: `#[deny(dependency_on_unit_never_type_fallback)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
help: use `()` annotations to avoid fallback changes
  |
6 |     make::<()>()?;
  |         ++++++

error: could not compile `never` (bin "never") due to 2 previous errors
exit=101

見本 B は、1.99.1 で通りました(終了コード 0)。「will be rejected by a future version of Rust」(未来の版で拒まれる)という warning の行が、黙らせていても出ていた合図です。

   Compiling never_allow v0.1.0 (/app)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
warning: the following packages contain code that will be rejected by a future version of Rust: never_allow v0.1.0 (/app)
note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
     Running `/tmp/t/debug/never_allow`
ok
exit=0

box patterns have been removed は、cfg で外したコードでも出る

#[cfg(any())] を付けた関数(ビルドから外れる関数)の中に、box パターンがある見本です。

#[cfg(any())]
fn unused(b: Box<i32>) -> i32 {
    let box x = b;
    x
}

fn main() {
    println!("ok");
}

試した結果(cargo run -q。2026年11月28日・Docker の Linux・edition 2021)は、1.99.1 が「warning: box pattern syntax is experimental」を出して通り(終了コード 0)、1.100.0 が「error: box patterns have been removed (feature box_patterns)」で落ちました(終了コード 101)。1.100.0 の出力は、次のとおりです。

error: `box` patterns have been removed (feature `box_patterns`)
 --> src/main.rs:3:9
  |
3 |     let box x = b;
  |         ^^^
  |
  = help: enable feature `deref_patterns` instead and...
help: ...if possible just remove keyword `box`...
  |
3 -     let box x = b;
3 +     let x = b;
  |
help: ...otherwise replace it with an invocation of macro `deref`
  |
3 -     let box x = b;
3 +     let deref!(x) = b;
  |

error: could not compile `boxpat` (bin "boxpat") due to 1 previous error
exit=101

1.99.1 の warning は、box パターンを experimental(試験中の機能)と呼んでいます。stable で動くコードに box パターンを書けたわけではありません。外したコードの中だけの話です。外していない box パターンは、流していません。

直し方です。#[cfg(any())] で外したままのコードなら、box を取ると 1.99.1・1.100.0 とも通りました(試した結果)。ただし box を取ると x は Box<i32> のままです。cfg を外してビルドに入れたときは型が合わず、「error[E0308]: mismatched types」で落ちました(試した結果。次の出力は let box x = b; を let x = b; に変えて、#[cfg(any())] を外し、main から呼んだ形で、1.99.1 と 1.100.0 で同じでした)。

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
1 | fn unused(b: Box<i32>) -> i32 {
  |                           --- expected `i32` because of return type
2 |     let x = b;
3 |     x
  |     ^ expected `i32`, found `Box<i32>`
  |
  = note: expected type `i32`
           found struct `Box<i32>`
help: consider unboxing the value
  |
3 |     *x
  |     +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `boxlive` (bin "boxlive") due to 1 previous error
exit=101

help は *x を案内していますが、*x に直した形は流していません。help は、deref_patterns を有効にする案から続けて、「...if possible just remove keyword box...」と案内しています。この記事では、box を取る形だけを扱います。help の deref の 2 つは、stable で使えるかを確かめていません。

RELEASES.md の Compatibility Notes は、この変更を次のように書いています。

box patterns are no longer syntactically allowed

訳すと「box パターンは、構文として書けなくなった」です。

Compatibility Notes のほかの項は? 手元で再現しなかった物と確かめていない物

1.100.0 の Compatibility Notes(互換性の注意)は 20 項あります。ここでは、ふつうのアプリを書く人が当たりうる物だけを挙げます。全部は RELEASES.md を見てください。

下の 4 項は、手元で落ちる例を作れなかったか、試していない項です。当たったら、エラー文を RELEASES.md の原文と見比べてください。

項手元では
if let のクロージャ再現しなかった
cargo install の lockfile確かめていない
Box の impl流していない
Infallibleこの項で落ちる例は流していない

表の項の原文と訳は、次のとおりです。

  • "Closures now more precisely capture variables used in if let. This may cause new errors"(クロージャが if let で使う変数を、前より細かく捕まえる。新しいエラーが出ることがある)。6 通り試して、1.100.0 だけで出た新しいエラーは 0 件でした。再現しなかったので、落ちる例は書きません
  • "cargo-install: now uses the existing lockfile in the .crate tarball by default. --locked in cargo install becomes a no-op."(cargo install が、.crate の中の lockfile を既定で使う。--locked は何もしなくなる)。実際の crate の install で違いを見ていないので、確かめていません
  • "Some impls on Box have been generalised to arbitrary allocators, which may cause new type inference errors"(Box の一部の impl が、どの allocator でも使える形になり、新しい型推論のエラーが出ることがある)。流していません
  • "Infallible is now a type alias to !"(Infallible が ! の別名になった)。この項で落ちる例は、流していません。1 つ目の節の never 型と同じ話です

--locked を付けて cargo install する例は、fnm を Cargo で入れた例にあります(その記事では --locked の効果は書いていません)。

試した環境と、試していないこと

比べたのは、Docker の公式の Rust のイメージ rust:1.99 と rust:1.100 です。Debian GNU/Linux 13・x86_64 で、Cargo.toml は edition = "2021"、流したのは 2026年11月28日です。流した形は、次のとおりです。

docker run --rm -v "$PWD":/app -w /app rust:1.100 sh -c 'cargo run 2>&1; echo "exit=$?"'

この枠は、手元で流した形から、手元の都合で付けた環境変数を 1 つ外した物です(外した形そのものは流していません)。box パターンの見本だけ、cargo run -q で流しました。

rustc --version の出力は、1.99 側が rustc 1.99.1 (58cf8f9c3 2026-10-13)、1.100 側が rustc 1.100.0 (0ef1bc386 2026-11-09) でした(試した結果)。

手元の Docker の rust:1.100 で rustup update stable を流したあとも、rustc --version は 1.100.0 のままでした。このイメージは既定のツールチェーンが stable ではなく 1.100.0 で(rustup default で 1.100.0-x86_64-unknown-linux-gnu (default))、rustup default stable のあとに rustc 1.100.1 (20ddd0171 2026-11-25) になりました(試した結果)。rustup 自体の版の話は、rustup を 1.29.2 以上に上げる話(認証局の鍵の問題)にあります。

rustup の文書には、"rustup uses the stable channel by default, which represents the latest release of Rust. Stable publishes new releases every six weeks." とあります。訳すと「rustup は既定で stable の channel(配布の系統)を使い、それが最新のリリースを指す。stable は 6 週ごとに新しいリリースを出す」です。

試していない物は、Windows・macOS・rustup を直に入れた環境・OS のパッケージの Rust・edition 2015 と 2018・1.100.1・依存の crate の中のコードです。

よくある質問

1.100 に上げたら「the trait bound !: Default is not satisfied」と出ました

型が決まらずに ! になった所へ、() と型を書きます(make::<()>()?;)。1.99.1 でも 1.100.0 でも通りました(試した結果)。エラーが依存の crate の中を指しているなら、直しがもう出ているかもしれないので、まず依存の crate を上げてみます。

1.99 では通っていたのに、なぜ?

手元の見本で 1.99.1 で通ったのは、#![allow(dependency_on_unit_never_type_fallback)] で lint を黙らせていた見本 B です。黙らせていない見本 A は、1.99.1 でも別の文で落ちました。

「lint dependency_on_unit_never_type_fallback has been removed」と出ました

この lint は、1.100 で取り除かれました。#![allow(dependency_on_unit_never_type_fallback)] を消して、() と型を書きます。黙らせても、1.100 ではコードが落ちるのを止められません(見本 B。試した結果)。

「box patterns have been removed」と出ました

#[cfg(any())] で外したままのコードなら、box を取ると通ります(let box x = b; を let x = b; に。試した結果)。ビルドに入れるコードでは、x が Box<i32> のままなので、型が合わずに落ちることがあります。

逆に、1.100 で通るようになった例

ここは壊れる例ではありません。if let で user.name を使うクロージャを持ったまま、user.log を変える見本です。

struct User {
    name: Option<String>,
    log: String,
}

fn main() {
    let mut user = User { name: Some(String::from("taro")), log: String::new() };
    let len = || {
        if let User { name: Some(ref n), .. } = user {
            n.len()
        } else {
            0
        }
    };
    user.log.push_str("checked");
    println!("{} {}", len(), user.log);
}

試した結果、1.99.1 は「error[E0502]: cannot borrow user.log as mutable because it is also borrowed as immutable」で落ち、1.100.0 は 4 checked と出て通りました(終了コード 0)。1.99.1 の出力は、次のとおりです。

   Compiling closure2 v0.1.0 (/app)
error[E0502]: cannot borrow `user.log` as mutable because it is also borrowed as immutable
  --> src/main.rs:15:5
   |
 8 |     let len = || {
   |               -- immutable borrow occurs here
 9 |         if let User { name: Some(ref n), .. } = user {
   |                                                 ---- first borrow occurs due to use of `user` in closure
...
15 |     user.log.push_str("checked");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
16 |     println!("{} {}", len(), user.log);
   |                       --- immutable borrow later used here

For more information about this error, try `rustc --explain E0502`.
error: could not compile `closure2` (bin "closure2") due to 1 previous error
exit=101

1.100.0 は、user 全体でなく user.name だけを捕まえていると読めます(読みです。コンパイラの捕まえ方は、出力では確かめていません)。

まとめ

  • Cargo.toml の edition を見ること。2015・2018・2021 は、1.100 から never type fallback が ! になる。2024 は前から !
  • 「!: Default is not satisfied」のエラーは、型が決まらずに ! になった所へ、() と型を書くこと
  • lint dependency_on_unit_never_type_fallback は消えたので、#![allow] を残さないこと
  • エラーが依存の crate の中を指すときは、まず依存の crate を上げてみること
  • cfg で外したままの box パターンは、box を取ること。ビルドに入れるコードでは、型が合うかを確かめること
  • 上げるなら 1.100.1。手元で流したのは 1.100.0 で、1.100.1 では流していない

参考資料

この記事の内容は、2026年11月28日の 6時27分〜6時46分ごろ(日本時間)に開いた資料と、手元の出力をもとにしています。

※この記事は、2026年11月28日時点の公式の資料と照らして確かめています。誤りに気づいたらお問い合わせからお知らせください。

PR
PR
タイトルとURLをコピーしました