2022-08-14 07:17:53 -05:00
|
|
|
//@ run-pass
|
2022-10-13 20:29:25 -05:00
|
|
|
//@ compile-flags: -Z validate-mir
|
2023-01-24 02:06:35 -06:00
|
|
|
//@ revisions: edition2021 edition2024
|
|
|
|
//@ [edition2021] edition: 2021
|
|
|
|
//@ [edition2024] compile-flags: -Z unstable-options
|
|
|
|
//@ [edition2024] edition: 2024
|
2022-10-12 17:29:08 -05:00
|
|
|
#![feature(let_chains)]
|
2023-01-24 02:06:35 -06:00
|
|
|
#![cfg_attr(edition2024, feature(if_let_rescope))]
|
2022-08-14 07:17:53 -05:00
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct DropOrderCollector(RefCell<Vec<u32>>);
|
|
|
|
|
|
|
|
struct LoudDrop<'a>(&'a DropOrderCollector, u32);
|
|
|
|
|
|
|
|
impl Drop for LoudDrop<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("{}", self.1);
|
|
|
|
self.0.0.borrow_mut().push(self.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DropOrderCollector {
|
|
|
|
fn option_loud_drop(&self, n: u32) -> Option<LoudDrop> {
|
|
|
|
Some(LoudDrop(self, n))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn loud_drop(&self, n: u32) -> LoudDrop {
|
|
|
|
LoudDrop(self, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print(&self, n: u32) {
|
|
|
|
println!("{}", n);
|
|
|
|
self.0.borrow_mut().push(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn if_(&self) {
|
|
|
|
if self.option_loud_drop(1).is_some() {
|
|
|
|
self.print(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.option_loud_drop(3).is_none() {
|
|
|
|
unreachable!();
|
|
|
|
} else if self.option_loud_drop(4).is_some() {
|
|
|
|
self.print(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
if {
|
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.
2022-10-20 02:50:32 -05:00
|
|
|
if self.option_loud_drop(6).is_some() && self.option_loud_drop(7).is_some() {
|
2022-08-14 07:17:53 -05:00
|
|
|
self.loud_drop(8);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} {
|
|
|
|
self.print(9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn if_let(&self) {
|
2023-01-24 02:06:35 -06:00
|
|
|
#[cfg(edition2021)]
|
2022-08-14 07:17:53 -05:00
|
|
|
if let None = self.option_loud_drop(2) {
|
|
|
|
unreachable!();
|
|
|
|
} else {
|
|
|
|
self.print(1);
|
|
|
|
}
|
2023-01-24 02:06:35 -06:00
|
|
|
#[cfg(edition2024)]
|
|
|
|
if let None = self.option_loud_drop(1) {
|
|
|
|
unreachable!();
|
|
|
|
} else {
|
|
|
|
self.print(2);
|
|
|
|
}
|
2022-08-14 07:17:53 -05:00
|
|
|
|
|
|
|
if let Some(_) = self.option_loud_drop(4) {
|
|
|
|
self.print(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(_d) = self.option_loud_drop(6) {
|
|
|
|
self.print(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_(&self) {
|
|
|
|
match self.option_loud_drop(2) {
|
|
|
|
_any => self.print(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.option_loud_drop(4) {
|
|
|
|
_ => self.print(3),
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.option_loud_drop(6) {
|
|
|
|
Some(_) => self.print(5),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
match {
|
|
|
|
let _ = self.loud_drop(7);
|
|
|
|
let _d = self.loud_drop(9);
|
|
|
|
self.print(8);
|
|
|
|
()
|
|
|
|
} {
|
|
|
|
() => self.print(10),
|
|
|
|
}
|
|
|
|
|
|
|
|
match {
|
|
|
|
match self.option_loud_drop(14) {
|
|
|
|
_ => {
|
|
|
|
self.print(11);
|
|
|
|
self.option_loud_drop(13)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} {
|
|
|
|
_ => self.print(12),
|
|
|
|
}
|
|
|
|
|
|
|
|
match {
|
|
|
|
loop {
|
|
|
|
break match self.option_loud_drop(16) {
|
|
|
|
_ => {
|
|
|
|
self.print(15);
|
|
|
|
self.option_loud_drop(18)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} {
|
|
|
|
_ => self.print(17),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
2022-10-20 02:50:32 -05:00
|
|
|
fn and_chain(&self) {
|
|
|
|
// issue-103107
|
|
|
|
if self.option_loud_drop(1).is_some() // 1
|
|
|
|
&& self.option_loud_drop(2).is_some() // 2
|
|
|
|
&& self.option_loud_drop(3).is_some() // 3
|
|
|
|
&& self.option_loud_drop(4).is_some() // 4
|
|
|
|
&& self.option_loud_drop(5).is_some() // 5
|
|
|
|
{
|
|
|
|
self.print(6); // 6
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = self.option_loud_drop(7).is_some() // 1
|
|
|
|
&& self.option_loud_drop(8).is_some() // 2
|
|
|
|
&& self.option_loud_drop(9).is_some(); // 3
|
|
|
|
self.print(10); // 4
|
|
|
|
|
|
|
|
// Test associativity
|
|
|
|
if self.option_loud_drop(11).is_some() // 1
|
|
|
|
&& (self.option_loud_drop(12).is_some() // 2
|
|
|
|
&& self.option_loud_drop(13).is_some() // 3
|
|
|
|
&& self.option_loud_drop(14).is_some()) // 4
|
|
|
|
&& self.option_loud_drop(15).is_some() // 5
|
|
|
|
{
|
|
|
|
self.print(16); // 6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn or_chain(&self) {
|
|
|
|
// issue-103107
|
|
|
|
if self.option_loud_drop(1).is_none() // 1
|
|
|
|
|| self.option_loud_drop(2).is_none() // 2
|
|
|
|
|| self.option_loud_drop(3).is_none() // 3
|
|
|
|
|| self.option_loud_drop(4).is_none() // 4
|
|
|
|
|| self.option_loud_drop(5).is_some() // 5
|
|
|
|
{
|
|
|
|
self.print(6); // 6
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = self.option_loud_drop(7).is_none() // 1
|
|
|
|
|| self.option_loud_drop(8).is_none() // 2
|
|
|
|
|| self.option_loud_drop(9).is_none(); // 3
|
|
|
|
self.print(10); // 4
|
|
|
|
|
|
|
|
// Test associativity
|
|
|
|
if self.option_loud_drop(11).is_none() // 1
|
|
|
|
|| (self.option_loud_drop(12).is_none() // 2
|
|
|
|
|| self.option_loud_drop(13).is_none() // 3
|
|
|
|
|| self.option_loud_drop(14).is_none()) // 4
|
|
|
|
|| self.option_loud_drop(15).is_some() // 5
|
|
|
|
{
|
|
|
|
self.print(16); // 6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 17:21:19 -06:00
|
|
|
fn mixed_and_or_chain(&self) {
|
|
|
|
// issue-103107
|
|
|
|
if self.option_loud_drop(1).is_none() // 1
|
|
|
|
|| self.option_loud_drop(2).is_none() // 2
|
|
|
|
|| self.option_loud_drop(3).is_some() // 3
|
|
|
|
&& self.option_loud_drop(4).is_some() // 4
|
|
|
|
&& self.option_loud_drop(5).is_none() // 5
|
|
|
|
|| self.option_loud_drop(6).is_none() // 6
|
|
|
|
|| self.option_loud_drop(7).is_some() // 7
|
|
|
|
{
|
|
|
|
self.print(8); // 8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:29:08 -05:00
|
|
|
fn let_chain(&self) {
|
|
|
|
// take the "then" branch
|
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.
2022-10-20 02:50:32 -05:00
|
|
|
if self.option_loud_drop(1).is_some() // 1
|
|
|
|
&& self.option_loud_drop(2).is_some() // 2
|
2022-10-12 17:29:08 -05:00
|
|
|
&& let Some(_d) = self.option_loud_drop(4) { // 4
|
|
|
|
self.print(3); // 3
|
|
|
|
}
|
|
|
|
|
2023-01-24 02:06:35 -06:00
|
|
|
#[cfg(edition2021)]
|
2022-10-12 17:29:08 -05:00
|
|
|
// take the "else" branch
|
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.
2022-10-20 02:50:32 -05:00
|
|
|
if self.option_loud_drop(5).is_some() // 1
|
|
|
|
&& self.option_loud_drop(6).is_some() // 2
|
2022-10-13 22:13:52 -05:00
|
|
|
&& let None = self.option_loud_drop(8) { // 4
|
2022-10-12 17:29:08 -05:00
|
|
|
unreachable!();
|
|
|
|
} else {
|
2022-10-13 22:13:52 -05:00
|
|
|
self.print(7); // 3
|
2022-10-12 17:29:08 -05:00
|
|
|
}
|
2023-01-24 02:06:35 -06:00
|
|
|
#[cfg(edition2024)]
|
|
|
|
// take the "else" branch
|
|
|
|
if self.option_loud_drop(5).is_some() // 1
|
|
|
|
&& self.option_loud_drop(6).is_some() // 2
|
|
|
|
&& let None = self.option_loud_drop(7) { // 4
|
|
|
|
unreachable!();
|
|
|
|
} else {
|
|
|
|
self.print(8); // 3
|
|
|
|
}
|
2022-10-12 17:29:08 -05:00
|
|
|
|
|
|
|
// let exprs interspersed
|
|
|
|
if self.option_loud_drop(9).is_some() // 1
|
|
|
|
&& let Some(_d) = self.option_loud_drop(13) // 5
|
|
|
|
&& self.option_loud_drop(10).is_some() // 2
|
|
|
|
&& let Some(_e) = self.option_loud_drop(12) { // 4
|
|
|
|
self.print(11); // 3
|
|
|
|
}
|
|
|
|
|
|
|
|
// let exprs first
|
|
|
|
if let Some(_d) = self.option_loud_drop(18) // 5
|
|
|
|
&& let Some(_e) = self.option_loud_drop(17) // 4
|
|
|
|
&& self.option_loud_drop(14).is_some() // 1
|
|
|
|
&& self.option_loud_drop(15).is_some() { // 2
|
|
|
|
self.print(16); // 3
|
|
|
|
}
|
|
|
|
|
|
|
|
// let exprs last
|
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.
2022-10-20 02:50:32 -05:00
|
|
|
if self.option_loud_drop(19).is_some() // 1
|
|
|
|
&& self.option_loud_drop(20).is_some() // 2
|
2022-10-12 17:29:08 -05:00
|
|
|
&& let Some(_d) = self.option_loud_drop(23) // 5
|
|
|
|
&& let Some(_e) = self.option_loud_drop(22) { // 4
|
|
|
|
self.print(21); // 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn while_(&self) {
|
|
|
|
let mut v = self.option_loud_drop(4);
|
|
|
|
while let Some(_d) = v
|
|
|
|
&& self.option_loud_drop(1).is_some()
|
|
|
|
&& self.option_loud_drop(2).is_some() {
|
|
|
|
self.print(3);
|
|
|
|
v = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 07:17:53 -05:00
|
|
|
fn assert_sorted(self) {
|
|
|
|
assert!(
|
|
|
|
self.0
|
|
|
|
.into_inner()
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.all(|(idx, item)| idx + 1 == item.try_into().unwrap())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
println!("-- if --");
|
|
|
|
let collector = DropOrderCollector::default();
|
|
|
|
collector.if_();
|
|
|
|
collector.assert_sorted();
|
|
|
|
|
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.
2022-10-20 02:50:32 -05:00
|
|
|
println!("-- and chain --");
|
|
|
|
let collector = DropOrderCollector::default();
|
|
|
|
collector.and_chain();
|
|
|
|
collector.assert_sorted();
|
|
|
|
|
|
|
|
println!("-- or chain --");
|
|
|
|
let collector = DropOrderCollector::default();
|
|
|
|
collector.or_chain();
|
|
|
|
collector.assert_sorted();
|
|
|
|
|
2022-12-03 17:21:19 -06:00
|
|
|
println!("-- mixed and/or chain --");
|
|
|
|
let collector = DropOrderCollector::default();
|
|
|
|
collector.mixed_and_or_chain();
|
|
|
|
collector.assert_sorted();
|
|
|
|
|
2022-08-14 07:17:53 -05:00
|
|
|
println!("-- if let --");
|
|
|
|
let collector = DropOrderCollector::default();
|
|
|
|
collector.if_let();
|
|
|
|
collector.assert_sorted();
|
|
|
|
|
|
|
|
println!("-- match --");
|
|
|
|
let collector = DropOrderCollector::default();
|
|
|
|
collector.match_();
|
|
|
|
collector.assert_sorted();
|
2022-10-12 17:29:08 -05:00
|
|
|
|
|
|
|
println!("-- let chain --");
|
|
|
|
let collector = DropOrderCollector::default();
|
|
|
|
collector.let_chain();
|
|
|
|
collector.assert_sorted();
|
|
|
|
|
|
|
|
println!("-- while --");
|
|
|
|
let collector = DropOrderCollector::default();
|
|
|
|
collector.while_();
|
|
|
|
collector.assert_sorted();
|
2022-08-14 07:17:53 -05:00
|
|
|
}
|