rust-lang / rust-lang/rust

The inference problem caused by `Option::filter` on function result

Open
#130,026 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference A-result-option C-discussion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

no error case:

pub fn true_pred<T>(_: T) -> bool {
    true
}

pub fn foo<T, F>(ele: &T, mut f: F) -> Option<& T>
where F: FnMut(&T) -> bool,
{
    [ele].into_iter()
        .fold(None, |acc, _| {
            acc.filter(|min| f(min))
        })
        //.filter(true_pred) // error!
}

error case:

pub fn true_pred<T>(_: T) -> bool {
    true
}

pub fn foo<T, F>(ele: &T, mut f: F) -> Option<& T>
where F: FnMut(&T) -> bool,
{
    [ele].into_iter()
        .fold(None, |acc, _| {
            acc.filter(|min| f(min))
        })
        .filter(true_pred) // error!
}

cargo check output:

error[E0308]: mismatched types
  --> src/lib.rs:8:5
   |
5  |   pub fn foo<T, F>(ele: &T, mut f: F) -> Option<& T>
   |              - found this type parameter ----------- expected `Option<&T>` because of return type
...
8  | /     [ele].into_iter()
9  | |         .fold(None, |acc, _| {
10 | |             acc.filter(|min| f(min))
11 | |         })
12 | |         .filter(true_pred) // error!
   | |__________________________^ expected `Option<&T>`, found `Option<T>`
   |
   = note: expected enum `Option<&_>`
              found enum `Option<_>`
help: try using `.as_ref()` to convert `Option<T>` to `Option<&T>`
   |
12 |         .filter(true_pred).as_ref() // error!
   |                           +++++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `test1` (lib) due to 1 previous error

rustc version:

rustc 1.82.0-nightly (1f12b9b0f 2024-08-27)
binary: rustc
commit-hash: 1f12b9b0fdbe735968ac002792a720f0ba4faca6
commit-date: 2024-08-27
host: aarch64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0

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 error from the examples in src/lib.rs with cargo check using the reported nightly rustc version, comparing the no-error and error cases. Then trace the compiler's type-inference handling for the Option::filter call and identify an appropriate compiler test location. Done means the behavior is understood and covered by a regression test, with the intended result confirmed.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.