Skip to content

Commit bf2d3c8

Browse files
committed
Reborrow even when passing &Object to &Object, so as to permit freezing
1 parent afb2077 commit bf2d3c8

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/librustc/middle/typeck/check/regionck.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
391391
let target_ty = rcx.resolve_node_type(expr.id);
392392
match ty::get(target_ty).sty {
393393
ty::ty_trait(_, _, ty::RegionTraitStore(trait_region), _, _) => {
394-
let source_ty = rcx.fcx.expr_ty(source);
394+
let source_ty = rcx.resolve_expr_type_adjusted(source);
395395
constrain_regions_in_type(
396396
rcx,
397397
trait_region,
@@ -1153,17 +1153,20 @@ pub mod guarantor {
11531153
match ty::get(ty).sty {
11541154
ty::ty_rptr(r, _) |
11551155
ty::ty_evec(_, ty::vstore_slice(r)) |
1156+
ty::ty_trait(_, _, ty::RegionTraitStore(r), _, _) |
11561157
ty::ty_estr(ty::vstore_slice(r)) => {
11571158
BorrowedPointer(r)
11581159
}
11591160
ty::ty_uniq(*) |
11601161
ty::ty_estr(ty::vstore_uniq) |
1162+
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
11611163
ty::ty_evec(_, ty::vstore_uniq) => {
11621164
OwnedPointer
11631165
}
11641166
ty::ty_box(*) |
11651167
ty::ty_ptr(*) |
11661168
ty::ty_evec(_, ty::vstore_box) |
1169+
ty::ty_trait(_, _, ty::BoxTraitStore, _, _) |
11671170
ty::ty_estr(ty::vstore_box) => {
11681171
OtherPointer
11691172
}

src/librustc/middle/typeck/infer/coercion.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,6 @@ impl Coerce {
285285
let r_a = self.infcx.next_region_var(Coercion(self.trace));
286286

287287
let a_borrowed = match *sty_a {
288-
ty::ty_trait(_, _, ty::RegionTraitStore(_), _, _) => {
289-
return self.subtype(a, b);
290-
}
291288
ty::ty_trait(did, ref substs, _, _, b) => {
292289
ty::mk_trait(tcx, did, substs.clone(),
293290
ty::RegionTraitStore(r_a), b_mutbl, b)
@@ -297,7 +294,7 @@ impl Coerce {
297294
}
298295
};
299296

300-
if_ok!(self.tys(a_borrowed, b));
297+
if_ok!(self.subtype(a_borrowed, b));
301298
Ok(Some(@AutoDerefRef(AutoDerefRef {
302299
autoderefs: 0,
303300
autoref: Some(AutoBorrowObj(r_a, b_mutbl))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2012 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 we can coerce an `@Object` to an `&Object`
12+
13+
trait Foo {
14+
fn foo(&self) -> uint;
15+
fn bar(&mut self) -> uint;
16+
}
17+
18+
impl Foo for uint {
19+
fn foo(&self) -> uint {
20+
*self
21+
}
22+
23+
fn bar(&mut self) -> uint {
24+
*self += 1;
25+
*self
26+
}
27+
}
28+
29+
fn do_it_mut(obj: &mut Foo) {
30+
let x = obj.bar();
31+
let y = obj.foo();
32+
assert_eq!(x, y);
33+
34+
do_it_imm(obj, y);
35+
}
36+
37+
fn do_it_imm(obj: &Foo, v: uint) {
38+
let y = obj.foo();
39+
assert_eq!(v, y);
40+
}
41+
42+
fn main() {
43+
let mut x = 22_u;
44+
let obj = &mut x as &mut Foo;
45+
do_it_mut(obj);
46+
do_it_imm(obj, 23u);
47+
do_it_mut(obj);
48+
}

0 commit comments

Comments
 (0)