Skip to content

Commit

Permalink
rustc_interface: use refcell_try_map
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigorenkoPV committed Nov 23, 2024
1 parent 81275ce commit 59e49ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![feature(file_buffered)]
#![feature(iter_intersperse)]
#![feature(let_chains)]
#![feature(refcell_try_map)]
#![feature(try_blocks)]
#![warn(unreachable_pub)]
// tidy-alphabetical-end
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ pub struct Query<T> {

impl<T> Query<T> {
fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<QueryResult<'_, T>> {
RefMut::filter_map(
self.result.borrow_mut(),
|r: &mut Option<Result<Steal<T>>>| -> Option<&mut Steal<T>> {
r.get_or_insert_with(|| f().map(Steal::new)).as_mut().ok()
},
)
.map_err(|r| *r.as_ref().unwrap().as_ref().map(|_| ()).unwrap_err())
.map(QueryResult)
let result = RefMut::try_map(self.result.borrow_mut(), |option| {
match option.get_or_insert_with(|| f().map(Steal::new)) {
Ok(steal) => Ok(steal),
&mut Err(error) => Err(error),
}
});
match result {
Ok(r) => Ok(QueryResult(r)),
Err((_, e)) => Err(e),
}
}
}

Expand Down

0 comments on commit 59e49ca

Please sign in to comment.