Skip to content

Commit 5520ea8

Browse files
committed
Reviewer changes
1 parent 52d6d3b commit 5520ea8

File tree

7 files changed

+108
-65
lines changed

7 files changed

+108
-65
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,8 @@ fn trait_cast_types(fcx: &FnCtxt,
896896
match autoref {
897897
&ty::AutoUnsize(ref k) |
898898
&ty::AutoUnsizeUniq(ref k) => trait_cast_types_unsize(fcx, k, src_ty, sp),
899-
&ty::AutoPtr(_, _, Some(box ref autoref))
900-
| &ty::AutoUnsafe(_, Some(box ref autoref))=> {
899+
&ty::AutoPtr(_, _, Some(box ref autoref)) |
900+
&ty::AutoUnsafe(_, Some(box ref autoref)) => {
901901
trait_cast_types_autoref(fcx, autoref, src_ty, sp)
902902
}
903903
_ => None

src/librustc/middle/typeck/coherence.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,19 @@ fn get_base_type_def_id(inference_context: &InferCtxt,
157157
ty_unboxed_closure(def_id, _) => {
158158
Some(def_id)
159159
}
160-
ty_ptr(ty::mt {ty, ..}) | ty_rptr(_, ty::mt {ty, ..}) | ty_uniq(ty)
161-
=> match ty::get(ty).sty {
162-
ty_trait(box ty::TyTrait { def_id, .. }) => {
163-
Some(def_id)
164-
}
165-
_ => {
166-
fail!("get_base_type() returned a type that wasn't an \
167-
enum, struct, or trait");
160+
ty_ptr(ty::mt {ty, ..}) |
161+
ty_rptr(_, ty::mt {ty, ..}) |
162+
ty_uniq(ty) => {
163+
match ty::get(ty).sty {
164+
ty_trait(box ty::TyTrait { def_id, .. }) => {
165+
Some(def_id)
166+
}
167+
_ => {
168+
fail!("get_base_type() returned a type that wasn't an \
169+
enum, struct, or trait");
170+
}
168171
}
169-
},
172+
}
170173
ty_trait(box ty::TyTrait { def_id, .. }) => {
171174
Some(def_id)
172175
}

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

+26-38
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,7 @@ impl<'f> Coerce<'f> {
350350
}
351351
})
352352
}
353-
(&ty::ty_ptr(ty::mt{ty: t_a, ..}), &ty::ty_ptr(mt_b))
354-
| (&ty::ty_rptr(_, ty::mt{ty: t_a, ..}), &ty::ty_ptr(mt_b)) => {
353+
(&ty::ty_rptr(_, ty::mt{ty: t_a, ..}), &ty::ty_ptr(mt_b)) => {
355354
self.unpack_actual_value(t_a, |sty_a| {
356355
match self.unsize_ty(sty_a, mt_b.ty) {
357356
Some((ty, kind)) => {
@@ -478,63 +477,52 @@ impl<'f> Coerce<'f> {
478477
b.repr(tcx));
479478

480479
let coercion = Coercion(self.get_ref().trace.clone());
480+
let r_a = self.get_ref().infcx.next_region_var(coercion);
481481

482-
match *sty_a {
483-
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
484-
ty::ty_trait(box ty::TyTrait {
485-
def_id,
486-
ref substs,
487-
bounds,
488-
..
489-
}) => {
490-
let tr = ty::mk_trait(tcx, def_id, substs.clone(), bounds);
491-
let r_a = self.get_ref().infcx.next_region_var(coercion);
492-
let a_borrowed = ty::mk_rptr(tcx, r_a, ty::mt{ mutbl: b_mutbl, ty: tr });
493-
494-
try!(self.subtype(a_borrowed, b));
495-
Ok(Some(AutoDerefRef(AutoDerefRef {
496-
autoderefs: 1,
497-
autoref: Some(AutoPtr(r_a, b_mutbl, None))
498-
})))
499-
}
500-
_ => {
501-
self.subtype(a, b)
502-
}
503-
},
504-
_ => {
505-
self.subtype(a, b)
506-
}
507-
}
482+
self.coerce_object(a, sty_a, b,
483+
|tr| ty::mk_rptr(tcx, r_a, ty::mt{ mutbl: b_mutbl, ty: tr }),
484+
|| AutoPtr(r_a, b_mutbl, None))
508485
}
509486

510487
fn coerce_unsafe_object(&self,
511-
a: ty::t,
512-
sty_a: &ty::sty,
513-
b: ty::t,
514-
b_mutbl: ast::Mutability) -> CoerceResult
488+
a: ty::t,
489+
sty_a: &ty::sty,
490+
b: ty::t,
491+
b_mutbl: ast::Mutability) -> CoerceResult
515492
{
516493
let tcx = self.get_ref().infcx.tcx;
517494

518495
debug!("coerce_unsafe_object(a={}, sty_a={:?}, b={})",
519496
a.repr(tcx), sty_a,
520497
b.repr(tcx));
521498

499+
self.coerce_object(a, sty_a, b,
500+
|tr| ty::mk_ptr(tcx, ty::mt{ mutbl: b_mutbl, ty: tr }),
501+
|| AutoUnsafe(b_mutbl, None))
502+
}
503+
504+
fn coerce_object(&self,
505+
a: ty::t,
506+
sty_a: &ty::sty,
507+
b: ty::t,
508+
mk_ty: |ty::t| -> ty::t,
509+
mk_adjust: || -> ty::AutoRef) -> CoerceResult
510+
{
511+
let tcx = self.get_ref().infcx.tcx;
512+
522513
match *sty_a {
523-
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) |
524-
ty::ty_ptr(ty::mt{ty, ..}) => match ty::get(ty).sty {
514+
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
525515
ty::ty_trait(box ty::TyTrait {
526516
def_id,
527517
ref substs,
528518
bounds,
529519
..
530520
}) => {
531521
let tr = ty::mk_trait(tcx, def_id, substs.clone(), bounds);
532-
let a_raw = ty::mk_ptr(tcx, ty::mt{ mutbl: b_mutbl, ty: tr });
533-
534-
try!(self.subtype(a_raw, b));
522+
try!(self.subtype(mk_ty(tr), b));
535523
Ok(Some(AutoDerefRef(AutoDerefRef {
536524
autoderefs: 1,
537-
autoref: Some(AutoUnsafe(b_mutbl, None))
525+
autoref: Some(mk_adjust())
538526
})))
539527
}
540528
_ => {

src/test/compile-fail/dst-bad-coercions.rs

+29-6
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,44 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// Test implicit coercions involving DSTs and raw pointers.
12+
1113
struct S;
1214
trait T {}
1315
impl T for S {}
1416

17+
struct Foo<Sized? T> {
18+
f: T
19+
}
20+
1521
pub fn main() {
22+
// Test that we cannot convert from *-ptr to &-ptr
1623
let x: *const S = &S;
17-
let y: &S = x; //~ ERROR mismatched types: expected `&S`, found `*const S` (expected &-ptr
18-
let y: &T = x; //~ ERROR mismatched types: expected `&T`, found `*const S` (expected &-ptr
24+
let y: &S = x; //~ ERROR mismatched types
25+
let y: &T = x; //~ ERROR mismatched types
1926

27+
// Test that we cannot convert from *-ptr to &-ptr (mut version)
2028
let x: *mut S = &mut S;
21-
let y: &S = x; //~ ERROR mismatched types: expected `&S`, found `*mut S` (expected &-ptr
22-
let y: &T = x; //~ ERROR mismatched types: expected `&T`, found `*mut S` (expected &-ptr
29+
let y: &S = x; //~ ERROR mismatched types
30+
let y: &T = x; //~ ERROR mismatched types
2331

32+
// Test that we cannot convert an immutable ptr to a mutable one using *-ptrs
2433
let x: &mut T = &S; //~ ERROR types differ in mutability
2534
let x: *mut T = &S; //~ ERROR types differ in mutability
2635
let x: *mut S = &S;
27-
//~^ ERROR mismatched types: expected `*mut S`, found `&S` (values differ in mutability)
28-
}
36+
//~^ ERROR mismatched types
37+
38+
// The below four sets of tests test that we cannot implicitly deref a *-ptr
39+
// during a coercion.
40+
let x: *const S = &S;
41+
let y: *const T = x; //~ ERROR mismatched types
42+
43+
let x: *mut S = &mut S;
44+
let y: *mut T = x; //~ ERROR mismatched types
45+
46+
let x: *const Foo<S> = &Foo {f: S};
47+
let y: *const Foo<T> = x; //~ ERROR mismatched types
48+
49+
let x: *mut Foo<S> = &mut Foo {f: S};
50+
let y: *mut Foo<T> = x; //~ ERROR mismatched types
51+
}

src/test/run-fail/dst-raw-slice.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 bounds checking for DST raw slices
12+
// error-pattern:index out of bounds
13+
14+
fn main() {
15+
let a: *const [_] = &[1i, 2, 3];
16+
unsafe {
17+
let _b = (*a)[3];
18+
}
19+
}

src/test/run-pass/dst-coercions.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,27 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// Test coercions involving DST and/or raw pointers
12+
1113
struct S;
1214
trait T {}
1315
impl T for S {}
1416

1517
pub fn main() {
1618
let x: &T = &S;
19+
// Test we can convert from &-ptr to *-ptr of trait objects
1720
let x: *const T = &S;
1821

22+
// Test we can convert from &-ptr to *-ptr of struct pointer (not DST)
1923
let x: *const S = &S;
2024

25+
// As above, but mut
2126
let x: &mut T = &mut S;
2227
let x: *mut T = &mut S;
2328

2429
let x: *mut S = &mut S;
2530

31+
// Test we can chnage the mutability from mut to const.
2632
let x: &T = &mut S;
2733
let x: *const T = &mut S;
28-
}
34+
}

src/test/run-pass/dst-raw.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,22 @@ impl Trait for A {
2323
}
2424
}
2525

26-
pub struct Foo<Sized? T> {
26+
struct Foo<Sized? T> {
2727
f: T
2828
}
2929

3030
pub fn main() {
3131
// raw trait object
3232
let x = A { f: 42 };
33-
let y: *const A = &x;
34-
let z: *const Trait = y;
33+
let z: *const Trait = &x;
3534
let r = unsafe {
3635
(&*z).foo()
3736
};
3837
assert!(r == 42);
3938

4039
// raw DST struct
4140
let p = Foo {f: A { f: 42 }};
42-
let q: *const Foo<A> = &p;
43-
let o: *const Foo<Trait> = q;
41+
let o: *const Foo<Trait> = &p;
4442
let r = unsafe {
4543
(&*o).f.foo()
4644
};
@@ -51,27 +49,29 @@ pub fn main() {
5149
unsafe {
5250
let b = (*a)[2];
5351
assert!(b == 3);
52+
let len = (*a).len();
53+
assert!(len == 3);
5454
}
5555

5656
// raw DST struct with slice
5757
let c: *const Foo<[_]> = &Foo {f: [1i, 2, 3]};
5858
unsafe {
5959
let b = (&*c).f[0];
6060
assert!(b == 1);
61+
let len = (&*c).f.len();
62+
assert!(len == 3);
6163
}
6264

6365
// all of the above with *mut
6466
let mut x = A { f: 42 };
65-
let y: *mut A = &mut x;
66-
let z: *mut Trait = y;
67+
let z: *mut Trait = &mut x;
6768
let r = unsafe {
6869
(&*z).foo()
6970
};
7071
assert!(r == 42);
7172

7273
let mut p = Foo {f: A { f: 42 }};
73-
let q: *mut Foo<A> = &mut p;
74-
let o: *mut Foo<Trait> = q;
74+
let o: *mut Foo<Trait> = &mut p;
7575
let r = unsafe {
7676
(&*o).f.foo()
7777
};
@@ -81,11 +81,15 @@ pub fn main() {
8181
unsafe {
8282
let b = (*a)[2];
8383
assert!(b == 3);
84+
let len = (*a).len();
85+
assert!(len == 3);
8486
}
8587

8688
let c: *mut Foo<[_]> = &mut Foo {f: [1i, 2, 3]};
8789
unsafe {
8890
let b = (&*c).f[0];
8991
assert!(b == 1);
92+
let len = (&*c).f.len();
93+
assert!(len == 3);
9094
}
9195
}

0 commit comments

Comments
 (0)