Remove drop order twist of && and || and make them associative

Previously a short circuiting && chain would drop the
first element after all the other elements, and otherwise
follow evaluation order, so code like:

f(1).g() && f(2).g() && f(3).g() && f(4).g()

would drop the temporaries in the order 2,3,4,1. This made
&& and || non-associative regarding drop order, so
adding ()'s to the expression would change drop order:

f(1).g() && (f(2).g() && f(3).g()) && f(4).g()

for example would drop in the order 3,2,4,1.

As, except for the bool result, there is no data returned
by the sub-expressions of the short circuiting binops,
we can safely discard of any temporaries created by the
sub-expr. Previously, code was already putting the rhs's
into terminating scopes, but missed it for the lhs's.

This commit addresses this "twist". In the expression,
we now also put the lhs into a terminating scope.
The drop order for the above expressions is 1,2,3,4
now.
This commit is contained in:
est31 2022-10-20 09:50:32 +02:00
parent cab4fd678c
commit 8cf521d80e
3 changed files with 129 additions and 11 deletions

View file

@ -241,18 +241,35 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
// scopes, meaning that temporaries cannot outlive them.
// This ensures fixed size stacks.
hir::ExprKind::Binary(
source_map::Spanned { node: hir::BinOpKind::And, .. },
_,
source_map::Spanned { node: outer @ hir::BinOpKind::And, .. },
ref l,
ref r,
)
| hir::ExprKind::Binary(
source_map::Spanned { node: hir::BinOpKind::Or, .. },
_,
source_map::Spanned { node: outer @ hir::BinOpKind::Or, .. },
ref l,
ref r,
) => {
// For shortcircuiting operators, mark the RHS as a terminating
// scope since it only executes conditionally.
// If the LHS is not another binop itself of the same kind as ours,
// we also mark it as terminating, so that in && or || chains,
// the temporaries are dropped in order instead of the very first
// being dropped last. For the Let exception, see below.
let terminate_lhs = match l.kind {
hir::ExprKind::Let(_) => false,
hir::ExprKind::Binary(source_map::Spanned { node, .. }, ..)
if node == outer =>
{
false
}
_ => true,
};
if terminate_lhs {
terminating(l.hir_id.local_id);
}
// `Let` expressions (in a let-chain) shouldn't be terminating, as their temporaries
// should live beyond the immediate expression
if !matches!(r.kind, hir::ExprKind::Let(_)) {