Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mismatched types, one type is more general than the other error shows types with the same name #77365

Closed
micouy opened this issue Sep 30, 2020 · 10 comments
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-typesystem Area: The type system C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@micouy
Copy link

micouy commented Sep 30, 2020

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
@micouy micouy added the C-bug Category: This is a bug. label Sep 30, 2020
@jyn514
Copy link
Member

jyn514 commented Sep 30, 2020

Possible duplicate of #76353

@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 30, 2020
@camelid camelid added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Sep 30, 2020
@camelid
Copy link
Member

camelid commented Sep 30, 2020

:)

// I can't think how to do better than this right now. -nikomatsakis

@camelid camelid added the A-closures Area: closures (`|args| { .. }`) label Sep 30, 2020
@micouy
Copy link
Author

micouy commented Oct 9, 2020

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);
}

@jyn514
Copy link
Member

jyn514 commented Oct 9, 2020

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

@camelid
Copy link
Member

camelid commented Oct 28, 2020

searched nightlies: from nightly-2019-01-01 to nightly-2020-10-28
regressed nightly: nightly-2020-06-24
searched commits: from 6bb3dbf to ff5b446
regressed commit: 1557fb0

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 

@camelid
Copy link
Member

camelid commented Oct 28, 2020

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 :/

@camelid
Copy link
Member

camelid commented Oct 28, 2020

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

@camelid camelid added the A-lifetimes Area: lifetime related label Oct 28, 2020
@camelid
Copy link
Member

camelid commented Oct 28, 2020

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}

@jyn514
Copy link
Member

jyn514 commented Oct 28, 2020

@camelid the previous error was because of #41078.

@matthiaskrgr
Copy link
Member

looks like another case of #75791

@jyn514 jyn514 closed this as completed Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-typesystem Area: The type system C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants