Skip to content

Add Cloned to Option, kill find_copy and get_copy in favour of explicit cloning #18914

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

Merged
merged 3 commits into from
Nov 17, 2014
Merged
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
9 changes: 9 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ use mem;
use result::{Result, Ok, Err};
use slice;
use slice::AsSlice;
use clone::Clone;

// Note that this is not a lang item per se, but it has a hidden dependency on
// `Iterator`, which is one. The compiler assumes that the `next` method of
Expand Down Expand Up @@ -676,6 +677,14 @@ impl<T> Option<T> {
}
}

impl<'a, T: Clone> Option<&'a T> {
/// Maps an Option<&T> to an Option<T> by cloning the contents of the Option<&T>.
#[unstable = "recently added as part of collections reform"]
pub fn cloned(self) -> Option<T> {
self.map(|t| t.clone())
}
}

impl<T: Default> Option<T> {
/// Returns the contained value or a default
///
Expand Down
13 changes: 13 additions & 0 deletions src/libcoretest/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use core::option::*;
use core::kinds::marker;
use core::mem;
use core::clone::Clone;

#[test]
fn test_get_ptr() {
Expand Down Expand Up @@ -239,3 +240,15 @@ fn test_collect() {

assert!(v == None);
}

fn test_cloned() {
let s = 1u32;
let n: Option<&'static u32> = None;
let o = Some(&s);

assert_eq!(o.clone(), Some(&s));
assert_eq!(o.cloned(), Some(1u32));

assert_eq!(n.clone(), None);
assert_eq!(n.cloned(), None);
}
4 changes: 2 additions & 2 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ struct ImproperCTypesVisitor<'a, 'tcx: 'a> {

impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
fn check_def(&mut self, sp: Span, ty_id: ast::NodeId, path_id: ast::NodeId) {
match self.cx.tcx.def_map.borrow().get_copy(&path_id) {
match self.cx.tcx.def_map.borrow()[path_id].clone() {
def::DefPrimTy(ast::TyInt(ast::TyI)) => {
self.cx.span_lint(IMPROPER_CTYPES, sp,
"found rust type `int` in foreign module, while \
Expand Down Expand Up @@ -866,7 +866,7 @@ fn method_context(cx: &Context, m: &ast::Method) -> MethodContext {
node: m.id
};

match cx.tcx.impl_or_trait_items.borrow().find_copy(&did) {
match cx.tcx.impl_or_trait_items.borrow().get(&did).cloned() {
None => cx.sess().span_bug(m.span, "missing method descriptor?!"),
Some(md) => {
match md {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ impl<'a, 'b, 'c, 'tcx, 'v> Visitor<'v> for ImplVisitor<'a, 'b, 'c, 'tcx> {
match item.node {
ItemImpl(_, Some(ref trait_ref), _, _) => {
let def_map = &self.ecx.tcx.def_map;
let trait_def = def_map.borrow().get_copy(&trait_ref.ref_id);
let trait_def = def_map.borrow()[trait_ref.ref_id].clone();
let def_id = trait_def.def_id();

// Load eagerly if this is an implementation of the Drop trait
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
pos: pos,
len: len };

match st.tcx.rcache.borrow().find_copy(&key) {
match st.tcx.rcache.borrow().get(&key).cloned() {
Some(tt) => return tt,
None => {}
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,7 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
var_id: var_id,
closure_expr_id: id
};
let upvar_borrow = tcx.upvar_borrow_map.borrow()
.get_copy(&upvar_id);
let upvar_borrow = tcx.upvar_borrow_map.borrow()[upvar_id].clone();
var_id.encode(rbml_w);
upvar_borrow.encode(rbml_w);
})
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/borrowck/move_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl MoveData {

fn existing_move_path(&self, lp: &Rc<LoanPath>)
-> Option<MovePathIndex> {
self.path_map.borrow().find_copy(lp)
self.path_map.borrow().get(lp).cloned()
}

fn existing_base_paths(&self, lp: &Rc<LoanPath>)
Expand All @@ -312,7 +312,7 @@ impl MoveData {
* paths of `lp` to `result`, but does not add new move paths
*/

match self.path_map.borrow().find_copy(lp) {
match self.path_map.borrow().get(lp).cloned() {
Some(index) => {
self.each_base_path(index, |p| {
result.push(p);
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
fn fold_pat(&mut self, pat: P<Pat>) -> P<Pat> {
match pat.node {
PatIdent(..) | PatEnum(..) => {
let def = self.tcx.def_map.borrow().find_copy(&pat.id);
let def = self.tcx.def_map.borrow().get(&pat.id).cloned();
match def {
Some(DefConst(did)) => match lookup_const_by_id(self.tcx, did) {
Some(const_expr) => {
Expand Down Expand Up @@ -753,7 +753,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
Some(Vec::from_elem(arity, DUMMY_WILD_PAT)),

&PatIdent(_, _, _) => {
let opt_def = cx.tcx.def_map.borrow().find_copy(&pat_id);
let opt_def = cx.tcx.def_map.borrow().get(&pat_id).cloned();
match opt_def {
Some(DefConst(..)) =>
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
Expand All @@ -768,7 +768,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
}

&PatEnum(_, ref args) => {
let def = cx.tcx.def_map.borrow().get_copy(&pat_id);
let def = cx.tcx.def_map.borrow()[pat_id].clone();
match def {
DefConst(..) =>
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
Expand All @@ -786,7 +786,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],

&PatStruct(_, ref pattern_fields, _) => {
// Is this a struct or an enum variant?
let def = cx.tcx.def_map.borrow().get_copy(&pat_id);
let def = cx.tcx.def_map.borrow()[pat_id].clone();
let class_id = match def {
DefConst(..) =>
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn join_all<It: Iterator<constness>>(mut cs: It) -> constness {
}

fn lookup_const<'a>(tcx: &'a ty::ctxt, e: &Expr) -> Option<&'a Expr> {
let opt_def = tcx.def_map.borrow().find_copy(&e.id);
let opt_def = tcx.def_map.borrow().get(&e.id).cloned();
match opt_def {
Some(def::DefConst(def_id)) => {
lookup_const_by_id(tcx, def_id)
Expand Down Expand Up @@ -321,7 +321,7 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P<Pat> {
PatTup(exprs.iter().map(|expr| const_expr_to_pat(tcx, &**expr)).collect()),

ExprCall(ref callee, ref args) => {
let def = tcx.def_map.borrow().get_copy(&callee.id);
let def = tcx.def_map.borrow()[callee.id].clone();
match tcx.def_map.borrow_mut().entry(expr.id) {
Vacant(entry) => { entry.set(def); }
_ => {}
Expand Down Expand Up @@ -353,7 +353,7 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P<Pat> {
}

ExprPath(ref path) => {
let opt_def = tcx.def_map.borrow().find_copy(&expr.id);
let opt_def = tcx.def_map.borrow().get(&expr.id).cloned();
match opt_def {
Some(def::DefStruct(..)) =>
PatStruct(path.clone(), vec![], false),
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,TYPER> {

// Each match binding is effectively an assignment to the
// binding being produced.
let def = def_map.borrow().get_copy(&pat.id);
let def = def_map.borrow()[pat.id].clone();
match mc.cat_def(pat.id, pat.span, pat_ty, def) {
Ok(binding_cmt) => {
delegate.mutate(pat.id, pat.span, binding_cmt, Init);
Expand Down Expand Up @@ -957,8 +957,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,TYPER> {
// inferred by regionbk
let upvar_id = ty::UpvarId { var_id: id_var,
closure_expr_id: closure_expr.id };
let upvar_borrow = self.tcx().upvar_borrow_map.borrow()
.get_copy(&upvar_id);
let upvar_borrow = self.tcx().upvar_borrow_map.borrow()[upvar_id].clone();

self.delegate.borrow(closure_expr.id,
closure_expr.span,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ fn visit_expr(ir: &mut IrMaps, expr: &Expr) {
match expr.node {
// live nodes required for uses or definitions of variables:
ExprPath(_) => {
let def = ir.tcx.def_map.borrow().get_copy(&expr.id);
let def = ir.tcx.def_map.borrow()[expr.id].clone();
debug!("expr {}: path that leads to {}", expr.id, def);
match def {
DefLocal(..) => ir.add_live_node_for_node(expr.id, ExprNode(expr.span)),
Expand Down Expand Up @@ -1316,7 +1316,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {

fn access_path(&mut self, expr: &Expr, succ: LiveNode, acc: uint)
-> LiveNode {
match self.ir.tcx.def_map.borrow().get_copy(&expr.id) {
match self.ir.tcx.def_map.borrow()[expr.id].clone() {
DefLocal(nid) => {
let ln = self.live_node(expr.id, expr.span);
if acc != 0u {
Expand Down Expand Up @@ -1582,7 +1582,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn check_lvalue(&mut self, expr: &Expr) {
match expr.node {
ExprPath(_) => {
match self.ir.tcx.def_map.borrow().get_copy(&expr.id) {
match self.ir.tcx.def_map.borrow()[expr.id].clone() {
DefLocal(nid) => {
// Assignment to an immutable variable or argument: only legal
// if there is no later assignment. If this local is actually
Expand Down
14 changes: 7 additions & 7 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EmbargoVisitor<'a, 'tcx> {
ast::ItemImpl(_, _, ref ty, ref impl_items) => {
let public_ty = match ty.node {
ast::TyPath(_, _, id) => {
match self.tcx.def_map.borrow().get_copy(&id) {
match self.tcx.def_map.borrow()[id].clone() {
def::DefPrimTy(..) => true,
def => {
let did = def.def_id();
Expand Down Expand Up @@ -313,7 +313,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EmbargoVisitor<'a, 'tcx> {
ast::ItemTy(ref ty, _) if public_first => {
match ty.node {
ast::TyPath(_, _, id) => {
match self.tcx.def_map.borrow().get_copy(&id) {
match self.tcx.def_map.borrow()[id].clone() {
def::DefPrimTy(..) | def::DefTyParam(..) => {},
def => {
let did = def.def_id();
Expand Down Expand Up @@ -620,7 +620,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
ast::TyPath(_, _, id) => id,
_ => return Some((err_span, err_msg, None)),
};
let def = self.tcx.def_map.borrow().get_copy(&id);
let def = self.tcx.def_map.borrow()[id].clone();
let did = def.def_id();
assert!(is_local(did));
match self.tcx.map.get(did.node) {
Expand Down Expand Up @@ -706,7 +706,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
// Checks that a path is in scope.
fn check_path(&mut self, span: Span, path_id: ast::NodeId, path: &ast::Path) {
debug!("privacy - path {}", self.nodestr(path_id));
let orig_def = self.tcx.def_map.borrow().get_copy(&path_id);
let orig_def = self.tcx.def_map.borrow()[path_id].clone();
let ck = |tyname: &str| {
let ck_public = |def: ast::DefId| {
let name = token::get_ident(path.segments
Expand Down Expand Up @@ -789,7 +789,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
// def map is not. Therefore the names we work out below will not always
// be accurate and we can get slightly wonky error messages (but type
// checking is always correct).
match self.tcx.def_map.borrow().get_copy(&path_id) {
match self.tcx.def_map.borrow()[path_id].clone() {
def::DefStaticMethod(..) => ck("static method"),
def::DefFn(..) => ck("function"),
def::DefStatic(..) => ck("static"),
Expand Down Expand Up @@ -873,7 +873,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
}
}
ty::ty_enum(_, _) => {
match self.tcx.def_map.borrow().get_copy(&expr.id) {
match self.tcx.def_map.borrow()[expr.id].clone() {
def::DefVariant(_, variant_id, _) => {
for field in fields.iter() {
self.check_field(expr.span, variant_id,
Expand Down Expand Up @@ -1254,7 +1254,7 @@ struct CheckTypeForPrivatenessVisitor<'a, 'b: 'a, 'tcx: 'b> {

impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
fn path_is_private_type(&self, path_id: ast::NodeId) -> bool {
let did = match self.tcx.def_map.borrow().find_copy(&path_id) {
let did = match self.tcx.def_map.borrow().get(&path_id).cloned() {
// `int` etc. (None doesn't seem to occur.)
None | Some(def::DefPrimTy(..)) => return false,
Some(def) => def.def_id()
Expand Down
20 changes: 10 additions & 10 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ impl<'a> Resolver<'a> {
sp);

// Add or reuse the child.
let child = module_.children.borrow().find_copy(&name);
let child = module_.children.borrow().get(&name).cloned();
match child {
None => {
let child = Rc::new(NameBindings::new());
Expand Down Expand Up @@ -1381,7 +1381,7 @@ impl<'a> Resolver<'a> {
let mod_name = path.segments.last().unwrap().identifier.name;

let parent_opt = parent.module().children.borrow()
.find_copy(&mod_name);
.get(&mod_name).cloned();
let new_parent = match parent_opt {
// It already exists
Some(ref child) if child.get_module_if_available()
Expand Down Expand Up @@ -2676,7 +2676,7 @@ impl<'a> Resolver<'a> {
BoundResult(..) => {}
_ => {
match containing_module.external_module_children.borrow_mut()
.find_copy(&source) {
.get(&source).cloned() {
None => {} // Continue.
Some(module) => {
debug!("(resolving single import) found external \
Expand Down Expand Up @@ -3191,7 +3191,7 @@ impl<'a> Resolver<'a> {
fn search_parent_externals(needle: Name, module: &Rc<Module>)
-> Option<Rc<Module>> {
module.external_module_children.borrow()
.find_copy(&needle)
.get(&needle).cloned()
.map(|_| module.clone())
.or_else(|| {
match module.parent_link.clone() {
Expand Down Expand Up @@ -3478,7 +3478,7 @@ impl<'a> Resolver<'a> {

// Search for external modules.
if namespace == TypeNS {
match module_.external_module_children.borrow().find_copy(&name) {
match module_.external_module_children.borrow().get(&name).cloned() {
None => {}
Some(module) => {
let name_bindings =
Expand Down Expand Up @@ -3763,7 +3763,7 @@ impl<'a> Resolver<'a> {

// Finally, search through external children.
if namespace == TypeNS {
match module_.external_module_children.borrow().find_copy(&name) {
match module_.external_module_children.borrow().get(&name).cloned() {
None => {}
Some(module) => {
let name_bindings =
Expand Down Expand Up @@ -4043,7 +4043,7 @@ impl<'a> Resolver<'a> {
// item, it's ok
match def {
DefTyParam(_, did, _) if {
self.def_map.borrow().find_copy(&did.node)
self.def_map.borrow().get(&did.node).cloned()
== Some(DefTyParamBinder(item_id))
} => {} // ok
DefSelfTy(did) if did == item_id => {} // ok
Expand Down Expand Up @@ -4096,7 +4096,7 @@ impl<'a> Resolver<'a> {
// item, it's ok
match def {
DefTyParam(_, did, _) if {
self.def_map.borrow().find_copy(&did.node)
self.def_map.borrow().get(&did.node).cloned()
== Some(DefTyParamBinder(item_id))
} => {} // ok
DefSelfTy(did) if did == item_id => {} // ok
Expand Down Expand Up @@ -4148,7 +4148,7 @@ impl<'a> Resolver<'a> {
// FIXME #4950: Try caching?

for (i, rib) in ribs.iter().enumerate().rev() {
match rib.bindings.find_copy(&name) {
match rib.bindings.get(&name).cloned() {
Some(def_like) => {
return self.upvarify(ribs[i + 1..], def_like, span);
}
Expand Down Expand Up @@ -5440,7 +5440,7 @@ impl<'a> Resolver<'a> {
// Finally, search through external children.
if namespace == TypeNS {
match containing_module.external_module_children.borrow()
.find_copy(&name) {
.get(&name).cloned() {
None => {}
Some(module) => {
match module.def_id.get() {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn lookup(tcx: &ty::ctxt, id: DefId) -> Option<Stability> {
lookup(tcx, trait_method_id)
}
_ if is_local(id) => {
tcx.stability.borrow().local.find_copy(&id.node)
tcx.stability.borrow().local.get(&id.node).cloned()
}
_ => {
let stab = csearch::get_stability(&tcx.sess.cstore, id);
Expand Down
Loading