A puzzled "error[E0283]: type annotations required:" error


#1
#![feature(collections_bound)]
#![feature(collections)]
#![feature(btree_range)]

use std::collections::BTreeMap;
use std::collections::Bound::{Included, Excluded, Unbounded};
use std::collections::btree_map::Range;

type ID = [u8; 20];
// [u8; 20] impl Ord

trait Foo<K, V> {
    fn foo(&self, key :&K);
}

// // Error
impl<V> Foo<ID, V> for  BTreeMap<ID, V> {
    fn foo(&self, key: &ID) {
        let range: Range<ID, V> = Self::range(self, Unbounded, Included(key));
    }
}
 
// // // Work
// impl<K, V> Foo<K, V> for  BTreeMap<K, V>
//     where K: Ord{
//     fn foo(&self, key: &K) {
//         let range: Range<K, V> = Self::range(self, Unbounded, Included(key));
//     }
// }

fn main() {
    let mut map = BTreeMap::new();
    let key: ID = [124u8; 20];

    map.insert(key, "string");
    map.foo(&key);
}

#2

I think that’s a bug with type aliases, because if you define ID as follows, the code compiles:

#[derive(Ord, Eq, PartialOrd, PartialEq, Copy, Clone)]
struct ID([u8;20]);

#3

@oli_obk maybe type inference (or trait bound check) bug for array;

// // // Error
impl<V> Foo<[u8; 20], V> for  BTreeMap<[u8; 20], V> {
    fn foo(&self, key: &[u8; 20]) {
        let range: Range<[u8; 20], V> = Self::range(self, Unbounded, Included(key));
    }
}

// // // Work
// impl<V> Foo<u8, V> for  BTreeMap<u8, V> {
//     fn foo(&self, key: &u8) {
//         let range: Range<u8, V> = Self::range(self, Unbounded, Included(key));
//     }
// }


#6

Not bug, see https://github.com/rust-lang/rust/issues/35738