From b02e3a165c4f6b7bc3d2158cef2cb46f5b91e14a Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Tue, 6 Jun 2017 18:50:21 +0300 Subject: [PATCH] rustc_typeck: do not overlap a borrow of TypeckTables with method lookup. --- src/librustc_typeck/check/method/confirm.rs | 9 ++++- src/test/run-pass/issue-42463.rs | 41 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/test/run-pass/issue-42463.rs diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index c8815f3df5a..36bd6657389 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -446,9 +446,13 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> { // overloaded lvalue ops, and will be fixed by them in order to get // the correct region. let mut source = self.node_ty(expr.id); - if let Some(adjustments) = self.tables.borrow_mut().adjustments.get_mut(&expr.id) { + // Do not mutate adjustments in place, but rather take them, + // and replace them after mutating them, to avoid having the + // tables borrowed during (`deref_mut`) method resolution. + let previous_adjustments = self.tables.borrow_mut().adjustments.remove(&expr.id); + if let Some(mut adjustments) = previous_adjustments { let pref = LvaluePreference::PreferMutLvalue; - for adjustment in adjustments { + for adjustment in &mut adjustments { if let Adjust::Deref(Some(ref mut deref)) = adjustment.kind { if let Some(ok) = self.try_overloaded_deref(expr.span, source, pref) { let method = self.register_infer_ok_obligations(ok); @@ -462,6 +466,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> { } source = adjustment.target; } + self.tables.borrow_mut().adjustments.insert(expr.id, adjustments); } match expr.node { diff --git a/src/test/run-pass/issue-42463.rs b/src/test/run-pass/issue-42463.rs new file mode 100644 index 00000000000..7182fc213f7 --- /dev/null +++ b/src/test/run-pass/issue-42463.rs @@ -0,0 +1,41 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::ops::{Deref, DerefMut}; + +struct CheckedDeref { + value: T, + check: F +} + +impl bool, T> Deref for CheckedDeref { + type Target = T; + fn deref(&self) -> &T { + assert!((self.check)(&self.value)); + &self.value + } +} + +impl bool, T> DerefMut for CheckedDeref { + fn deref_mut(&mut self) -> &mut T { + assert!((self.check)(&self.value)); + &mut self.value + } +} + + +fn main() { + let mut v = CheckedDeref { + value: vec![0], + check: |v: &Vec<_>| !v.is_empty() + }; + v.push(1); + assert_eq!(*v, vec![0, 1]); +}