Weird problems with recursion in type bounds in tests on libs

Hi there,

I've been playing around with rust implementing a linear algebra library and I've come across a weird problem.

Here is a snippet of my code for points and vectors: Rust Playground

As you can see, it works just fine. But if instead of a bin I try to put the same code in a lib and execute the main code in a test I get the following (I've also split it across files):

vec.rs:209:20: 209:29 error: overflow evaluating the requirement <&_ as std::ops::Add<_>>::Output [E0275]
vec.rs:209 assert_eq!(&p1 + &p2, exp_result);
vec.rs:209:20: 209:29 note: consider adding a #![recursion_limit="128"] attribute to your crate

The problem persist no even with recursion_limit="512".
What is happening?

Are there any other compiler errors? I got similar errors that seem to be a consequence of missing imports. Fixing the earlier errors causes the weird recursion errors to go away.

For reference, here's a playground link that reproduces the problem: Rust Playground

And here it is after the fix: Rust Playground

Not really. Only the usual unused variable stuff...

Also, I think if that was the case the error would also show in the bin version, right?

edit: some more information: ok, it seems like this will happen only in some contexts:
if I make a test function in the file the test will work fine, while if I make the test in a #[cfg(test)] mod or in a doc_test the error happens.

Can you post the complete code that produces the error, either as a multi-file gist or expanded into a single file (replace mod vec; with mod vec { /* contents of vec.rs */ })?

Note that when you move the test into a module or doctest, you'll need to add extra imports or change any paths it references from other modules.

Here you go:

Running on the playground will work fine, but executing the doc_test blows up the last two Add implementation for Vector.

I this shortened version I wasn't able to make the code break with #cfg(test) though...

Interesting! This fails for me with a runtime stack overflow, different from the compiler recursion limit that I hit before. But if I remove either of the last two Add impls, the other one works. It fails only if both are included.

I also found the problem goes away if I remove the Point trait and all references to it, even though as far as I can tell this should have no effect at runtime.

And missing imports still cause weird compile-time errors trying to instantiate types like Vector<Vector<Vector<Vector<...>>>>.

This appears very likely to be a compiler bug. I didn't find any existing issues that looked relevant, so it would be great if you could submit a new one.

Is there anyway the compiler can output what it's trying to instantiate during the recursion?

For reference, https://github.com/rust-lang/rust/issues/34137 was filed for tracking this issue.