Skip to content

rustc_typeck: do not overlap a borrow of TypeckTables with method lookup. #42480

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

Merged
merged 1 commit into from
Jun 7, 2017
Merged
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
9 changes: 7 additions & 2 deletions src/librustc_typeck/check/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions src/test/run-pass/issue-42463.rs
Original file line number Diff line number Diff line change
@@ -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]);
}