Skip to content

Commit be3723c

Browse files
Rollup merge of rust-lang#81298 - lcnr:big-money-big-prices, r=oli-obk
replace RefCell with Cell in FnCtxt small cleanup
2 parents 64cf8c2 + 688cf64 commit be3723c

File tree

6 files changed

+18
-23
lines changed

6 files changed

+18
-23
lines changed

compiler/rustc_typeck/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(super) fn check_fn<'a, 'tcx>(
6666
// Create the function context. This is either derived from scratch or,
6767
// in the case of closures, based on the outer context.
6868
let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id);
69-
*fcx.ps.borrow_mut() = UnsafetyState::function(fn_sig.unsafety, fn_id);
69+
fcx.ps.set(UnsafetyState::function(fn_sig.unsafety, fn_id));
7070

7171
let tcx = fcx.tcx;
7272
let sess = tcx.sess;

compiler/rustc_typeck/src/check/coercion.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1472,22 +1472,22 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14721472
fn_output = Some(&fn_decl.output); // `impl Trait` return type
14731473
}
14741474
}
1475-
if let (Some(sp), Some(fn_output)) = (fcx.ret_coercion_span.borrow().as_ref(), fn_output) {
1476-
self.add_impl_trait_explanation(&mut err, cause, fcx, expected, *sp, fn_output);
1475+
if let (Some(sp), Some(fn_output)) = (fcx.ret_coercion_span.get(), fn_output) {
1476+
self.add_impl_trait_explanation(&mut err, cause, fcx, expected, sp, fn_output);
14771477
}
14781478

1479-
if let Some(sp) = fcx.ret_coercion_span.borrow().as_ref() {
1479+
if let Some(sp) = fcx.ret_coercion_span.get() {
14801480
// If the closure has an explicit return type annotation,
14811481
// then a type error may occur at the first return expression we
14821482
// see in the closure (if it conflicts with the declared
14831483
// return type). Skip adding a note in this case, since it
14841484
// would be incorrect.
1485-
if !err.span.primary_spans().iter().any(|span| span == sp) {
1485+
if !err.span.primary_spans().iter().any(|&span| span == sp) {
14861486
let hir = fcx.tcx.hir();
14871487
let body_owner = hir.body_owned_by(hir.enclosing_body_owner(fcx.body_id));
14881488
if fcx.tcx.is_closure(hir.body_owner_def_id(body_owner).to_def_id()) {
14891489
err.span_note(
1490-
*sp,
1490+
sp,
14911491
&format!(
14921492
"return type inferred to be `{}` here",
14931493
fcx.resolve_vars_if_possible(expected)

compiler/rustc_typeck/src/check/expr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -680,14 +680,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
680680
if self.ret_coercion.is_none() {
681681
self.tcx.sess.emit_err(ReturnStmtOutsideOfFnBody { span: expr.span });
682682
} else if let Some(ref e) = expr_opt {
683-
if self.ret_coercion_span.borrow().is_none() {
684-
*self.ret_coercion_span.borrow_mut() = Some(e.span);
683+
if self.ret_coercion_span.get().is_none() {
684+
self.ret_coercion_span.set(Some(e.span));
685685
}
686686
self.check_return_expr(e);
687687
} else {
688688
let mut coercion = self.ret_coercion.as_ref().unwrap().borrow_mut();
689-
if self.ret_coercion_span.borrow().is_none() {
690-
*self.ret_coercion_span.borrow_mut() = Some(expr.span);
689+
if self.ret_coercion_span.get().is_none() {
690+
self.ret_coercion_span.set(Some(expr.span));
691691
}
692692
let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
693693
if let Some((fn_decl, _)) = self.get_fn_decl(expr.hir_id) {

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_span::{self, MultiSpan, Span};
2323
use rustc_trait_selection::traits::{self, ObligationCauseCode, StatementAsExpression};
2424

2525
use crate::structured_errors::StructuredDiagnostic;
26-
use std::mem::replace;
2726
use std::slice;
2827

2928
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -589,11 +588,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
589588
blk: &'tcx hir::Block<'tcx>,
590589
expected: Expectation<'tcx>,
591590
) -> Ty<'tcx> {
592-
let prev = {
593-
let mut fcx_ps = self.ps.borrow_mut();
594-
let unsafety_state = fcx_ps.recurse(blk);
595-
replace(&mut *fcx_ps, unsafety_state)
596-
};
591+
let prev = self.ps.replace(self.ps.get().recurse(blk));
597592

598593
// In some cases, blocks have just one exit, but other blocks
599594
// can be targeted by multiple breaks. This can happen both
@@ -709,7 +704,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
709704

710705
self.write_ty(blk.hir_id, ty);
711706

712-
*self.ps.borrow_mut() = prev;
707+
self.ps.set(prev);
713708
ty
714709
}
715710

compiler/rustc_typeck/src/check/fn_ctxt/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ pub struct FnCtxt<'a, 'tcx> {
6666
pub(super) in_tail_expr: bool,
6767

6868
/// First span of a return site that we find. Used in error messages.
69-
pub(super) ret_coercion_span: RefCell<Option<Span>>,
69+
pub(super) ret_coercion_span: Cell<Option<Span>>,
7070

7171
pub(super) resume_yield_tys: Option<(Ty<'tcx>, Ty<'tcx>)>,
7272

73-
pub(super) ps: RefCell<UnsafetyState>,
73+
pub(super) ps: Cell<UnsafetyState>,
7474

7575
/// Whether the last checked node generates a divergence (e.g.,
7676
/// `return` will set this to `Always`). In general, when entering
@@ -127,9 +127,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
127127
ret_coercion_impl_trait: None,
128128
ret_type_span: None,
129129
in_tail_expr: false,
130-
ret_coercion_span: RefCell::new(None),
130+
ret_coercion_span: Cell::new(None),
131131
resume_yield_tys: None,
132-
ps: RefCell::new(UnsafetyState::function(hir::Unsafety::Normal, hir::CRATE_HIR_ID)),
132+
ps: Cell::new(UnsafetyState::function(hir::Unsafety::Normal, hir::CRATE_HIR_ID)),
133133
diverges: Cell::new(Diverges::Maybe),
134134
has_errors: Cell::new(false),
135135
enclosing_breakables: RefCell::new(EnclosingBreakables {

compiler/rustc_typeck/src/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ impl UnsafetyState {
184184
UnsafetyState { def, unsafety, unsafe_push_count: 0, from_fn: true }
185185
}
186186

187-
pub fn recurse(&mut self, blk: &hir::Block<'_>) -> UnsafetyState {
187+
pub fn recurse(self, blk: &hir::Block<'_>) -> UnsafetyState {
188188
use hir::BlockCheckMode;
189189
match self.unsafety {
190190
// If this unsafe, then if the outer function was already marked as
191191
// unsafe we shouldn't attribute the unsafe'ness to the block. This
192192
// way the block can be warned about instead of ignoring this
193193
// extraneous block (functions are never warned about).
194-
hir::Unsafety::Unsafe if self.from_fn => *self,
194+
hir::Unsafety::Unsafe if self.from_fn => self,
195195

196196
unsafety => {
197197
let (unsafety, def, count) = match blk.rules {

0 commit comments

Comments
 (0)