rust-lang / rust-lang/rust

Terse diagnostic for never type fallback lint warning involving try operator and placeholder in path

Open
#132,358 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-lints D-terse L-dependency_on_unit_never_type_fallback T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Compiling the following function warns in Rust 1.81 and later:

pub fn foo() -> std::io::Result<()> {
    [1, 2, 3]
        .into_iter()
        .map(|_| -> std::io::Result<_> { Ok(()) })
        .collect::<std::io::Result<_>>()?;
    Ok(())
}

I expected it to compile without warnings, as it did with previous Rust versions. It currently produces this warning:

warning: this function depends on never type fallback being `()`
 --> src/lib.rs:1:1
  |
1 | pub fn foo() -> std::io::Result<()> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
  = help: specify the types explicitly
note: in edition 2024, the requirement `!: FromIterator<()>` will fail
 --> src/lib.rs:5:20
  |
5 |         .collect::<std::io::Result<_>>()?;
  |                    ^^^^^^^^^^^^^^^^^^
  = note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default

Playground

What is particularly perplexing about this warning is that I don't understand how the never type is even involved, as nothing in the code appears to diverge.

The current workaround is to add let () = ... before the iteration, which compiles without warnings:

// no warning
pub fn foo() -> std::io::Result<()> {
    let () = [1, 2, 3]
        .into_iter()
        .map(|_| -> std::io::Result<_> { Ok(()) })
        .collect::<std::io::Result<_>>()?;
    Ok(())
}

While this workaround works, it feels unnecessary and it's hard to explain and justify.

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.

Research direction

Reproduce the warning from the provided example in src/lib.rs, using the linked Playground if useful. Read issue #123748 for the never-type fallback context and investigate the compiler diagnostic path involved. Done means the reported behavior or its explanation is corrected for this example, with relevant regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.