Introducing dependency causes E0282, E0283 in project

I'm contributing a feature in a codebase that I don't own. Part of the changeset requires that I add a new dependency.

Both project and dependency independently compile and run tests successfully. But when I add the dependency to the project I'm working in and simply import the module anywhere (e.g. use dependency;), the test compilation in the project starts failing in unrelated files with the type annotations needed error.

I've gathered that this is because the project I'm working in depends on the ecow package, and the dependency I'm introducing depends the rkvy package. Here's an example of an error:

error[E0283]: type annotations needed
   --> compiler-core/src/
297 |     assert_eq!(config.locked(None).unwrap(), [].into());
    |     --------------------------------------------^^^^--- type must be known at this point
    = note: multiple `impl`s satisfying `std::collections::HashMap<ecow::EcoString, hexpm::version::Version>: PartialEq<_>` found in the following crates: `rkyv`, `std`:
            - impl<K, V, AK, AV> PartialEq<rkyv::collections::hash_map::ArchivedHashMap<AK, AV>> for std::collections::HashMap<K, V>
              where K: Hash, K: std::cmp::Eq, K: Borrow<AK>, AK: Hash, AK: std::cmp::Eq, AV: PartialEq<V>;
            - impl<K, V, S> PartialEq for std::collections::HashMap<K, V, S>
              where K: std::cmp::Eq, K: Hash, V: PartialEq, S: BuildHasher;

I don't quite know how to work around this, or what the correct action is to take. Is there a way I can intro this new dependency without having to resolve all of these trait ambiguities individually?

Appreciate the help!

In general the answer is to specify the type. This is usually easiest to do by using from() instead of into():

assert_eq!(config.locked(None).unwrap(), WhateverTypeItShouldBe::from([]));

But because the array is empty, its type is also ambiguous. You might need to declare a temporary variable so you can specify the type of that. (Or use [] as [ElementType; 0], but I'm not a fan of writing ases because as does so many different things.)

1 Like

The problematic line (you should include this so I don't have to go searching for it):

Rkyv essentially reimplements the standard library as zero-copy types. The issue here is that since rkyv's types can be compared with == to the standard library types, rust doesn't know if it should turn [] into rkyv's HashMap or std's HashMap. It only worked before because there was only one type comparable to HashMap.

In this case, you can put HashMap::new() or HashMap::from([]).

Thanks, my general question was "is there I way I can do this without having to go clear up all these ambiguities in the code base" and the answer appears to be "no". That's fine, I'll depend on another lib.

(you should include this so I don't have to go searching for it)

The question is self contained. There's no need to go look at the codebase in question to provide a answer.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.