Skip to content

Commit 6559323

Browse files
committed
librustc: Allow mutation of moved upvars.
1 parent 413328b commit 6559323

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

src/librustc/middle/mem_categorization.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,29 @@ impl MutabilityCategory {
306306
}
307307
}
308308

309+
fn from_def(def: &def::Def) -> MutabilityCategory {
310+
match *def {
311+
def::DefFn(..) | def::DefStaticMethod(..) | def::DefSelfTy(..) |
312+
def::DefMod(..) | def::DefForeignMod(..) | def::DefVariant(..) |
313+
def::DefTy(..) | def::DefTrait(..) | def::DefPrimTy(..) |
314+
def::DefTyParam(..) | def::DefUse(..) | def::DefStruct(..) |
315+
def::DefTyParamBinder(..) | def::DefRegion(..) | def::DefLabel(..) |
316+
def::DefMethod(..) => fail!("no MutabilityCategory for def: {}", *def),
317+
318+
def::DefStatic(_, false) => McImmutable,
319+
def::DefStatic(_, true) => McDeclared,
320+
321+
def::DefArg(_, binding_mode) |
322+
def::DefBinding(_, binding_mode) |
323+
def::DefLocal(_, binding_mode) => match binding_mode {
324+
ast::BindByValue(ast::MutMutable) => McDeclared,
325+
_ => McImmutable
326+
},
327+
328+
def::DefUpvar(_, def, _, _) => MutabilityCategory::from_def(&*def)
329+
}
330+
}
331+
309332
pub fn inherit(&self) -> MutabilityCategory {
310333
match *self {
311334
McImmutable => McImmutable,
@@ -503,8 +526,8 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
503526
def::DefStaticMethod(..) => {
504527
Ok(self.cat_rvalue_node(id, span, expr_ty))
505528
}
506-
def::DefMod(_) | def::DefForeignMod(_) | def::DefStatic(_, false) |
507-
def::DefUse(_) | def::DefTrait(_) | def::DefTy(_) | def::DefPrimTy(_) |
529+
def::DefMod(_) | def::DefForeignMod(_) | def::DefUse(_) |
530+
def::DefTrait(_) | def::DefTy(_) | def::DefPrimTy(_) |
508531
def::DefTyParam(..) | def::DefTyParamBinder(..) | def::DefRegion(_) |
509532
def::DefLabel(_) | def::DefSelfTy(..) | def::DefMethod(..) => {
510533
Ok(Rc::new(cmt_ {
@@ -516,30 +539,25 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
516539
}))
517540
}
518541

519-
def::DefStatic(_, true) => {
542+
def::DefStatic(_, _) => {
520543
Ok(Rc::new(cmt_ {
521544
id:id,
522545
span:span,
523546
cat:cat_static_item,
524-
mutbl: McDeclared,
547+
mutbl: MutabilityCategory::from_def(&def),
525548
ty:expr_ty
526549
}))
527550
}
528551

529-
def::DefArg(vid, binding_mode) => {
552+
def::DefArg(vid, _) => {
530553
// Idea: make this could be rewritten to model by-ref
531554
// stuff as `&const` and `&mut`?
532555

533-
// m: mutability of the argument
534-
let m = match binding_mode {
535-
ast::BindByValue(ast::MutMutable) => McDeclared,
536-
_ => McImmutable
537-
};
538556
Ok(Rc::new(cmt_ {
539557
id: id,
540558
span: span,
541559
cat: cat_arg(vid),
542-
mutbl: m,
560+
mutbl: MutabilityCategory::from_def(&def),
543561
ty:expr_ty
544562
}))
545563
}
@@ -564,7 +582,6 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
564582
if var_is_refd {
565583
self.cat_upvar(id, span, var_id, fn_node_id)
566584
} else {
567-
// FIXME #2152 allow mutation of moved upvars
568585
Ok(Rc::new(cmt_ {
569586
id:id,
570587
span:span,
@@ -573,13 +590,12 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
573590
onceness: closure_ty.onceness,
574591
capturing_proc: fn_node_id,
575592
}),
576-
mutbl:McImmutable,
593+
mutbl: MutabilityCategory::from_def(&def),
577594
ty:expr_ty
578595
}))
579596
}
580597
}
581598
ty::ty_unboxed_closure(_) => {
582-
// FIXME #2152 allow mutation of moved upvars
583599
Ok(Rc::new(cmt_ {
584600
id: id,
585601
span: span,
@@ -588,7 +604,7 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
588604
onceness: ast::Many,
589605
capturing_proc: fn_node_id,
590606
}),
591-
mutbl: McImmutable,
607+
mutbl: MutabilityCategory::from_def(&def),
592608
ty: expr_ty
593609
}))
594610
}
@@ -602,19 +618,14 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
602618
}
603619
}
604620

605-
def::DefLocal(vid, binding_mode) |
606-
def::DefBinding(vid, binding_mode) => {
621+
def::DefLocal(vid, _) |
622+
def::DefBinding(vid, _) => {
607623
// by-value/by-ref bindings are local variables
608-
let m = match binding_mode {
609-
ast::BindByValue(ast::MutMutable) => McDeclared,
610-
_ => McImmutable
611-
};
612-
613624
Ok(Rc::new(cmt_ {
614625
id: id,
615626
span: span,
616627
cat: cat_local(vid),
617-
mutbl: m,
628+
mutbl: MutabilityCategory::from_def(&def),
618629
ty: expr_ty
619630
}))
620631
}

0 commit comments

Comments
 (0)