Skip to content

Confusing and big error when misusing function instead of function call in iterator chain #81604

@WaffleLapkin

Description

@WaffleLapkin

I've accidentally used a function (String::from) instead of a function call (String::from("...")) and the error was hard to read. Maybe it can be improved. (playground)

Code

fn test(media: &str) {
    std::iter::once((
        String::from, // <-- I've meant `String::from("...")`
        serde_json::Value::String(String::from("document")),
    ))
    .chain(
        std::iter::once((
            String::from("media"),
            serde_json::Value::String(media.to_owned()),
        ))
        .chain(
            Some("caption")
                .iter()
                .map(serde_json::to_value)
                .map(Result::unwrap)
                .map(|v| (String::from("caption"), v))
                .chain(
                    Some("parse_mode")
                        .iter()
                        .map(serde_json::to_value)
                        .map(Result::unwrap)
                        .map(|v| (String::from("parse_mode"), v)),
                ),
        ),
    )
    .collect::<Vec<_>>();
}

Error

(this error is from 1.51.0-nightly (2021-01-30 04caa632dd10c2bf64b6), previous compiler versions produce similar errors)

error[E0271]: type mismatch resolving `<std::iter::Chain<std::iter::Once<(std::string::String, Value)>, std::iter::Chain<std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:18:26: 18:58]>, std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:24:34: 24:69]>>> as IntoIterator>::Item == (fn(_) -> std::string::String {<std::string::String as From<_>>::from}, Value)`
 --> src/lib.rs:8:10
  |
8 |         .chain(
  |          ^^^^^ expected struct `std::string::String`, found fn item
  |
  = note: expected type `(std::string::String, Value)`
            found tuple `(fn(_) -> std::string::String {<std::string::String as From<_>>::from}, Value)`

error[E0599]: the method `collect` exists for struct `std::iter::Chain<std::iter::Once<(fn(_) -> std::string::String {<std::string::String as From<_>>::from}, Value)>, std::iter::Chain<std::iter::Once<(std::string::String, Value)>, std::iter::Chain<std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:18:26: 18:58]>, std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:24:34: 24:69]>>>>`, but its trait bounds were not satisfied
  --> src/lib.rs:28:10
   |
28 |           .collect(),
   |            ^^^^^^^ method cannot be called on `std::iter::Chain<std::iter::Once<(fn(_) -> std::string::String {<std::string::String as From<_>>::from}, Value)>, std::iter::Chain<std::iter::Once<(std::string::String, Value)>, std::iter::Chain<std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:18:26: 18:58]>, std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:24:34: 24:69]>>>>` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `<std::iter::Chain<std::iter::Once<(std::string::String, Value)>, std::iter::Chain<std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:18:26: 18:58]>, std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:24:34: 24:69]>>> as Iterator>::Item = (fn(_) -> std::string::String {<std::string::String as From<_>>::from}, Value)`
           which is required by `std::iter::Chain<std::iter::Once<(fn(_) -> std::string::String {<std::string::String as From<_>>::from}, Value)>, std::iter::Chain<std::iter::Once<(std::string::String, Value)>, std::iter::Chain<std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:18:26: 18:58]>, std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:24:34: 24:69]>>>>: Iterator`
           `std::iter::Chain<std::iter::Once<(fn(_) -> std::string::String {<std::string::String as From<_>>::from}, Value)>, std::iter::Chain<std::iter::Once<(std::string::String, Value)>, std::iter::Chain<std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:18:26: 18:58]>, std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:24:34: 24:69]>>>>: Iterator`
           which is required by `&mut std::iter::Chain<std::iter::Once<(fn(_) -> std::string::String {<std::string::String as From<_>>::from}, Value)>, std::iter::Chain<std::iter::Once<(std::string::String, Value)>, std::iter::Chain<std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:18:26: 18:58]>, std::iter::Map<std::iter::Map<std::iter::Map<std::option::Iter<'_, &str>, fn(&&str) -> std::result::Result<Value, serde_json::Error> {to_value::<&&str>}>, fn(std::result::Result<Value, serde_json::Error>) -> Value {std::result::Result::<Value, serde_json::Error>::unwrap}>, [closure@src/lib.rs:24:34: 24:69]>>>>: Iterator`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions