Open Sherlock-Holo opened 4 years ago
@Sherlock-Holo Can you please elaborate, what you are trying to achieve here? As I can see you already handled the borrower check issue in the next function try_set_lock1() by storing the result in a local variable.
In try_set_lock0
, the
if let Ok(_) = guard.file_handle.try_lock() {
Ok(())
} else {
Err(2)
}
expression will return a Result
, this result doesn't contain any borrowed value, so that won't have any lifetime problem and it should compile success
Confirmed that this is not specific to async-await: playground
@nikomatsakis said that this is a consequence of the scoping rules for tail expressions in the language, not something that can be fixed in the compiler. They also said it's possible we'd want to change this in an edition, but it's not clear how easy or hard that is.
@pnkfelix or @matthewjasper do you remember if we have an issue from NLL days regarding the scoping giving surprising errors? I feel like we should open a meta-issue.
Hello, is there any update on this issue? I also encountered the same issue:
use std::cell::RefCell;
use std::rc::Rc;
pub struct Node {
pub next: Option<Rc<RefCell<Node>>>,
}
pub fn foo(root: Option<Rc<RefCell<Node>>>) {
let mut queue = Vec::new();
if let Some(x) = root {
queue.push(x);
}
while !queue.is_empty() {
let x = queue.pop().unwrap();
if let Some(next) = x.borrow().next.clone() {
queue.push(next);
}
// uncommenting any of the following will compile
// ()
// 0;
// let completely_random_var = 0;
}
}
fn main() {}
What is interesting is that using while let
expression in place of while
also resolves the issue
--- before
+++ after
@@ -10,15 +10,10 @@
if let Some(x) = root {
queue.push(x);
}
- while !queue.is_empty() {
- let x = queue.pop().unwrap();
+ while let Some(x) = queue.pop() {
if let Some(next) = x.borrow().next.clone() {
queue.push(next);
}
- // uncommenting any of the following will compile
- // ()
- // 0;
- // let completely_random_var = 0;
}
}
Actually, the compiler does already suggest adding ;
to fix this. IMO, the original code should compile w/o error, and having ;
is unnecessary. Is this behavior intended or to be fixed?
I tried this code: playground
I expected to see this happen:
compile success
Instead, this happened: the first function report error
Meta
rustc --version --verbose
: