Accidentally recursive trait requirement

Hi, I'm trying to write a Sub impl for references to Points. Everything works fine when types are given explicitly but then fails due to a recursive requirement when I try to write a generic function. The full code is here: Rust Playground

I'm new to Rust so any hints or insight would be helpful.

Thanks.

I believe this might be a bug in type inference. If you explicitly specify the parameters in the call, i.e. test::<i32, 2>(&a), then the code compiles. An important thing to note here is that the error message talks about &Point<BTreeSet<_>, {_: usize}> or &Point<HashSet<_, _>, {_: usize}> (depending on the choice of nightly vs stable). I don’t really see any reason of why type inference should start considering HashSet or BTreeSet for any of this, in fact, the type of &a should be very clearly known so it’s questionably that the explicit test::<i32, 2> should be needed to avoid any ambiguities or something like that.

3 Likes

Looks like #39959.

Edit: Maybe not exactly this one, I’m not sure if the examples given in this issues are actually expected to compile…

1 Like

That does look like what I'm encountering. Will work around the issue by cloning my values.