2023-10-16 12:36:39 -05:00
|
|
|
// skip-filecheck
|
2020-09-30 19:04:19 -05:00
|
|
|
// compile-flags: -Zmir-opt-level=1 -Zunsound-mir-opts
|
2020-10-02 09:50:45 -05:00
|
|
|
// ignore-test
|
|
|
|
// FIXME: the pass is unsound and causes ICEs in the MIR validator
|
|
|
|
|
2020-08-31 12:11:44 -05:00
|
|
|
// EMIT_MIR simplify_try_if_let.{impl#0}-append.SimplifyArmIdentity.diff
|
Modify SimplifyArmIdentity so it can trigger on mir-opt-level=1
I also added test cases to make sure the optimization can fire on all of
these cases:
```rust
fn case_1(o: Option<u8>) -> Option<u8> {
match o {
Some(u) => Some(u),
None => None,
}
}
fn case2(r: Result<u8, i32>) -> Result<u8, i32> {
match r {
Ok(u) => Ok(u),
Err(i) => Err(i),
}
}
fn case3(r: Result<u8, i32>) -> Result<u8, i32> {
let u = r?;
Ok(u)
}
```
Without MIR inlining, this still does not completely optimize away the
`?` operator because the `Try::into_result()`, `From::from()` and
`Try::from_error()` calls still exist. This does move us a bit closer to
that goal though because:
- We can now run the pass on mir-opt-level=1
- We no longer depend on the copy propagation pass running which is
unlikely to stabilize anytime soon.
2020-05-11 19:13:15 -05:00
|
|
|
|
|
|
|
use std::ptr::NonNull;
|
|
|
|
|
|
|
|
pub struct LinkedList {
|
|
|
|
head: Option<NonNull<Node>>,
|
|
|
|
tail: Option<NonNull<Node>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Node {
|
|
|
|
next: Option<NonNull<Node>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LinkedList {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { head: None, tail: None }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn append(&mut self, other: &mut Self) {
|
|
|
|
match self.tail {
|
2020-10-02 09:50:45 -05:00
|
|
|
None => {}
|
Modify SimplifyArmIdentity so it can trigger on mir-opt-level=1
I also added test cases to make sure the optimization can fire on all of
these cases:
```rust
fn case_1(o: Option<u8>) -> Option<u8> {
match o {
Some(u) => Some(u),
None => None,
}
}
fn case2(r: Result<u8, i32>) -> Result<u8, i32> {
match r {
Ok(u) => Ok(u),
Err(i) => Err(i),
}
}
fn case3(r: Result<u8, i32>) -> Result<u8, i32> {
let u = r?;
Ok(u)
}
```
Without MIR inlining, this still does not completely optimize away the
`?` operator because the `Try::into_result()`, `From::from()` and
`Try::from_error()` calls still exist. This does move us a bit closer to
that goal though because:
- We can now run the pass on mir-opt-level=1
- We no longer depend on the copy propagation pass running which is
unlikely to stabilize anytime soon.
2020-05-11 19:13:15 -05:00
|
|
|
Some(mut tail) => {
|
|
|
|
// `as_mut` is okay here because we have exclusive access to the entirety
|
|
|
|
// of both lists.
|
|
|
|
if let Some(other_head) = other.head.take() {
|
|
|
|
unsafe {
|
|
|
|
tail.as_mut().next = Some(other_head);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut one = LinkedList::new();
|
|
|
|
let mut two = LinkedList::new();
|
|
|
|
one.append(&mut two);
|
|
|
|
}
|