Skip to content

Commit 31e46ac

Browse files
committed
During method resolution, only reborrow if we are not doing an auto-ref.
The current behavior leads to adjustments like `&&*` being applied instead of just `&` (when the unmodified receiver is a `&T` or an `&mut T`). This causes both safety errors and unexpected behavior. The safety errors result from regionck not being prepared for auto-ref-ref-like adjustments; this is worth fixing on its own, but I think the best way to do it is to modify regionck to use expr-use-visitor (and fix expr-use-visitor as well, which I don't think properly invokes `borrow` for each level of auto-ref), and for now it's simpler to just not produce the adjustment in question. (I have a separate patch porting regionck to use exprusevisitor for a different bug, so that is coming.)
1 parent ca98fef commit 31e46ac

File tree

4 files changed

+102
-12
lines changed

4 files changed

+102
-12
lines changed

src/librustc_typeck/check/method/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub fn lookup<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
100100
call_expr.repr(fcx.tcx()),
101101
self_expr.repr(fcx.tcx()));
102102

103+
let self_ty = fcx.infcx().resolve_type_vars_if_possible(self_ty);
103104
let pick = try!(probe::probe(fcx, span, method_name, self_ty, call_expr.id));
104105
Ok(confirm::confirm(fcx, span, self_expr, call_expr, self_ty, pick, supplied_method_types))
105106
}

src/librustc_typeck/check/method/probe.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn create_steps<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
168168
check::autoderef(
169169
fcx, span, self_ty, None, NoPreference,
170170
|t, d| {
171-
let adjustment = consider_reborrow(t, d);
171+
let adjustment = AutoDeref(d);
172172
steps.push(CandidateStep { self_ty: t, adjustment: adjustment });
173173
None::<()> // keep iterating until we can't anymore
174174
});
@@ -185,14 +185,6 @@ fn create_steps<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
185185
}
186186

187187
return steps;
188-
189-
fn consider_reborrow(ty: Ty, d: uint) -> PickAdjustment {
190-
// Insert a `&*` or `&mut *` if this is a reference type:
191-
match ty.sty {
192-
ty::ty_rptr(_, ref mt) => AutoRef(mt.mutbl, box AutoDeref(d+1)),
193-
_ => AutoDeref(d),
194-
}
195-
}
196188
}
197189

198190
impl<'a,'tcx> ProbeContext<'a,'tcx> {
@@ -626,7 +618,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
626618
return None;
627619
}
628620

629-
match self.pick_adjusted_method(step) {
621+
match self.pick_by_value_method(step) {
630622
Some(result) => return Some(result),
631623
None => {}
632624
}
@@ -644,11 +636,34 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
644636
}
645637
}
646638

647-
fn pick_adjusted_method(&mut self,
639+
fn pick_by_value_method(&mut self,
648640
step: &CandidateStep<'tcx>)
649641
-> Option<PickResult<'tcx>>
650642
{
651-
self.pick_method(step.self_ty).map(|r| self.adjust(r, step.adjustment.clone()))
643+
/*!
644+
* For each type `T` in the step list, this attempts to find a
645+
* method where the (transformed) self type is exactly `T`. We
646+
* do however do one transformation on the adjustment: if we
647+
* are passing a region pointer in, we will potentially
648+
* *reborrow* it to a shorter lifetime. This allows us to
649+
* transparently pass `&mut` pointers, in particular, without
650+
* consuming them for their entire lifetime.
651+
*/
652+
653+
let adjustment = match step.adjustment {
654+
AutoDeref(d) => consider_reborrow(step.self_ty, d),
655+
AutoUnsizeLength(..) | AutoRef(..) => step.adjustment.clone(),
656+
};
657+
658+
return self.pick_method(step.self_ty).map(|r| self.adjust(r, adjustment.clone()));
659+
660+
fn consider_reborrow(ty: Ty, d: uint) -> PickAdjustment {
661+
// Insert a `&*` or `&mut *` if this is a reference type:
662+
match ty.sty {
663+
ty::ty_rptr(_, ref mt) => AutoRef(mt.mutbl, box AutoDeref(d+1)),
664+
_ => AutoDeref(d),
665+
}
666+
}
652667
}
653668

654669
fn pick_autorefd_method(&mut self,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Check that when we clone a `&T` pointer we properly relate the
12+
// lifetime of the pointer which results to the pointer being cloned.
13+
// Bugs in method resolution have sometimes broken this connection.
14+
// Issue #19261.
15+
16+
fn leak<'a, T>(x: T) -> &'a T {
17+
(&x).clone() //~ ERROR `x` does not live long enough
18+
}
19+
20+
fn main() { }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that an `&mut self` method, when invoked on an lvalue whose
12+
// type is `&mut [u8]`, passes in a pointer to the lvalue and not a
13+
// temporary. Issue #19147.
14+
15+
use std::raw;
16+
use std::mem;
17+
use std::slice;
18+
use std::io::IoResult;
19+
20+
trait MyWriter {
21+
fn my_write(&mut self, buf: &[u8]) -> IoResult<()>;
22+
}
23+
24+
impl<'a> MyWriter for &'a mut [u8] {
25+
fn my_write(&mut self, buf: &[u8]) -> IoResult<()> {
26+
slice::bytes::copy_memory(*self, buf);
27+
28+
let write_len = buf.len();
29+
unsafe {
30+
*self = mem::transmute(raw::Slice {
31+
data: self.as_ptr().offset(write_len as int),
32+
len: self.len() - write_len,
33+
});
34+
}
35+
36+
Ok(())
37+
}
38+
}
39+
40+
fn main() {
41+
let mut buf = [0_u8, .. 6];
42+
43+
{
44+
let mut writer = buf.as_mut_slice();
45+
writer.my_write(&[0, 1, 2]).unwrap();
46+
writer.my_write(&[3, 4, 5]).unwrap();
47+
}
48+
49+
// If `my_write` is not modifying `buf` in place, then we will
50+
// wind up with `[3, 4, 5, 0, 0, 0]` because the first call to
51+
// `my_write()` doesn't update the starting point for the write.
52+
53+
assert_eq!(buf, [0, 1, 2, 3, 4, 5]);
54+
}

0 commit comments

Comments
 (0)