-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
const evaluating && and || does not short-circuit #29608
Comments
The first has ‘sub with overflow’ as a warning, the second as an error. |
yes, but the execution of the first statement (during runtime) works, the execution of the second statement (during compile-time) doesn't. |
/cc @rust-lang/lang is this expected or not? |
I think this is a bug, |
Mmm, I'm not sure. I think this is expected, for the reason @oli-obk states. |
I take it back. I'm not 100% sure what I think. |
Put another way, I think the warning-vs-error thing is a red herring. The question is whether constant evaluation should ever look at the RHS. If it does, then the warning-vs-error is appropriate. That is, if you swapped the order of the arguments to |
Right, I agree warning vs error is ok, but that the rhs should not be evaluated. |
OK, I agree. Bug. |
I am not sure if it belongs to the same bug report, but I would also expect
to report the same warning as
Instead, the compiler seem to completely disregard the overflow in the first branch of I am not sure if it is desirable for the compiler to report about issues found in dead code or if they should just be "hidden" behind a warning about the dead code. I would definitely not expect them to cause error (as in the |
@ranma42 yes, the behavior will now be the same. The warning will be gone since the second expression is unreachable. The issue with "dead code" here is configurability. Maybe the first boolean expression is a value that is somewhat dynamic: const N: usize = 0;
const ARR: [i32; N] = [42; N];
const X: bool = (N > 0) && ARR[0] == 99; This code should not report a warning or an error about |
@oli-obk I agree that in const-evaluated code it might be undesirable to have these warnings, but at least in "normal" code (including my first example with In my opinion, the different warning behaviour between const-evaluated expressions and expressions that are meant for runtime evaluation but that are optimised away could be reasonable. If I expect a boolean expression to be const-evaluated, I am assuming that some of it might be dead code (because of the shortcut operators). Instead, for normal expressions, my basic assumption is that the compiler is unable to evaluate them before runtime; if they can be evaluated at compile time (at least partially, i.e. if they have dead code), I probably wrote something wrong. If I intentionally write a normal expression with dead code, I would not be surprised if the compiler required me some additional annotation to suppress the warning (cfr. the unused variable warning suppressed by the underscore prefix). |
I see your point. That is indeed a different issue and should have it's own issue number. This issue is solely about constant evaluation of the |
Right, added as #29803. |
As described in rust-lang#29608, the rhs of a logical expression is evaluated even if evaluation of the lhs means no such evaluation should take place.
This issue will be postponed until the two const evaluators have been unified |
Oh good point. I'm assuming this'll be solved partially.
|
I'll add some tests |
Nevermind, this is not fixed. After #46882 is merged, it should be as simple as removing all constness checks from https://github.com/rust-lang/rust/blob/master/src/librustc_mir/hair/cx/expr.rs#L292 |
What's blocking this? |
#49146 is the tracking issue for |
This has been solved together with #49146 |
This prints
false
This gives a compile time error
The text was updated successfully, but these errors were encountered: