rust-lang / rust-lang/rust

[E0308] suggests u128::from of a Duration

Open
#158,208 6 comments 0 reactions 1 assignee View on GitHub

@JATIN-RAGHAV is already working on this.

Since Jun 22, 2026.

A-diagnostics D-confusing D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use std::time::Duration;

fn f(x: Duration, y: Duration, z: u128) {
    x.saturating_sub(y) / z;
}
Current output
error[E0308]: mismatched types
 --> src/lib.rs:4:27
  |
4 |     x.saturating_sub(y) / z;
  |                           ^ expected `u32`, found `u128`
  |
help: you can convert `x.saturating_sub(y)` from `u32` to `u128`, matching the type of `z`
  |
4 |     u128::from(x.saturating_sub(y)) / z;
  |     +++++++++++                   +
Desired output

Rationale and extra context
  1. The "expected u32" is suspicous. Is this from the fallback of integer types?
  2. The suggested fix results in another error, so that should be removed.
4 |     u128::from(x.saturating_sub(y)) / z;
  |     ^^^^ the trait `From<Duration>` is not implemented for `u128`

It seems like the suggestions only sees the saturating_sub without considering the types of x and y or something like that.

Other cases

Rust Version
rustc 1.96.0 (ac68faa20 2026-05-25)
binary: rustc
commit-hash: ac68faa20c58cbccd01ee7208bf3b6e93a7d7f96
commit-date: 2026-05-25
host: x86_64-unknown-linux-gnu
release: 1.96.0
LLVM version: 22.1.2
Anything else?

Also happens with 1.94.0 and nightly 2026-06-20.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.