The following bit me pretty hard today (at runtime!!)
fn main() {
let s = -1;
let u: usize = 0;
let _ = u == s+1;
}
This triggers:
thread '<main>' panicked at 'arithmetic operation overflowed', <anon>:4
I think understand why: s is being inferred as type usize because of the comparison. However by saying s = -1 I thought I was pretty clear about the fact that I would never want s to be unsigned. I guess the compiler should reject this, right? I can file an issue if needed.
[quote]There's a lot of concern that having +, -, and *
overflow by default is incorrect - it's a source of many security
vulnerabilities. Let's try changing the default to being checked and
measure the impact on performance and code size.[/quote]
Sorry if I didn't make myself clear enough. What bothers me is not that overflow panics by default. It's a nice design decision. My problem is that when I write "let s = -1", I do not want s to be inferred as an unsigned type!
@mdup FYI, I don't think any of the responses to your topic are correct. You're correct that it's extremely odd that s can be inferred to be usize when explicitly specifying usize would be an error. Probably a bug; too late to make it an error now, but it could be linted...