Skip to content

Commit edfb83c

Browse files
committed
auto merge of #18914 : Gankro/rust/cloned, r=aturon
Part of #18424. r? @aturon [breaking-change]
2 parents 803aacd + dfb7a81 commit edfb83c

31 files changed

+121
-119
lines changed

src/libcore/option.rs

+9
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ use mem;
150150
use result::{Result, Ok, Err};
151151
use slice;
152152
use slice::AsSlice;
153+
use clone::Clone;
153154

154155
// Note that this is not a lang item per se, but it has a hidden dependency on
155156
// `Iterator`, which is one. The compiler assumes that the `next` method of
@@ -691,6 +692,14 @@ impl<T> Option<T> {
691692
}
692693
}
693694

695+
impl<'a, T: Clone> Option<&'a T> {
696+
/// Maps an Option<&T> to an Option<T> by cloning the contents of the Option<&T>.
697+
#[unstable = "recently added as part of collections reform"]
698+
pub fn cloned(self) -> Option<T> {
699+
self.map(|t| t.clone())
700+
}
701+
}
702+
694703
impl<T: Default> Option<T> {
695704
/// Returns the contained value or a default
696705
///

src/libcoretest/option.rs

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use core::option::*;
1212
use core::kinds::marker;
1313
use core::mem;
14+
use core::clone::Clone;
1415

1516
#[test]
1617
fn test_get_ptr() {
@@ -239,3 +240,15 @@ fn test_collect() {
239240

240241
assert!(v == None);
241242
}
243+
244+
fn test_cloned() {
245+
let s = 1u32;
246+
let n: Option<&'static u32> = None;
247+
let o = Some(&s);
248+
249+
assert_eq!(o.clone(), Some(&s));
250+
assert_eq!(o.cloned(), Some(1u32));
251+
252+
assert_eq!(n.clone(), None);
253+
assert_eq!(n.cloned(), None);
254+
}

src/librustc/lint/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ struct ImproperCTypesVisitor<'a, 'tcx: 'a> {
391391

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

872-
match cx.tcx.impl_or_trait_items.borrow().find_copy(&did) {
872+
match cx.tcx.impl_or_trait_items.borrow().get(&did).cloned() {
873873
None => cx.sess().span_bug(m.span, "missing method descriptor?!"),
874874
Some(md) => {
875875
match md {

src/librustc/metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,7 @@ impl<'a, 'b, 'c, 'tcx, 'v> Visitor<'v> for ImplVisitor<'a, 'b, 'c, 'tcx> {
18631863
match item.node {
18641864
ItemImpl(_, Some(ref trait_ref), _, _) => {
18651865
let def_map = &self.ecx.tcx.def_map;
1866-
let trait_def = def_map.borrow().get_copy(&trait_ref.ref_id);
1866+
let trait_def = def_map.borrow()[trait_ref.ref_id].clone();
18671867
let def_id = trait_def.def_id();
18681868

18691869
// Load eagerly if this is an implementation of the Drop trait

src/librustc/metadata/tydecode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
439439
pos: pos,
440440
len: len };
441441

442-
match st.tcx.rcache.borrow().find_copy(&key) {
442+
match st.tcx.rcache.borrow().get(&key).cloned() {
443443
Some(tt) => return tt,
444444
None => {}
445445
}

src/librustc/middle/astencode.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1200,8 +1200,7 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
12001200
var_id: var_id,
12011201
closure_expr_id: id
12021202
};
1203-
let upvar_borrow = tcx.upvar_borrow_map.borrow()
1204-
.get_copy(&upvar_id);
1203+
let upvar_borrow = tcx.upvar_borrow_map.borrow()[upvar_id].clone();
12051204
var_id.encode(rbml_w);
12061205
upvar_borrow.encode(rbml_w);
12071206
})

src/librustc/middle/borrowck/move_data.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl MoveData {
295295

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

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

315-
match self.path_map.borrow().find_copy(lp) {
315+
match self.path_map.borrow().get(lp).cloned() {
316316
Some(index) => {
317317
self.each_base_path(index, |p| {
318318
result.push(p);

src/librustc/middle/check_match.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
359359
fn fold_pat(&mut self, pat: P<Pat>) -> P<Pat> {
360360
match pat.node {
361361
PatIdent(..) | PatEnum(..) => {
362-
let def = self.tcx.def_map.borrow().find_copy(&pat.id);
362+
let def = self.tcx.def_map.borrow().get(&pat.id).cloned();
363363
match def {
364364
Some(DefConst(did)) => match lookup_const_by_id(self.tcx, did) {
365365
Some(const_expr) => {
@@ -749,7 +749,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
749749
Some(Vec::from_elem(arity, DUMMY_WILD_PAT)),
750750

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

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

783783
&PatStruct(_, ref pattern_fields, _) => {
784784
// Is this a struct or an enum variant?
785-
let def = cx.tcx.def_map.borrow().get_copy(&pat_id);
785+
let def = cx.tcx.def_map.borrow()[pat_id].clone();
786786
let class_id = match def {
787787
DefConst(..) =>
788788
cx.tcx.sess.span_bug(pat_span, "const pattern should've \

src/librustc/middle/const_eval.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn join_all<It: Iterator<constness>>(mut cs: It) -> constness {
8585
}
8686

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

322322
ExprCall(ref callee, ref args) => {
323-
let def = tcx.def_map.borrow().get_copy(&callee.id);
323+
let def = tcx.def_map.borrow()[callee.id].clone();
324324
match tcx.def_map.borrow_mut().entry(expr.id) {
325325
Vacant(entry) => { entry.set(def); }
326326
_ => {}
@@ -352,7 +352,7 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P<Pat> {
352352
}
353353

354354
ExprPath(ref path) => {
355-
let opt_def = tcx.def_map.borrow().find_copy(&expr.id);
355+
let opt_def = tcx.def_map.borrow().get(&expr.id).cloned();
356356
match opt_def {
357357
Some(def::DefStruct(..)) =>
358358
PatStruct(path.clone(), vec![], false),

src/librustc/middle/expr_use_visitor.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,TYPER> {
850850

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

963962
self.delegate.borrow(closure_expr.id,
964963
closure_expr.span,

src/librustc/middle/liveness.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ fn visit_expr(ir: &mut IrMaps, expr: &Expr) {
449449
match expr.node {
450450
// live nodes required for uses or definitions of variables:
451451
ExprPath(_) => {
452-
let def = ir.tcx.def_map.borrow().get_copy(&expr.id);
452+
let def = ir.tcx.def_map.borrow()[expr.id].clone();
453453
debug!("expr {}: path that leads to {}", expr.id, def);
454454
match def {
455455
DefLocal(..) => ir.add_live_node_for_node(expr.id, ExprNode(expr.span)),
@@ -1316,7 +1316,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
13161316

13171317
fn access_path(&mut self, expr: &Expr, succ: LiveNode, acc: uint)
13181318
-> LiveNode {
1319-
match self.ir.tcx.def_map.borrow().get_copy(&expr.id) {
1319+
match self.ir.tcx.def_map.borrow()[expr.id].clone() {
13201320
DefLocal(nid) => {
13211321
let ln = self.live_node(expr.id, expr.span);
13221322
if acc != 0u {
@@ -1582,7 +1582,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15821582
fn check_lvalue(&mut self, expr: &Expr) {
15831583
match expr.node {
15841584
ExprPath(_) => {
1585-
match self.ir.tcx.def_map.borrow().get_copy(&expr.id) {
1585+
match self.ir.tcx.def_map.borrow()[expr.id].clone() {
15861586
DefLocal(nid) => {
15871587
// Assignment to an immutable variable or argument: only legal
15881588
// if there is no later assignment. If this local is actually

src/librustc/middle/privacy.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EmbargoVisitor<'a, 'tcx> {
245245
ast::ItemImpl(_, _, ref ty, ref impl_items) => {
246246
let public_ty = match ty.node {
247247
ast::TyPath(_, _, id) => {
248-
match self.tcx.def_map.borrow().get_copy(&id) {
248+
match self.tcx.def_map.borrow()[id].clone() {
249249
def::DefPrimTy(..) => true,
250250
def => {
251251
let did = def.def_id();
@@ -313,7 +313,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EmbargoVisitor<'a, 'tcx> {
313313
ast::ItemTy(ref ty, _) if public_first => {
314314
match ty.node {
315315
ast::TyPath(_, _, id) => {
316-
match self.tcx.def_map.borrow().get_copy(&id) {
316+
match self.tcx.def_map.borrow()[id].clone() {
317317
def::DefPrimTy(..) | def::DefTyParam(..) => {},
318318
def => {
319319
let did = def.def_id();
@@ -620,7 +620,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
620620
ast::TyPath(_, _, id) => id,
621621
_ => return Some((err_span, err_msg, None)),
622622
};
623-
let def = self.tcx.def_map.borrow().get_copy(&id);
623+
let def = self.tcx.def_map.borrow()[id].clone();
624624
let did = def.def_id();
625625
assert!(is_local(did));
626626
match self.tcx.map.get(did.node) {
@@ -706,7 +706,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
706706
// Checks that a path is in scope.
707707
fn check_path(&mut self, span: Span, path_id: ast::NodeId, path: &ast::Path) {
708708
debug!("privacy - path {}", self.nodestr(path_id));
709-
let orig_def = self.tcx.def_map.borrow().get_copy(&path_id);
709+
let orig_def = self.tcx.def_map.borrow()[path_id].clone();
710710
let ck = |tyname: &str| {
711711
let ck_public = |def: ast::DefId| {
712712
let name = token::get_ident(path.segments
@@ -789,7 +789,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
789789
// def map is not. Therefore the names we work out below will not always
790790
// be accurate and we can get slightly wonky error messages (but type
791791
// checking is always correct).
792-
match self.tcx.def_map.borrow().get_copy(&path_id) {
792+
match self.tcx.def_map.borrow()[path_id].clone() {
793793
def::DefStaticMethod(..) => ck("static method"),
794794
def::DefFn(..) => ck("function"),
795795
def::DefStatic(..) => ck("static"),
@@ -873,7 +873,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
873873
}
874874
}
875875
ty::ty_enum(_, _) => {
876-
match self.tcx.def_map.borrow().get_copy(&expr.id) {
876+
match self.tcx.def_map.borrow()[expr.id].clone() {
877877
def::DefVariant(_, variant_id, _) => {
878878
for field in fields.iter() {
879879
self.check_field(expr.span, variant_id,
@@ -1255,7 +1255,7 @@ struct CheckTypeForPrivatenessVisitor<'a, 'b: 'a, 'tcx: 'b> {
12551255

12561256
impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
12571257
fn path_is_private_type(&self, path_id: ast::NodeId) -> bool {
1258-
let did = match self.tcx.def_map.borrow().find_copy(&path_id) {
1258+
let did = match self.tcx.def_map.borrow().get(&path_id).cloned() {
12591259
// `int` etc. (None doesn't seem to occur.)
12601260
None | Some(def::DefPrimTy(..)) => return false,
12611261
Some(def) => def.def_id()

src/librustc/middle/resolve.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ impl<'a> Resolver<'a> {
10971097
sp);
10981098

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

13831383
let parent_opt = parent.module().children.borrow()
1384-
.find_copy(&mod_name);
1384+
.get(&mod_name).cloned();
13851385
let new_parent = match parent_opt {
13861386
// It already exists
13871387
Some(ref child) if child.get_module_if_available()
@@ -2676,7 +2676,7 @@ impl<'a> Resolver<'a> {
26762676
BoundResult(..) => {}
26772677
_ => {
26782678
match containing_module.external_module_children.borrow_mut()
2679-
.find_copy(&source) {
2679+
.get(&source).cloned() {
26802680
None => {} // Continue.
26812681
Some(module) => {
26822682
debug!("(resolving single import) found external \
@@ -3191,7 +3191,7 @@ impl<'a> Resolver<'a> {
31913191
fn search_parent_externals(needle: Name, module: &Rc<Module>)
31923192
-> Option<Rc<Module>> {
31933193
module.external_module_children.borrow()
3194-
.find_copy(&needle)
3194+
.get(&needle).cloned()
31953195
.map(|_| module.clone())
31963196
.or_else(|| {
31973197
match module.parent_link.clone() {
@@ -3478,7 +3478,7 @@ impl<'a> Resolver<'a> {
34783478

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

37643764
// Finally, search through external children.
37653765
if namespace == TypeNS {
3766-
match module_.external_module_children.borrow().find_copy(&name) {
3766+
match module_.external_module_children.borrow().get(&name).cloned() {
37673767
None => {}
37683768
Some(module) => {
37693769
let name_bindings =
@@ -4043,7 +4043,7 @@ impl<'a> Resolver<'a> {
40434043
// item, it's ok
40444044
match def {
40454045
DefTyParam(_, did, _) if {
4046-
self.def_map.borrow().find_copy(&did.node)
4046+
self.def_map.borrow().get(&did.node).cloned()
40474047
== Some(DefTyParamBinder(item_id))
40484048
} => {} // ok
40494049
DefSelfTy(did) if did == item_id => {} // ok
@@ -4096,7 +4096,7 @@ impl<'a> Resolver<'a> {
40964096
// item, it's ok
40974097
match def {
40984098
DefTyParam(_, did, _) if {
4099-
self.def_map.borrow().find_copy(&did.node)
4099+
self.def_map.borrow().get(&did.node).cloned()
41004100
== Some(DefTyParamBinder(item_id))
41014101
} => {} // ok
41024102
DefSelfTy(did) if did == item_id => {} // ok
@@ -4148,7 +4148,7 @@ impl<'a> Resolver<'a> {
41484148
// FIXME #4950: Try caching?
41494149

41504150
for (i, rib) in ribs.iter().enumerate().rev() {
4151-
match rib.bindings.find_copy(&name) {
4151+
match rib.bindings.get(&name).cloned() {
41524152
Some(def_like) => {
41534153
return self.upvarify(ribs[i + 1..], def_like, span);
41544154
}
@@ -5444,7 +5444,7 @@ impl<'a> Resolver<'a> {
54445444
// Finally, search through external children.
54455445
if namespace == TypeNS {
54465446
match containing_module.external_module_children.borrow()
5447-
.find_copy(&name) {
5447+
.get(&name).cloned() {
54485448
None => {}
54495449
Some(module) => {
54505450
match module.def_id.get() {

src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub fn lookup(tcx: &ty::ctxt, id: DefId) -> Option<Stability> {
139139
lookup(tcx, trait_method_id)
140140
}
141141
_ if is_local(id) => {
142-
tcx.stability.borrow().local.find_copy(&id.node)
142+
tcx.stability.borrow().local.get(&id.node).cloned()
143143
}
144144
_ => {
145145
let stab = csearch::get_stability(&tcx.sess.cstore, id);

0 commit comments

Comments
 (0)