Evaluation order of boolean subconditions


#1

So as a bit of background I’m working on a code coverage tool for rust and am currently tackling condition coverage which involves working out if every boolean subcondition has been evaluated as true and false - so for (A || B) && C. A, B and C have to have been true and false for 100% coverage.

Now in C and C++, the evaluation order of boolean subconditions isn’t standardised and could potentially change given different optimisation flags for the compiler. Which can cause things to go wrong if people have subconditions with side effects. Does rust make any guarantees about execution order? In this example I made in rust playground the first condition is always executed first https://play.rust-lang.org/?gist=1eb61ac64a81eb96a619f4c13915d13b&version=stable I’m just wondering if this is guaranteed?

Thanks for any help, I’m aware this is a bit language lawyery so help is greatly appreciated :slight_smile:


#2

Is that really true? I found this which says that can only change if the ops are overloaded;

(But you can’t change && or || in Rust at all.)


#3

Maybe it isn’t true then, at least not in recent standards. I guess I assumed based on MISRA C rules and vague memories of points raised here https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points

I suppose that UB leads to an extension of what if there is a condition f1(&mut x) || f2(&mut x) where f1 and f2 both have side effects, because it looks like that would be UB in C++.


#4

There’s a sequence point after logical operators, so even side effects should be safe.