Skip to content

Get rid of most of the RefCells in librustc_typeck/check #25247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
// process.
fn walk_adjustment(&mut self, expr: &ast::Expr) {
let typer = self.typer;
if let Some(adjustment) = typer.adjustments().borrow().get(&expr.id) {
typer.adjustments( |adjustments| if let Some(adjustment) = adjustments.get(&expr.id) {
match *adjustment {
ty::AdjustReifyFnPointer |
ty::AdjustUnsafeFnPointer => {
Expand All @@ -802,7 +802,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
self.walk_autoderefref(expr, adj);
}
}
}
})
}

/// Autoderefs for overloaded Deref calls in fact reference their receiver. That is, if we have
Expand Down Expand Up @@ -1283,10 +1283,11 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
}
}

fn copy_or_move<'tcx>(typer: &mc::Typer<'tcx>,
cmt: &mc::cmt<'tcx>,
move_reason: MoveReason)
-> ConsumeMode
fn copy_or_move<'tcx,T>(typer: &T,
cmt: &mc::cmt<'tcx>,
move_reason: MoveReason)
-> ConsumeMode
where T: mc::Typer<'tcx>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make this more generic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, Typer is no longer object-safe. I think I'd prefer that Typer stay object-safe; pity we don't have a good solution for passing FnOnce() closurs as objects yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, I remember the issue I had now. Yeah, there was literally no way to add a closure (that I could see) without breaking object safety.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing a re-review of these changes, this doesn't matter anymore I axed the Typer interface last week.

{
if typer.type_moves_by_default(cmt.span, cmt.ty) {
Move(move_reason)
Expand Down
15 changes: 8 additions & 7 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ use syntax::codemap::Span;
use syntax::print::pprust;
use syntax::parse::token;

use std::cell::RefCell;
use std::rc::Rc;

#[derive(Clone, PartialEq, Debug)]
Expand Down Expand Up @@ -292,7 +291,8 @@ pub trait Typer<'tcx> : ty::ClosureTyper<'tcx> {
fn node_method_ty(&self, method_call: ty::MethodCall) -> Option<Ty<'tcx>>;
fn node_method_origin(&self, method_call: ty::MethodCall)
-> Option<ty::MethodOrigin<'tcx>>;
fn adjustments<'a>(&'a self) -> &'a RefCell<NodeMap<ty::AutoAdjustment<'tcx>>>;
fn adjustments<F, T>(&self, closure: F) -> T
where F: FnOnce(&NodeMap<ty::AutoAdjustment<'tcx>>) -> T;
fn is_method_call(&self, id: ast::NodeId) -> bool;
fn temporary_scope(&self, rvalue_id: ast::NodeId) -> Option<region::CodeExtent>;
fn upvar_capture(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture>;
Expand Down Expand Up @@ -409,9 +409,10 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {

fn expr_ty_adjusted(&self, expr: &ast::Expr) -> McResult<Ty<'tcx>> {
let unadjusted_ty = try!(self.expr_ty(expr));
Ok(ty::adjust_ty(self.tcx(), expr.span, expr.id, unadjusted_ty,
self.typer.adjustments().borrow().get(&expr.id),
|method_call| self.typer.node_method_ty(method_call)))
self.typer.adjustments( |adjustments|
Ok(ty::adjust_ty(self.tcx(), expr.span, expr.id, unadjusted_ty,
adjustments.get(&expr.id),
|method_call| self.typer.node_method_ty(method_call))))
}

fn node_ty(&self, id: ast::NodeId) -> McResult<Ty<'tcx>> {
Expand Down Expand Up @@ -443,7 +444,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
}

pub fn cat_expr(&self, expr: &ast::Expr) -> McResult<cmt<'tcx>> {
match self.typer.adjustments().borrow().get(&expr.id) {
self.typer.adjustments( |adjustments| match adjustments.get(&expr.id) {
None => {
// No adjustments.
self.cat_expr_unadjusted(expr)
Expand All @@ -470,7 +471,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
}
}
}
}
} )
}

pub fn cat_expr_autoderefd(&self,
Expand Down
14 changes: 8 additions & 6 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6127,10 +6127,11 @@ pub struct ClosureUpvar<'tcx> {
}

// Returns a list of `ClosureUpvar`s for each upvar.
pub fn closure_upvars<'tcx>(typer: &mc::Typer<'tcx>,
closure_id: ast::DefId,
substs: &Substs<'tcx>)
-> Option<Vec<ClosureUpvar<'tcx>>>
pub fn closure_upvars<'tcx,T>(typer: &T,
closure_id: ast::DefId,
substs: &Substs<'tcx>)
-> Option<Vec<ClosureUpvar<'tcx>>>
where T: mc::Typer<'tcx>
{
// Presently an unboxed closure type cannot "escape" out of a
// function, so we will only encounter ones that originated in the
Expand Down Expand Up @@ -6831,8 +6832,9 @@ impl<'a,'tcx> mc::Typer<'tcx> for ParameterEnvironment<'a,'tcx> {
self.tcx.method_map.borrow().get(&method_call).map(|method| method.origin.clone())
}

fn adjustments(&self) -> &RefCell<NodeMap<ty::AutoAdjustment<'tcx>>> {
&self.tcx.adjustments
fn adjustments<F, T>(&self, closure: F) -> T
where F: FnOnce(&NodeMap<ty::AutoAdjustment<'tcx>>) -> T {
closure(&self.tcx.adjustments.borrow())
}

fn is_method_call(&self, id: ast::NodeId) -> bool {
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_trans/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,9 @@ impl<'blk, 'tcx> mc::Typer<'tcx> for BlockS<'blk, 'tcx> {
.map(|method| method.origin.clone())
}

fn adjustments<'a>(&'a self) -> &'a RefCell<NodeMap<ty::AutoAdjustment<'tcx>>> {
&self.tcx().adjustments
fn adjustments<F, T>(&self, closure: F) -> T
where F: FnOnce(&NodeMap<ty::AutoAdjustment<'tcx>>) -> T {
closure(&self.tcx().adjustments.borrow())
}

fn is_method_call(&self, id: ast::NodeId) -> bool {
Expand Down
Loading