Rust: `mismatched types, one type is more general than the other` error shows types with the same name

Created on 30 Sep 2020  路  10Comments  路  Source: rust-lang/rust

Example code:

fn main() {
    let mut v: Vec<i32> = vec![];
    let f = |_| true;
    v.retain(f);
}

Error message:

error[E0308]: mismatched types
 --> src/main.rs:4:7
  |
4 |     v.retain(f);
  |       ^^^^^^ one type is more general than the other
  |
  = note: expected type `FnOnce<(&i32,)>`
             found type `FnOnce<(&i32,)>`

rustc version:

rustc 1.48.0-nightly (dbb73f8f7 2020-09-12)
binary: rustc
commit-hash: dbb73f8f79ab176a897d5a95e696adb71b957cbe
commit-date: 2020-09-12
host: x86_64-apple-darwin
release: 1.48.0-nightly
LLVM version: 11.0
A-closures A-diagnostics A-lifetimes A-typesystem C-bug D-incorrect T-compiler

All 10 comments

For those having the same problem - adding type annotations in the closure definition makes the error go away.

fn main() {
    let mut v: Vec<i32> = vec![];
    let f = |_: &i32| true; // now it compiles
    v.retain(f);
}

let f = |_: &_| true; will also work.

searched nightlies: from nightly-2019-01-01 to nightly-2020-10-28
regressed nightly: nightly-2020-06-24
searched commits: from https://github.com/rust-lang/rust/commit/6bb3dbfc6c6d8992d08431f320ba296a0c2f7498 to https://github.com/rust-lang/rust/commit/ff5b446d2fdbd898bc97a751f2f72858de185cf1
regressed commit: https://github.com/rust-lang/rust/commit/1557fb031b272b4c5bfcc7de5df7eddc7b36a584


bisected with cargo-bisect-rustc v0.6.0

Host triple: x86_64-apple-darwin
Reproduce with:

cargo bisect-rustc --preserve --prompt --start=2019-01-01 

This is an error -> very confusing error issue.

I actually don't understand the errors that used to be emitted. Can anyone else make sense of it?

error[E0631]: type mismatch in closure arguments
 --> src/main.rs:4:14
  |
3 |     let f = |_| true;
  |             -------- found signature of `fn(_) -> _`
4 |     v.retain(f);
  |              ^ expected signature of `for<'r> fn(&'r i32) -> _`

error[E0271]: type mismatch resolving `for<'r> <[closure@src/main.rs:3:13: 3:21] as std::ops::FnOnce<(&'r i32,)>>::Output == bool`
 --> src/main.rs:4:7
  |
4 |     v.retain(f);
  |       ^^^^^^ expected bound lifetime parameter, found concrete lifetime

error: aborting due to 2 previous errors

Unfortunately, the regression was in a rollup :/

The regression was in a rollup, but I think it's pretty likely that #73496 is the cause, so cc @estebank

Some debug logs that may be helpful:

