Skip to content

Commit 2ea3875

Browse files
committed
auto merge of #19617 : nikomatsakis/rust/issue-19261-2, r=nrc
**First commit.** Patch up debruijn indices. Fixes #19537. **Second commit.** Stop reborrowing so much. Fixes #19147. Fixes #19261. r? @nick29581
2 parents d2e2bd1 + 061a87e commit 2ea3875

File tree

8 files changed

+164
-23
lines changed

8 files changed

+164
-23
lines changed

src/librustc_trans/trans/datum.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,10 @@ impl<'tcx, K: KindOps + fmt::Show> Datum<'tcx, K> {
535535
/// Copies the value into a new location. This function always preserves the existing datum as
536536
/// a valid value. Therefore, it does not consume `self` and, also, cannot be applied to affine
537537
/// values (since they must never be duplicated).
538-
pub fn shallow_copy<'blk, 'tcx>(&self,
539-
bcx: Block<'blk, 'tcx>,
540-
dst: ValueRef)
541-
-> Block<'blk, 'tcx> {
538+
pub fn shallow_copy<'blk>(&self,
539+
bcx: Block<'blk, 'tcx>,
540+
dst: ValueRef)
541+
-> Block<'blk, 'tcx> {
542542
/*!
543543
* Copies the value into a new location. This function always
544544
* preserves the existing datum as a valid value. Therefore,

src/librustc_typeck/astconv.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use middle::resolve_lifetime as rl;
5454
use middle::subst::{FnSpace, TypeSpace, AssocSpace, SelfSpace, Subst, Substs};
5555
use middle::subst::{VecPerParamSpace};
5656
use middle::ty::{mod, Ty};
57+
use middle::ty_fold;
5758
use rscope::{mod, UnelidableRscope, RegionScope, SpecificRscope,
5859
ShiftedRscope, BindingRscope};
5960
use TypeAndSubsts;
@@ -1062,7 +1063,8 @@ fn ty_of_method_or_bare_fn<'a, 'tcx, AC: AstConv<'tcx>>(
10621063
opt_self_info: Option<SelfInfo<'a, 'tcx>>,
10631064
decl: &ast::FnDecl)
10641065
-> (ty::BareFnTy<'tcx>,
1065-
Option<ty::ExplicitSelfCategory>) {
1066+
Option<ty::ExplicitSelfCategory>)
1067+
{
10661068
debug!("ty_of_method_or_bare_fn");
10671069

10681070
// New region names that appear inside of the arguments of the function
@@ -1078,6 +1080,11 @@ fn ty_of_method_or_bare_fn<'a, 'tcx, AC: AstConv<'tcx>>(
10781080
let (self_ty, mut implied_output_region) = match opt_self_info {
10791081
None => (None, None),
10801082
Some(self_info) => {
1083+
// Shift regions in the self type by 1 to account for the binding
1084+
// level introduced by the function itself.
1085+
let untransformed_self_ty =
1086+
ty_fold::shift_regions(this.tcx(), 1, &self_info.untransformed_self_ty);
1087+
10811088
// Figure out and record the explicit self category.
10821089
let explicit_self_category =
10831090
determine_explicit_self_category(this, &rb, &self_info);
@@ -1087,21 +1094,19 @@ fn ty_of_method_or_bare_fn<'a, 'tcx, AC: AstConv<'tcx>>(
10871094
(None, None)
10881095
}
10891096
ty::ByValueExplicitSelfCategory => {
1090-
(Some(self_info.untransformed_self_ty), None)
1097+
(Some(untransformed_self_ty), None)
10911098
}
10921099
ty::ByReferenceExplicitSelfCategory(region, mutability) => {
10931100
(Some(ty::mk_rptr(this.tcx(),
10941101
region,
10951102
ty::mt {
1096-
ty: self_info.untransformed_self_ty,
1103+
ty: untransformed_self_ty,
10971104
mutbl: mutability
10981105
})),
10991106
Some(region))
11001107
}
11011108
ty::ByBoxExplicitSelfCategory => {
1102-
(Some(ty::mk_uniq(this.tcx(),
1103-
self_info.untransformed_self_ty)),
1104-
None)
1109+
(Some(ty::mk_uniq(this.tcx(), untransformed_self_ty)), None)
11051110
}
11061111
}
11071112
}

src/librustc_typeck/check/method/mod.rs

+1
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

+27-12
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,

src/librustc_typeck/collect.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) {
10551055
ref selfty,
10561056
ref impl_items) => {
10571057
// Create generics from the generics specified in the impl head.
1058-
let ty_generics = ty_generics_for_type(
1058+
let ty_generics = ty_generics_for_impl(
10591059
ccx,
10601060
generics,
10611061
CreateTypeParametersForAssociatedTypes);
@@ -1655,6 +1655,24 @@ fn ty_generics_for_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
16551655
generics
16561656
}
16571657

1658+
fn ty_generics_for_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
1659+
generics: &ast::Generics,
1660+
create_type_parameters_for_associated_types:
1661+
CreateTypeParametersForAssociatedTypesFlag)
1662+
-> ty::Generics<'tcx>
1663+
{
1664+
let early_lifetimes = resolve_lifetime::early_bound_lifetimes(generics);
1665+
debug!("ty_generics_for_impl: early_lifetimes={}",
1666+
early_lifetimes);
1667+
ty_generics(ccx,
1668+
subst::TypeSpace,
1669+
early_lifetimes.as_slice(),
1670+
generics.ty_params.as_slice(),
1671+
ty::Generics::empty(),
1672+
&generics.where_clause,
1673+
create_type_parameters_for_associated_types)
1674+
}
1675+
16581676
fn ty_generics_for_fn_or_method<'tcx,AC>(
16591677
this: &AC,
16601678
generics: &ast::Generics,
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() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 the case where the `Self` type has a bound lifetime that must
12+
// be adjusted in the fn signature. Issue #19537.
13+
14+
use std::collections::HashMap;
15+
16+
struct Foo<'a> {
17+
map: HashMap<uint, &'a str>
18+
}
19+
20+
impl<'a> Foo<'a> {
21+
fn new() -> Foo<'a> { panic!() }
22+
fn insert(&'a mut self) { }
23+
}
24+
fn main() {
25+
let mut foo = Foo::new();
26+
foo.insert();
27+
foo.insert(); //~ ERROR cannot borrow
28+
}
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)