Skip to content

Nightly fails to infer the type of iterator collect & impl IntoIterator, and gives confusing error message #93794

Closed
@wwylele

Description

@wwylele

Code

I tried this code:

fn foo(b: bool) -> impl IntoIterator<Item = ()> {
    if b {
        return vec![()];
    }
    [()].into_iter().collect()
}

I expected to see this happen: The code compiles (as it does on stable and beta). The compiler could infer the return type from the first return statement (Vec<()>), and successfully collect the iterator into it in the second return.

Instead, this happened:
The compiler seems to have trouble to infer the type of the second return, and gives confusing messages. Instead of explaining that it cannot infer the type (which can be understandable given some update in the library), it gives some serious complaint that the given type doesn't support this kind of operation.

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): a value of type `impl IntoIterator<Item = ()>` cannot be built from an iterator over elements of type `()`
    --> src/lib.rs:5:22
     |
5    |     [()].into_iter().collect()
     |                      ^^^^^^^ value of type `impl IntoIterator<Item = ()>` cannot be built from `std::iter::Iterator<Item=()>`
     |
     = help: the trait `FromIterator<()>` is not implemented for `impl IntoIterator<Item = ()>`
note: required by a bound in `collect`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error

Version it worked on

It most recently worked on: Stable version: 1.58.1, 1.59.0-beta.6(2022-02-02 0426998)

Version with regression

rustc --version --verbose:

rustc 1.60.0-nightly (0c292c966 2022-02-08)
binary: rustc
commit-hash: 0c292c9667f1b202a9150d58bdd2e89e3e803996
commit-date: 2022-02-08
host: x86_64-pc-windows-msvc
release: 1.60.0-nightly
LLVM version: 13.0.0

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged

Metadata

Metadata

Assignees

Labels

A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions