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

Weird error explanation, when returning closures from match #79277

Closed
timcryt opened this issue Nov 21, 2020 · 1 comment
Closed

Weird error explanation, when returning closures from match #79277

timcryt opened this issue Nov 21, 2020 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@timcryt
Copy link

timcryt commented Nov 21, 2020

I tried this code:

use std::collections::HashMap;
use std::fmt::Debug;

fn main() {
    let a: HashMap<String, Box<dyn Debug>> = HashMap::new();
    let b = "hello";
    println!(
        "{:?}",
        (match &b[0..=0] {
            "h" => Box::new(|x| a.get_mut(x).unwrap() as &Box<dyn Debug>)
                as Box<dyn FnMut(&str) -> &Box<dyn Debug>>,
            "H" => Box::new(|x| a.get(x).unwrap()),
            _ => unreachable!(),
        })("world")
    );
}

I expected to see this happen: Compilation error with logical explanation.

Instead, this happened: Сompilation error with weird explanation.

error[E0308]: mismatched types
  --> src/main.rs:10:20
   |
10 |             "h" => Box::new(|x| a.get_mut(x).unwrap() as &Box<dyn Debug>)
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected reference `&Box<dyn Debug>`
              found reference `&Box<dyn Debug>`

error[E0308]: mismatched types
  --> src/main.rs:10:20
   |
10 |             "h" => Box::new(|x| a.get_mut(x).unwrap() as &Box<dyn Debug>)
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected type `FnOnce<(&str,)>`
              found type `FnOnce<(&str,)>`

error[E0308]: mismatched types
  --> src/main.rs:9:9
   |
9  | /         (match &b[0..=0] {
10 | |             "h" => Box::new(|x| a.get_mut(x).unwrap() as &Box<dyn Debug>)
11 | |                 as Box<dyn FnMut(&str) -> &Box<dyn Debug>>,
12 | |             "H" => Box::new(|x| a.get(x).unwrap()),
13 | |             _ => unreachable!(),
14 | |         })("world")
   | |__________^ one type is more general than the other
   |
   = note: expected type `FnOnce<(&str,)>`
              found type `FnOnce<(&str,)>`

error: aborting due to 3 previous errors

Meta

The same error message on both stable (1.48.0), beta (1.49.0-beta.1) and nightly (2020-11-20)

@timcryt timcryt added the C-bug Category: This is a bug. label Nov 21, 2020
@jyn514
Copy link
Member

jyn514 commented Nov 21, 2020

Duplicate of #41078

@jyn514 jyn514 marked this as a duplicate of #41078 Nov 21, 2020
@jyn514 jyn514 closed this as completed Nov 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants