Skip to content
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

Compute type fragments left over from moves #17439

Closed
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
15 changes: 15 additions & 0 deletions src/librustc/driver/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ impl Session {
pub fn span_err_with_code(&self, sp: Span, msg: &str, code: &str) {
self.diagnostic().span_err_with_code(sp, msg, code)
}
pub fn span_end_err(&self, sp: Span, msg: &str) {
self.diagnostic().span_end_err(sp, msg)
}
pub fn err(&self, msg: &str) {
self.diagnostic().handler().err(msg)
}
Expand All @@ -90,6 +93,12 @@ impl Session {
pub fn warn(&self, msg: &str) {
self.diagnostic().handler().warn(msg)
}
pub fn opt_span_warn(&self, opt_sp: Option<Span>, msg: &str) {
match opt_sp {
Some(sp) => self.span_warn(sp, msg),
None => self.warn(msg),
}
}
pub fn span_note(&self, sp: Span, msg: &str) {
self.diagnostic().span_note(sp, msg)
}
Expand All @@ -102,6 +111,12 @@ impl Session {
pub fn note(&self, msg: &str) {
self.diagnostic().handler().note(msg)
}
pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
match opt_sp {
Some(sp) => self.span_bug(sp, msg),
None => self.bug(msg),
}
}
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
self.diagnostic().span_bug(sp, msg)
}
Expand Down
33 changes: 23 additions & 10 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ fn owned_ptr_base_path<'a>(loan_path: &'a LoanPath) -> &'a LoanPath {
};

fn owned_ptr_base_path_helper<'a>(loan_path: &'a LoanPath) -> Option<&'a LoanPath> {
match *loan_path {
LpVar(_) | LpUpvar(_) => None,
match loan_path.variant {
LpVar(_) | LpUpvar(..) => None,
LpExtend(ref lp_base, _, LpDeref(mc::OwnedPtr)) => {
match owned_ptr_base_path_helper(&**lp_base) {
v @ Some(_) => v,
None => Some(&**lp_base)
}
}
LpDowncast(ref lp_base, _) |
LpExtend(ref lp_base, _, _) => owned_ptr_base_path_helper(&**lp_base)
}
}
Expand All @@ -66,14 +67,15 @@ fn owned_ptr_base_path_rc(loan_path: &Rc<LoanPath>) -> Rc<LoanPath> {
};

fn owned_ptr_base_path_helper(loan_path: &Rc<LoanPath>) -> Option<Rc<LoanPath>> {
match **loan_path {
LpVar(_) | LpUpvar(_) => None,
match loan_path.variant {
LpVar(_) | LpUpvar(..) => None,
LpExtend(ref lp_base, _, LpDeref(mc::OwnedPtr)) => {
match owned_ptr_base_path_helper(lp_base) {
v @ Some(_) => v,
None => Some(lp_base.clone())
}
}
LpDowncast(ref lp_base, _) |
LpExtend(ref lp_base, _, _) => owned_ptr_base_path_helper(lp_base)
}
}
Expand All @@ -98,6 +100,11 @@ impl<'a, 'tcx> euv::Delegate for CheckLoanCtxt<'a, 'tcx> {
self.consume_common(consume_id, consume_span, cmt, mode);
}

fn matched_pat(&mut self,
_matched_pat: &ast::Pat,
_cmt: mc::cmt,
_mode: euv::MatchMode) { }

fn consume_pat(&mut self,
consume_pat: &ast::Pat,
cmt: mc::cmt,
Expand Down Expand Up @@ -293,10 +300,11 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {

let mut loan_path = loan_path;
loop {
match *loan_path {
LpVar(_) | LpUpvar(_) => {
match loan_path.variant {
LpVar(_) | LpUpvar(..) => {
break;
}
LpDowncast(ref lp_base, _) |
LpExtend(ref lp_base, _, _) => {
loan_path = &**lp_base;
}
Expand Down Expand Up @@ -686,10 +694,15 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
* (*p).x = 22; // not ok, p is uninitialized, can't deref
*/

match **lp {
LpVar(_) | LpUpvar(_) => {
match lp.variant {
LpVar(_) | LpUpvar(..) => {
// assigning to `x` does not require that `x` is initialized
}
LpDowncast(ref lp_base, _) => {
// assigning to `(P->Variant).f` is ok if assigning to `P` is ok
self.check_if_assigned_path_is_moved(id, span,
use_kind, lp_base);
}
LpExtend(ref lp_base, _, LpInterior(_)) => {
// assigning to `P.f` is ok if assigning to `P` is ok
self.check_if_assigned_path_is_moved(id, span,
Expand Down Expand Up @@ -774,7 +787,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
loop {
debug!("mark_variable_as_used_mut(cmt={})", cmt.repr(this.tcx()));
match cmt.cat.clone() {
mc::cat_copied_upvar(mc::CopiedUpvar { upvar_id: id, .. }) |
mc::cat_copied_upvar(mc::CopiedUpvar { upvar_id: ty::UpvarId { var_id: id, .. }, .. }) |
mc::cat_local(id) | mc::cat_arg(id) => {
this.tcx().used_mut_nodes.borrow_mut().insert(id);
return;
Expand Down Expand Up @@ -804,7 +817,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
cmt = b;
}

mc::cat_downcast(b) |
mc::cat_downcast(b, _) |
mc::cat_interior(b, _) => {
assert_eq!(cmt.mutbl, mc::McInherited);
cmt = b;
Expand Down
34 changes: 32 additions & 2 deletions src/librustc/middle/borrowck/gather_loans/gather_moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub fn gather_decl(bccx: &BorrowckCtxt,
decl_id: ast::NodeId,
_decl_span: Span,
var_id: ast::NodeId) {
let loan_path = Rc::new(LpVar(var_id));
let ty = ty::node_id_to_type(bccx.tcx, var_id);
let loan_path = Rc::new(LoanPath::new(LpVar(var_id), ty));
move_data.add_move(bccx.tcx, loan_path, decl_id, Declared);
}

Expand All @@ -60,6 +61,35 @@ pub fn gather_move_from_expr(bccx: &BorrowckCtxt,
gather_move(bccx, move_data, move_error_collector, move_info);
}

pub fn gather_match_variant(bccx: &BorrowckCtxt,
move_data: &MoveData,
_move_error_collector: &MoveErrorCollector,
move_pat: &ast::Pat,
cmt: mc::cmt,
mode: euv::MatchMode) {
debug!("gather_match_variant(move_pat={}, cmt={}, mode={})",
move_pat.id, cmt.repr(bccx.tcx), mode);

let opt_lp = opt_loan_path(&cmt);
let opt_base_lp = match cmt.cat {
mc::cat_downcast(ref base_cmt, _variant_def_id) => opt_loan_path(base_cmt),
_ => fail!("should only encounter move_into_variant on cat_downcast."),
};
match (opt_lp, opt_base_lp) {
(Some(loan_path), Some(base_loan_path)) => {
move_data.add_variant_match(bccx.tcx,
loan_path,
move_pat.id,
base_loan_path,
mode);
}
(lp, base_lp) => {
debug!("add_variant_match body for ({:?}, {:?}) NOT YET IMPLEMENTED", lp, base_lp);
}
}

}

pub fn gather_move_from_pat(bccx: &BorrowckCtxt,
move_data: &MoveData,
move_error_collector: &MoveErrorCollector,
Expand Down Expand Up @@ -152,7 +182,7 @@ fn check_and_get_illegal_move_origin(bccx: &BorrowckCtxt,
None
}

mc::cat_downcast(ref b) |
mc::cat_downcast(ref b, _) |
mc::cat_interior(ref b, _) => {
match ty::get(b.ty).sty {
ty::ty_struct(did, _) | ty::ty_enum(did, _) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/borrowck/gather_loans/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<'a, 'tcx> GuaranteeLifetimeContext<'a, 'tcx> {
Ok(())
}

mc::cat_downcast(ref base) |
mc::cat_downcast(ref base, _) |
mc::cat_deref(ref base, _, mc::OwnedPtr) | // L-Deref-Send
mc::cat_interior(ref base, _) | // L-Field
mc::cat_deref(ref base, _, mc::GcPtr) => {
Expand Down Expand Up @@ -185,7 +185,7 @@ impl<'a, 'tcx> GuaranteeLifetimeContext<'a, 'tcx> {
mc::cat_deref(_, _, mc::Implicit(_, r)) => {
r
}
mc::cat_downcast(ref cmt) |
mc::cat_downcast(ref cmt, _) |
mc::cat_deref(ref cmt, _, mc::OwnedPtr) |
mc::cat_deref(ref cmt, _, mc::GcPtr) |
mc::cat_interior(ref cmt, _) |
Expand Down
23 changes: 21 additions & 2 deletions src/librustc/middle/borrowck/gather_loans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ impl<'a, 'tcx> euv::Delegate for GatherLoanCtxt<'a, 'tcx> {
}
}

fn matched_pat(&mut self,
matched_pat: &ast::Pat,
cmt: mc::cmt,
mode: euv::MatchMode) {
debug!("matched_pat(matched_pat={}, cmt={}, mode={})",
matched_pat.repr(self.tcx()),
cmt.repr(self.tcx()),
mode);

match cmt.cat {
mc::cat_downcast(..) =>
gather_moves::gather_match_variant(
self.bccx, &self.move_data, &self.move_error_collector,
matched_pat, cmt, mode),
_ => {}
}
}

fn consume_pat(&mut self,
consume_pat: &ast::Pat,
cmt: mc::cmt,
Expand Down Expand Up @@ -396,11 +414,12 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
//! For mutable loans of content whose mutability derives
//! from a local variable, mark the mutability decl as necessary.

match *loan_path {
match loan_path.variant {
LpVar(local_id) |
LpUpvar(ty::UpvarId{ var_id: local_id, closure_expr_id: _ }) => {
LpUpvar(ty::UpvarId{ var_id: local_id, closure_expr_id: _ }, _) => {
self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
}
LpDowncast(ref base, _) |
LpExtend(ref base, mc::McInherited, _) => {
self.mark_loan_path_as_mutated(&**base);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/borrowck/gather_loans/move_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn report_cannot_move_out_of(bccx: &BorrowckCtxt, move_from: mc::cmt) {
bccx.cmt_to_string(&*move_from)).as_slice());
}

mc::cat_downcast(ref b) |
mc::cat_downcast(ref b, _) |
mc::cat_interior(ref b, _) => {
match ty::get(b.ty).sty {
ty::ty_struct(did, _)
Expand Down
22 changes: 13 additions & 9 deletions src/librustc/middle/borrowck/gather_loans/restrictions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
cmt: mc::cmt) -> RestrictionResult {
debug!("restrict(cmt={})", cmt.repr(self.bccx.tcx));

let new_lp = |v: LoanPathVariant| Rc::new(LoanPath::new(v, cmt.ty));

match cmt.cat.clone() {
mc::cat_rvalue(..) => {
// Effectively, rvalues are stored into a
Expand All @@ -69,23 +71,23 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
mc::cat_local(local_id) |
mc::cat_arg(local_id) => {
// R-Variable, locally declared
let lp = Rc::new(LpVar(local_id));
let lp = new_lp(LpVar(local_id));
SafeIf(lp.clone(), vec![lp])
}

mc::cat_upvar(upvar_id, _) => {
// R-Variable, captured into closure
let lp = Rc::new(LpUpvar(upvar_id));
let lp = new_lp(LpUpvar(upvar_id, CaptureByRef));
SafeIf(lp.clone(), vec![lp])
}

mc::cat_copied_upvar(mc::CopiedUpvar { upvar_id, .. }) => {
// R-Variable, copied/moved into closure
let lp = Rc::new(LpVar(upvar_id));
let lp = new_lp(LpUpvar(upvar_id, CaptureByVal));
SafeIf(lp.clone(), vec![lp])
}

mc::cat_downcast(cmt_base) => {
mc::cat_downcast(cmt_base, _) => {
// When we borrow the interior of an enum, we have to
// ensure the enum itself is not mutated, because that
// could cause the type of the memory to change.
Expand All @@ -99,7 +101,7 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
// the memory, so no additional restrictions are
// needed.
let result = self.restrict(cmt_base);
self.extend(result, cmt.mutbl, LpInterior(i))
self.extend(result, cmt.mutbl, LpInterior(i), cmt.ty)
}

mc::cat_deref(cmt_base, _, pk @ mc::OwnedPtr) |
Expand All @@ -115,7 +117,7 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
// Eventually we should make these non-special and
// just rely on Deref<T> implementation.
let result = self.restrict(cmt_base);
self.extend(result, cmt.mutbl, LpDeref(pk))
self.extend(result, cmt.mutbl, LpDeref(pk), cmt.ty)
}

mc::cat_static_item(..) => {
Expand Down Expand Up @@ -157,7 +159,7 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
}

let result = self.restrict(cmt_base);
self.extend(result, cmt.mutbl, LpDeref(pk))
self.extend(result, cmt.mutbl, LpDeref(pk), cmt.ty)
}
mc::UnsafePtr(..) => {
// We are very trusting when working with unsafe
Expand All @@ -181,11 +183,13 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
fn extend(&self,
result: RestrictionResult,
mc: mc::MutabilityCategory,
elem: LoanPathElem) -> RestrictionResult {
elem: LoanPathElem,
ty: ty::t) -> RestrictionResult {
match result {
Safe => Safe,
SafeIf(base_lp, mut base_vec) => {
let lp = Rc::new(LpExtend(base_lp, mc, elem));
let v = LpExtend(base_lp, mc, elem);
let lp = Rc::new(LoanPath::new(v, ty));
base_vec.push(lp.clone());
SafeIf(lp, base_vec)
}
Expand Down
Loading