]> git.lizzy.rs Git - rust.git/commitdiff
rustc_typeck: do not overlap a borrow of TypeckTables with method lookup.
authorEduard-Mihai Burtescu <edy.burt@gmail.com>
Tue, 6 Jun 2017 15:50:21 +0000 (18:50 +0300)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Tue, 6 Jun 2017 18:48:16 +0000 (21:48 +0300)
src/librustc_typeck/check/method/confirm.rs
src/test/run-pass/issue-42463.rs [new file with mode: 0644]

index c8815f3df5aa25ecda4a4772b287a9b1eec84c21..36bd665738951ff3c29a47871b72be393771ea83 100644 (file)
@@ -446,9 +446,13 @@ fn convert_lvalue_derefs_to_mutable(&self) {
             // 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 @@ fn convert_lvalue_derefs_to_mutable(&self) {
                     }
                     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 (file)
index 0000000..7182fc2
--- /dev/null
@@ -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]);
+}