2:rustcDEBUG rustc_typeck::check::writeback writeback: typeck results for DefId(0:3 ~ issue_77365[317d]::main) are TypeckResults {
2:rustc    hir_owner: DefId(0:3 ~ issue_77365[317d]::main),
2:rustc    type_dependent_defs: {
2:rustc        28: Ok(
2:rustc            (
2:rustc                AssocFn,
2:rustc                DefId(5:4646 ~ alloc[fd51]::vec::{impl#0}::retain),
2:rustc            ),
2:rustc        ),
2:rustc        13: Ok(
2:rustc            (
2:rustc                AssocFn,
2:rustc                DefId(5:4622 ~ alloc[fd51]::vec::{impl#0}::new),
2:rustc            ),
2:rustc        ),
2:rustc    },
2:rustc    field_indices: {},
2:rustc    node_types: {
2:rustc        20: [closure@src/test/ui/issues/issue-77365.rs:3:13: 3:21],
2:rustc        17: &i32,
2:rustc        14: std::vec::Vec<i32>,
2:rustc        11: std::vec::Vec<i32>,
2:rustc        5: i32,
2:rustc        2: [closure@src/test/ui/issues/issue-77365.rs:3:13: 3:21],
2:rustc        31: (),
2:rustc        28: (),
2:rustc        25: std::vec::Vec<i32>,
2:rustc        19: bool,
2:rustc        13: fn() -> std::vec::Vec<i32> {std::vec::Vec::<i32>::new},
2:rustc        4: std::vec::Vec<i32>,
2:rustc        1: std::vec::Vec<i32>,
2:rustc        30: (),
2:rustc        27: [closure@src/test/ui/issues/issue-77365.rs:3:13: 3:21],
2:rustc        21: [closure@src/test/ui/issues/issue-77365.rs:3:13: 3:21],
2:rustc        18: &i32,
2:rustc        15: std::vec::Vec<i32>,
2:rustc        3: &i32,
2:rustc    },
2:rustc    node_substs: {
2:rustc        28: [
2:rustc            i32,
2:rustc            [closure@src/test/ui/issues/issue-77365.rs:3:13: 3:21],
2:rustc        ],
2:rustc        13: [
2:rustc            i32,
2:rustc        ],
2:rustc    },
2:rustc    user_provided_types: {
2:rustc        4: Canonical {
2:rustc            max_universe: U0,
2:rustc            variables: [],
2:rustc            value: Ty(
2:rustc                std::vec::Vec<i32>,
2:rustc            ),
2:rustc        },
2:rustc        13: Canonical {
2:rustc            max_universe: U0,
2:rustc            variables: [
2:rustc                CanonicalVarInfo {
2:rustc                    kind: Ty(
2:rustc                        General(
2:rustc                            U0,
2:rustc                        ),
2:rustc                    ),
2:rustc                },
2:rustc                CanonicalVarInfo {
2:rustc                    kind: Ty(
2:rustc                        General(
2:rustc                            U0,
2:rustc                        ),
2:rustc                    ),
2:rustc                },
2:rustc            ],
2:rustc            value: TypeOf(
2:rustc                DefId(5:4622 ~ alloc[fd51]::vec::{impl#0}::new),
2:rustc                UserSubsts {
2:rustc                    substs: [
2:rustc                        ^0,
2:rustc                    ],
2:rustc                    user_self_ty: Some(
2:rustc                        UserSelfTy {
2:rustc                            impl_def_id: DefId(5:4620 ~ alloc[fd51]::vec::{impl#0}),
2:rustc                            self_ty: std::vec::Vec<^1>,
2:rustc                        },
2:rustc                    ),
2:rustc                },
2:rustc            ),
2:rustc        },
2:rustc    },
2:rustc    user_provided_sigs: {
2:rustc        DefId(0:4 ~ issue_77365[317d]::main::{closure#0}): Canonical {
2:rustc            max_universe: U0,
2:rustc            variables: [
2:rustc                CanonicalVarInfo {
2:rustc                    kind: Ty(
2:rustc                        General(
2:rustc                            U0,
2:rustc                        ),
2:rustc                    ),
2:rustc                },
2:rustc                CanonicalVarInfo {
2:rustc                    kind: Ty(
2:rustc                        General(
2:rustc                            U0,
2:rustc                        ),
2:rustc                    ),
2:rustc                },
2:rustc            ],
2:rustc            value: Binder(
2:rustc                ([^1_0]; c_variadic: false)->^1_1,
2:rustc            ),
2:rustc        },
2:rustc    },
2:rustc    adjustments: {
2:rustc        25: [
2:rustc            Borrow(Ref(ReErased, Mut { allow_two_phase_borrow: Yes })) -> &mut Vec<i32>,
2:rustc        ],
2:rustc    },
2:rustc    pat_binding_modes: {
2:rustc        1: BindByValue(
2:rustc            Mut,
2:rustc        ),
2:rustc        2: BindByValue(
2:rustc            Not,
2:rustc        ),
2:rustc    },
2:rustc    pat_adjustments: {},
2:rustc    upvar_capture_map: {},
2:rustc    closure_kind_origins: {},
2:rustc    liberated_fn_sigs: {
2:rustc        0: ([]; c_variadic: false)->(),
2:rustc        20: ([&i32]; c_variadic: false)->bool,
2:rustc    },
2:rustc    fru_field_types: {},
2:rustc    coercion_casts: {},
2:rustc    used_trait_imports: {},
2:rustc    tainted_by_errors: Some(
2:rustc        ErrorReported,
2:rustc    ),
2:rustc    concrete_opaque_types: {},
2:rustc    closure_captures: {},
2:rustc    generator_interior_types: [],
2:rustc}

@camelid the previous error was because of https://github.com/rust-lang/rust/issues/41078.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thestinger picture thestinger  路  234Comments

withoutboats picture withoutboats  路  308Comments

nikomatsakis picture nikomatsakis  路  412Comments

cramertj picture cramertj  路  512Comments

nikomatsakis picture nikomatsakis  路  274Comments