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

Inference fails because it doesn't consider deref coercions #37164

Open
cristicbz opened this issue Oct 14, 2016 · 2 comments
Open

Inference fails because it doesn't consider deref coercions #37164

cristicbz opened this issue Oct 14, 2016 · 2 comments
Labels
A-inference Area: Type inference C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@cristicbz
Copy link
Contributor

Maybe this is intended, but it's not super-clear.

Playground

use std::collections::HashMap;

fn check(_: &str) -> bool { false }

fn chain<'a>(some_key: &'a str) -> HashMap<&'a str, Vec<usize>> {
    let mut map = HashMap::new();
    map.get(&some_key);
    {
        // uncomment type annotation to fix
        let key_ref /*: &&str*/ = map.keys().next().unwrap();
        if check(key_ref) {} // due to autoderef `key_ref` could be `&&str` and
                             // this would work. but `key_ref` is inferred to be
                             // `&str` due to the `check` call, causing `map` to
                             // be `HashMap<str, _>`.
    }
    map
}

fn main() {}

fails with

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
 --> min.rs:6:19
  |
6 |     let mut map = HashMap::new();
  |                   ^^^^^^^^^^^^
  |
  = note: `str` does not have a constant size known at compile-time
  = note: required by `<std::collections::HashMap<K, V>>::new`

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
 --> min.rs:7:9
  |
7 |     map.get(&some_key);
  |         ^^^
  |
  = note: `str` does not have a constant size known at compile-time

error[E0277]: the trait bound `str: std::borrow::Borrow<&str>` is not satisfied
 --> min.rs:7:9
  |
7 |     map.get(&some_key);
  |         ^^^

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
 --> min.rs:8:18
  |
8 |     if check(map.keys().next().unwrap()) {}
  |                  ^^^^
  |
  = note: `str` does not have a constant size known at compile-time

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
 --> min.rs:8:25
  |
8 |     if check(map.keys().next().unwrap()) {}
  |                         ^^^^
  |
  = note: `str` does not have a constant size known at compile-time
  = note: required because of the requirements on the impl of `std::iter::Iterator` for `std::collections::hash_map::Keys<'_, str, _>`

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
 --> min.rs:8:14
  |
8 |     if check(map.keys().next().unwrap()) {}
  |              ^^^^^^^^^^^^^^^^^
  |
  = note: `str` does not have a constant size known at compile-time
  = note: required by `std::collections::hash_map::Keys`

error[E0308]: mismatched types
 --> min.rs:9:5
  |
9 |     map
  |     ^^^ expected reference, found str
  |
  = note: expected type `std::collections::HashMap<&'a str, std::vec::Vec<usize>>`
  = note:    found type `std::collections::HashMap<str, _>`

error: aborting due to 7 previous errors
@cristicbz cristicbz changed the title Inference fails because it doesn't consider auto-deref Inference fails because it doesn't consider deref coercions Oct 15, 2016
@cristicbz
Copy link
Contributor Author

@eddyb pointed out that this is very difficult in general, but my intuition (based on very little actual knowledge) is that the situation in this sort of case could be improved by leveraging the fact that we require full type signatures for functions.

In particular map could be inferred to be HashMap<&'a str, Vec<usize>> right off the bat using the return type; intuitively there seems to be an opportunity to propagate constraints backwards from return types.

All that said, I have little understanding of how type inference works in rust so... yeah, might be completely off the mark here.

@Mark-Simulacrum Mark-Simulacrum added the A-inference Area: Type inference label May 14, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 26, 2017
@steveklabnik
Copy link
Member

Triage: this still does not compile, unsure if it's intended to or not.

@jonas-schievink jonas-schievink added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants