Rust-analyzer wrong type inferred

Is this wrong type inferred by rust-analyzer (I guess it should be the usize) or I'm missing something ?
In release mode I got owerflow that lead to some kind of recurisive allocation.
What is common practice with this kind of stuff, now I'm not sure that I can belive to rust-analyzer ?

That looks correct.

The compiler assumes integer literals are i32 unless told otherwise, so decimal_places is correct. Similarly, the format!() macro returns a new owned String, so the second f variable is correct too.

Note that your new f variable "shadows" the f: f64 passed as an argument.

1 Like

The code doesn’t compile if you specify decimal_places to be i32 though, it has to be usize.

Using the unstable type_name_of_val confirms that the type of decimal_places is usize, so it seems like this may be a bug in how R-A is handling integer fallback.

Yes, issue is that when I annotate it with u32 it doesn't compile.
Format documentation does specify that argument place is usize .. but I just belived to rust-analyzer and didn't expect that my code can "subtract with overflow".
This is just test code, real code is not this simple but got me to owerflow though.
I'm confused now should I belive to rust-analyzer when working with numerics.
I guess that this is some corner case with format macro but nevertheless.

Nothing in this function should be able to subtract with overflow. Recheck the line numbers in your stack trace.

IIRC rust-analyzer does not really expand the formatting machinery, this is probably why it doesn't infer the right type. I'd suggest you to open an issue at Sign in to GitHub · GitHub.

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.