Weird problems with recursion in type bounds in tests on libs


#1

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: https://play.rust-lang.org/?gist=0fd9a9d531baaef27c83366037bee60e&version=stable&backtrace=0

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?


Overflow evaluating the requirement `_: std::marker::Sized`
#2

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: https://play.rust-lang.org/?gist=14bbebfb830fe0c594a20be63064b711

And here it is after the fix: https://play.rust-lang.org/?gist=c94e2ee4ae1d270130cdeeb7dac9ae54


#3

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.


#4

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.


#5

Here you go:

https://play.rust-lang.org/?gist=24560f5721d5be6bfb4b30a1e8d1fa7a&version=stable&backtrace=0

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…


#6

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.


#7

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


#8

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