Skip to content

Commit

Permalink
Reviewer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Sep 2, 2014
1 parent 52d6d3b commit 5520ea8
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/typeck/check/vtable.rs
Expand Up @@ -896,8 +896,8 @@ fn trait_cast_types(fcx: &FnCtxt,
match autoref {
&ty::AutoUnsize(ref k) |
&ty::AutoUnsizeUniq(ref k) => trait_cast_types_unsize(fcx, k, src_ty, sp),
&ty::AutoPtr(_, _, Some(box ref autoref))
| &ty::AutoUnsafe(_, Some(box ref autoref))=> {
&ty::AutoPtr(_, _, Some(box ref autoref)) |
&ty::AutoUnsafe(_, Some(box ref autoref)) => {
trait_cast_types_autoref(fcx, autoref, src_ty, sp)
}
_ => None
Expand Down
21 changes: 12 additions & 9 deletions src/librustc/middle/typeck/coherence.rs
Expand Up @@ -157,16 +157,19 @@ fn get_base_type_def_id(inference_context: &InferCtxt,
ty_unboxed_closure(def_id, _) => {
Some(def_id)
}
ty_ptr(ty::mt {ty, ..}) | ty_rptr(_, ty::mt {ty, ..}) | ty_uniq(ty)
=> match ty::get(ty).sty {
ty_trait(box ty::TyTrait { def_id, .. }) => {
Some(def_id)
}
_ => {
fail!("get_base_type() returned a type that wasn't an \
enum, struct, or trait");
ty_ptr(ty::mt {ty, ..}) |
ty_rptr(_, ty::mt {ty, ..}) |
ty_uniq(ty) => {
match ty::get(ty).sty {
ty_trait(box ty::TyTrait { def_id, .. }) => {
Some(def_id)
}
_ => {
fail!("get_base_type() returned a type that wasn't an \
enum, struct, or trait");
}
}
},
}
ty_trait(box ty::TyTrait { def_id, .. }) => {
Some(def_id)
}
Expand Down
64 changes: 26 additions & 38 deletions src/librustc/middle/typeck/infer/coercion.rs
Expand Up @@ -350,8 +350,7 @@ impl<'f> Coerce<'f> {
}
})
}
(&ty::ty_ptr(ty::mt{ty: t_a, ..}), &ty::ty_ptr(mt_b))
| (&ty::ty_rptr(_, ty::mt{ty: t_a, ..}), &ty::ty_ptr(mt_b)) => {
(&ty::ty_rptr(_, ty::mt{ty: t_a, ..}), &ty::ty_ptr(mt_b)) => {
self.unpack_actual_value(t_a, |sty_a| {
match self.unsize_ty(sty_a, mt_b.ty) {
Some((ty, kind)) => {
Expand Down Expand Up @@ -478,63 +477,52 @@ impl<'f> Coerce<'f> {
b.repr(tcx));

let coercion = Coercion(self.get_ref().trace.clone());
let r_a = self.get_ref().infcx.next_region_var(coercion);

match *sty_a {
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
ty::ty_trait(box ty::TyTrait {
def_id,
ref substs,
bounds,
..
}) => {
let tr = ty::mk_trait(tcx, def_id, substs.clone(), bounds);
let r_a = self.get_ref().infcx.next_region_var(coercion);
let a_borrowed = ty::mk_rptr(tcx, r_a, ty::mt{ mutbl: b_mutbl, ty: tr });

try!(self.subtype(a_borrowed, b));
Ok(Some(AutoDerefRef(AutoDerefRef {
autoderefs: 1,
autoref: Some(AutoPtr(r_a, b_mutbl, None))
})))
}
_ => {
self.subtype(a, b)
}
},
_ => {
self.subtype(a, b)
}
}
self.coerce_object(a, sty_a, b,
|tr| ty::mk_rptr(tcx, r_a, ty::mt{ mutbl: b_mutbl, ty: tr }),
|| AutoPtr(r_a, b_mutbl, None))
}

fn coerce_unsafe_object(&self,
a: ty::t,
sty_a: &ty::sty,
b: ty::t,
b_mutbl: ast::Mutability) -> CoerceResult
a: ty::t,
sty_a: &ty::sty,
b: ty::t,
b_mutbl: ast::Mutability) -> CoerceResult
{
let tcx = self.get_ref().infcx.tcx;

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

self.coerce_object(a, sty_a, b,
|tr| ty::mk_ptr(tcx, ty::mt{ mutbl: b_mutbl, ty: tr }),
|| AutoUnsafe(b_mutbl, None))
}

fn coerce_object(&self,
a: ty::t,
sty_a: &ty::sty,
b: ty::t,
mk_ty: |ty::t| -> ty::t,
mk_adjust: || -> ty::AutoRef) -> CoerceResult
{
let tcx = self.get_ref().infcx.tcx;

match *sty_a {
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) |
ty::ty_ptr(ty::mt{ty, ..}) => match ty::get(ty).sty {
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
ty::ty_trait(box ty::TyTrait {
def_id,
ref substs,
bounds,
..
}) => {
let tr = ty::mk_trait(tcx, def_id, substs.clone(), bounds);
let a_raw = ty::mk_ptr(tcx, ty::mt{ mutbl: b_mutbl, ty: tr });

try!(self.subtype(a_raw, b));
try!(self.subtype(mk_ty(tr), b));
Ok(Some(AutoDerefRef(AutoDerefRef {
autoderefs: 1,
autoref: Some(AutoUnsafe(b_mutbl, None))
autoref: Some(mk_adjust())
})))
}
_ => {
Expand Down
35 changes: 29 additions & 6 deletions src/test/compile-fail/dst-bad-coercions.rs
Expand Up @@ -8,21 +8,44 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test implicit coercions involving DSTs and raw pointers.

struct S;
trait T {}
impl T for S {}

struct Foo<Sized? T> {
f: T
}

pub fn main() {
// Test that we cannot convert from *-ptr to &-ptr
let x: *const S = &S;
let y: &S = x; //~ ERROR mismatched types: expected `&S`, found `*const S` (expected &-ptr
let y: &T = x; //~ ERROR mismatched types: expected `&T`, found `*const S` (expected &-ptr
let y: &S = x; //~ ERROR mismatched types
let y: &T = x; //~ ERROR mismatched types

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

// Test that we cannot convert an immutable ptr to a mutable one using *-ptrs
let x: &mut T = &S; //~ ERROR types differ in mutability
let x: *mut T = &S; //~ ERROR types differ in mutability
let x: *mut S = &S;
//~^ ERROR mismatched types: expected `*mut S`, found `&S` (values differ in mutability)
}
//~^ ERROR mismatched types

// The below four sets of tests test that we cannot implicitly deref a *-ptr
// during a coercion.
let x: *const S = &S;
let y: *const T = x; //~ ERROR mismatched types

let x: *mut S = &mut S;
let y: *mut T = x; //~ ERROR mismatched types

let x: *const Foo<S> = &Foo {f: S};
let y: *const Foo<T> = x; //~ ERROR mismatched types

let x: *mut Foo<S> = &mut Foo {f: S};
let y: *mut Foo<T> = x; //~ ERROR mismatched types
}
19 changes: 19 additions & 0 deletions src/test/run-fail/dst-raw-slice.rs
@@ -0,0 +1,19 @@
// Copyright 2014 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.

// Test bounds checking for DST raw slices
// error-pattern:index out of bounds

fn main() {
let a: *const [_] = &[1i, 2, 3];
unsafe {
let _b = (*a)[3];
}
}
8 changes: 7 additions & 1 deletion src/test/run-pass/dst-coercions.rs
Expand Up @@ -8,21 +8,27 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test coercions involving DST and/or raw pointers

struct S;
trait T {}
impl T for S {}

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

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

// As above, but mut
let x: &mut T = &mut S;
let x: *mut T = &mut S;

let x: *mut S = &mut S;

// Test we can chnage the mutability from mut to const.
let x: &T = &mut S;
let x: *const T = &mut S;
}
}
22 changes: 13 additions & 9 deletions src/test/run-pass/dst-raw.rs
Expand Up @@ -23,24 +23,22 @@ impl Trait for A {
}
}

pub struct Foo<Sized? T> {
struct Foo<Sized? T> {
f: T
}

pub fn main() {
// raw trait object
let x = A { f: 42 };
let y: *const A = &x;
let z: *const Trait = y;
let z: *const Trait = &x;
let r = unsafe {
(&*z).foo()
};
assert!(r == 42);

// raw DST struct
let p = Foo {f: A { f: 42 }};
let q: *const Foo<A> = &p;
let o: *const Foo<Trait> = q;
let o: *const Foo<Trait> = &p;
let r = unsafe {
(&*o).f.foo()
};
Expand All @@ -51,27 +49,29 @@ pub fn main() {
unsafe {
let b = (*a)[2];
assert!(b == 3);
let len = (*a).len();
assert!(len == 3);
}

// raw DST struct with slice
let c: *const Foo<[_]> = &Foo {f: [1i, 2, 3]};
unsafe {
let b = (&*c).f[0];
assert!(b == 1);
let len = (&*c).f.len();
assert!(len == 3);
}

// all of the above with *mut
let mut x = A { f: 42 };
let y: *mut A = &mut x;
let z: *mut Trait = y;
let z: *mut Trait = &mut x;
let r = unsafe {
(&*z).foo()
};
assert!(r == 42);

let mut p = Foo {f: A { f: 42 }};
let q: *mut Foo<A> = &mut p;
let o: *mut Foo<Trait> = q;
let o: *mut Foo<Trait> = &mut p;
let r = unsafe {
(&*o).f.foo()
};
Expand All @@ -81,11 +81,15 @@ pub fn main() {
unsafe {
let b = (*a)[2];
assert!(b == 3);
let len = (*a).len();
assert!(len == 3);
}

let c: *mut Foo<[_]> = &mut Foo {f: [1i, 2, 3]};
unsafe {
let b = (&*c).f[0];
assert!(b == 1);
let len = (&*c).f.len();
assert!(len == 3);
}
}

5 comments on commit 5520ea8

@bors
Copy link
Contributor

@bors bors commented on 5520ea8 Sep 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from nikomatsakis
at nrc@5520ea8

@bors
Copy link
Contributor

@bors bors commented on 5520ea8 Sep 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging nick29581/rust/dst-raw = 5520ea8 into auto

@bors
Copy link
Contributor

@bors bors commented on 5520ea8 Sep 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nick29581/rust/dst-raw = 5520ea8 merged ok, testing candidate = e59a458

@bors
Copy link
Contributor

@bors bors commented on 5520ea8 Sep 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = e59a458

Please sign in to comment.