Skip to content

Type mismatch in function arguments E0631, E0271 are falsely recognized as E0308 mismatched types #74400

Closed
@vandenheuvel

Description

@vandenheuvel

Regression from 1.45.0-beta.4 (2020-07-13 e99e6422a2f799fc86a3) to 1.46.0-nightly (2020-07-15 7e11379f3b4c376fbb9a).

The following code yields an incorrect error:

use std::convert::identity;

pub fn is_sorted_and_has_duplicates_by<T, S: PartialEq + PartialOrd>(
    data: &[T],
    key: impl Fn(&T) -> S,
) -> Option<&T> {
    unimplemented!()
}

pub fn is_sorted_and_has_duplicates<T: PartialEq + PartialOrd>(data: &[T]) -> Option<&T> {
    is_sorted_and_has_duplicates_by(data, identity)
}

On beta, this gives:

error[E0631]: type mismatch in function arguments
  --> src/lib.rs:11:43
   |
3  | pub fn is_sorted_and_has_duplicates_by<T, S: PartialEq + PartialOrd>(
   |        ------------------------------- required by a bound in this
4  |     data: &[T],
5  |     key: impl Fn(&T) -> S,
   |               ----------- required by this bound in `is_sorted_and_has_duplicates_by`
...
11 |     is_sorted_and_has_duplicates_by(data, identity)
   |                                           ^^^^^^^^
   |                                           |
   |                                           expected signature of `for<'r> fn(&'r T) -> _`
   |                                           found signature of `fn(_) -> _`

error[E0271]: type mismatch resolving `for<'r> <fn(_) -> _ {std::convert::identity::<_>} as std::ops::FnOnce<(&'r T,)>>::Output == _`
  --> src/lib.rs:11:5
   |
3  | pub fn is_sorted_and_has_duplicates_by<T, S: PartialEq + PartialOrd>(
   |        ------------------------------- required by a bound in this
4  |     data: &[T],
5  |     key: impl Fn(&T) -> S,
   |                         - required by this bound in `is_sorted_and_has_duplicates_by`
...
11 |     is_sorted_and_has_duplicates_by(data, identity)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected bound lifetime parameter, found concrete lifetime

On nightly, this:

error[E0308]: mismatched types
  --> src/lib.rs:11:5
   |
11 |     is_sorted_and_has_duplicates_by(data, identity)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected type `std::ops::FnOnce<(&T,)>`
              found type `std::ops::FnOnce<(&T,)>`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-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-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions