-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rescope temp lifetime in let-chain into IfElse
- Loading branch information
1 parent
959a67a
commit 880e027
Showing
12 changed files
with
278 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//@ run-pass | ||
//@ edition:2024 | ||
//@ compile-flags: -Z validate-mir -Zunstable-options | ||
|
||
#![feature(let_chains)] | ||
#![feature(if_let_rescope)] | ||
|
||
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 print(&self, n: u32) { | ||
println!("{}", n); | ||
self.0.borrow_mut().push(n) | ||
} | ||
|
||
fn assert_sorted(self) { | ||
assert!( | ||
self.0 | ||
.into_inner() | ||
.into_iter() | ||
.enumerate() | ||
.all(|(idx, item)| idx + 1 == item.try_into().unwrap()) | ||
); | ||
} | ||
|
||
fn if_let(&self) { | ||
if let None = self.option_loud_drop(1) { | ||
unreachable!(); | ||
} else { | ||
self.print(2); | ||
} | ||
|
||
if let Some(_) = self.option_loud_drop(4) { | ||
self.print(3); | ||
} | ||
|
||
if let Some(_d) = self.option_loud_drop(6) { | ||
self.print(5); | ||
} | ||
} | ||
|
||
fn let_chain(&self) { | ||
// take the "then" branch | ||
if self.option_loud_drop(1).is_some() // 1 | ||
&& self.option_loud_drop(2).is_some() // 2 | ||
&& let Some(_d) = self.option_loud_drop(4) | ||
// 4 | ||
{ | ||
self.print(3); // 3 | ||
} | ||
|
||
// 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) | ||
// 3 | ||
{ | ||
unreachable!(); | ||
} else { | ||
self.print(8); // 4 | ||
} | ||
|
||
// 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 | ||
if self.option_loud_drop(19).is_some() // 1 | ||
&& self.option_loud_drop(20).is_some() // 2 | ||
&& let Some(_d) = self.option_loud_drop(23) // 5 | ||
&& let Some(_e) = self.option_loud_drop(22) | ||
// 4 | ||
{ | ||
self.print(21); // 3 | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
println!("-- if let --"); | ||
let collector = DropOrderCollector::default(); | ||
collector.if_let(); | ||
collector.assert_sorted(); | ||
|
||
println!("-- let chain --"); | ||
let collector = DropOrderCollector::default(); | ||
collector.let_chain(); | ||
collector.assert_sorted(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
struct A; | ||
struct B<'a, T>(&'a mut T); | ||
|
||
impl A { | ||
fn f(&mut self) -> Option<B<'_, Self>> { | ||
Some(B(self)) | ||
} | ||
} | ||
|
||
impl<'a, T> Drop for B<'a, T> { | ||
fn drop(&mut self) { | ||
// this is needed to keep NLL's hands off and to ensure | ||
// the inner mutable borrow stays alive | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut a = A; | ||
if let None = a.f().as_ref() { | ||
unreachable!() | ||
} else { | ||
a.f().unwrap(); | ||
//~^ ERROR cannot borrow `a` as mutable more than once at a time | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error[E0499]: cannot borrow `a` as mutable more than once at a time | ||
--> $DIR/feature-gate-if-let-rescope.rs:22:9 | ||
| | ||
LL | if let None = a.f().as_ref() { | ||
| ----- | ||
| | | ||
| first mutable borrow occurs here | ||
| a temporary with access to the first borrow is created here ... | ||
... | ||
LL | a.f().unwrap(); | ||
| ^ second mutable borrow occurs here | ||
LL | | ||
LL | }; | ||
| - ... and the first borrow might be used here, when that temporary is dropped and runs the destructor for type `Option<B<'_, A>>` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0499`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.