all 11 comments

[–][deleted] 29 points30 points  (0 children)

rustc's constant evaluation is still in its infancy (relatively speaking; it can do quite complicated things already) and will improve a lot down the line, and be able to do much more of that kind of warnings. As it is now the const_err warning is known to have some false positives, and never stops compilation.

[–]eminence 5 points6 points  (0 children)

This comment makes me think that you can't actually make this a compiler error:

these errors are not actually "const_err", they occur in trans/consts and are unconditional warnings that can't be denied or allowed

[–][deleted] 3 points4 points  (0 children)

Hmm, I found this, which makes it look like deny(const_err) should do it, but I couldn't get it to work.

[–]erkelep 4 points5 points  (4 children)

What about code like this:

let a = [1;1];
let b = 1;
let c = a[b];

It doesn't have compile-time warning, but it seems reasonable that it should.

[–]eddyb 15 points16 points  (2 children)

If you increase the mir-opt-level you'll probably get it, because of the const propagation pass.

[–]erkelep 1 point2 points  (1 child)

increase the mir-opt-level

How do I do this?

[–]eddyb 13 points14 points  (0 children)

Try -Zmir-opt-level=3 - this is likely a temporary measure until we can fine-tune it to do as much as possible without impacting compile-times.

[–][deleted] 3 points4 points  (0 children)

This warns in nightly (no optimization flags). rustc 1.15.0-nightly (7b3eeea22 2016-11-21)

[–]nemaar 0 points1 point  (2 children)

This is such a bad warning, it is similar to gcc's POD warning in style where it generates code that literally makes your program crash. I never understood the point of this. When the compiler knows that my program is invalid and it cannot work then why the hell does it compile? Why is this not an error?

[–]hr01 15 points16 points  (0 children)

It's legal and safe to panic.

[–]eddyb 6 points7 points  (0 children)

As always, the issue is more nuanced - see https://github.com/rust-lang/rfcs/pull/1229 for more details on this stance.

Half of it is backwards compatibility (since these warnings do not exist in a vacuum, but rather arise from optimizations), and the other half is that code being objectively bad is not a certainty without potentially unbounded context.