diff options
| author | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2017-06-06 18:50:21 +0300 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2017-06-06 21:48:16 +0300 |
| commit | b02e3a165c4f6b7bc3d2158cef2cb46f5b91e14a (patch) | |
| tree | d5f8076019d77832ee500da8281941ef6cd2113b | |
| parent | c94a9ac8ae33e6580940e02abb425dd2fe69b5d8 (diff) | |
| download | rust-b02e3a165c4f6b7bc3d2158cef2cb46f5b91e14a.tar.gz rust-b02e3a165c4f6b7bc3d2158cef2cb46f5b91e14a.zip | |
rustc_typeck: do not overlap a borrow of TypeckTables with method lookup.
| -rw-r--r-- | src/librustc_typeck/check/method/confirm.rs | 9 | ||||
| -rw-r--r-- | src/test/run-pass/issue-42463.rs | 41 |
2 files changed, 48 insertions, 2 deletions
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 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::ops::{Deref, DerefMut}; + +struct CheckedDeref<T, F> { + value: T, + check: F +} + +impl<F: Fn(&T) -> bool, T> Deref for CheckedDeref<T, F> { + type Target = T; + fn deref(&self) -> &T { + assert!((self.check)(&self.value)); + &self.value + } +} + +impl<F: Fn(&T) -> bool, T> DerefMut for CheckedDeref<T, F> { + 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]); +} |
